Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Created December 24, 2017 08:33
Show Gist options
  • Save dcbriccetti/2e5853a99f37071c057956f904266c52 to your computer and use it in GitHub Desktop.
Save dcbriccetti/2e5853a99f37071c057956f904266c52 to your computer and use it in GitHub Desktop.
C = 0.36 # uF
R1 = 1000 # Ohms
def analog_read():
return 10000
def read_resistance():
n = 10
total = 0
for i in range(1, n):
total = total + analog_read()
print('Total %d is %.1f' % (i, total))
t = total / float(n)
T = t * 0.632 * 3.3
r = (T / C) - R1
print('Only executed %d times' % len(range(1, n)))
return r
def read_resistance_corrected():
n = 10
total = 0
for i in range(n):
total = total + analog_read()
print('Total %d is %.1f' % (i, total))
t = total / float(n)
T = t * 0.632 * 3.3
r = (T / C) - R1
return r
def read_resistance_more_compact():
n = 10
avg_resistance = sum((analog_read() for i in range(10))) / n
T = avg_resistance * 0.632 * 3.3
return T / C - R1
print('Incorrect resistance: %.1f' % read_resistance())
print('Corrected resistance: %.1f' % read_resistance_corrected())
print('Corrected resistance from shortened function: %.1f' % read_resistance_more_compact())
'''
Total 1 is 10000.0
Total 2 is 20000.0
Total 3 is 30000.0
Total 4 is 40000.0
Total 5 is 50000.0
Total 6 is 60000.0
Total 7 is 70000.0
Total 8 is 80000.0
Total 9 is 90000.0
Only executed 9 times
Incorrect resistance: 51140.0
Total 0 is 10000.0
Total 1 is 20000.0
Total 2 is 30000.0
Total 3 is 40000.0
Total 4 is 50000.0
Total 5 is 60000.0
Total 6 is 70000.0
Total 7 is 80000.0
Total 8 is 90000.0
Total 9 is 100000.0
Corrected resistance: 56933.3
Corrected resistance from shortened function: 56933.3
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment