-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (63 loc) · 2.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from machine import Pin, ADC
import time
import logs
ADCLowVoltPin = 26 #GP26
ADCMaxVoltPin = 27 #GP27
PicoVoltage = 3.3
ADC16BitRange = 65536
LEDMeterRange = 10
batterySmallVolt = 1.5 #1.5v
batteryHighVolt = 3.0 #9v
def main():
LEDSegDisplay = []
log = logs.logger("batterycheck.log",2048)
try:
batteryLowVoltage = ADC(ADCLowVoltPin)
batteryHighVoltage = ADC(ADCMaxVoltPin)
voltagePerDegree = PicoVoltage / ADC16BitRange
i = LEDMeterRange
while i > 0:
LEDSegDisplay.append(Pin(i, Pin.OUT))
i -= 1
holdvalue = -1
while True:
percentageOfBattery = 0
batteryVoltage = voltagePerDegree * batteryLowVoltage.read_u16()
percentageOfBattery = batteryVoltage/batterySmallVolt
LEDdisplay = int(percentageOfBattery*LEDMeterRange)
log.write("low check percentageOfBattery: {0} LEDdisplay: {1}".format(percentageOfBattery, LEDdisplay))
#Calculate 9v reading when 1.5v pin is not being used
if LEDdisplay < 1:
batteryVoltage = voltagePerDegree * batteryHighVoltage.read_u16() * 22.5 # when running on 3v power supply
percentageOfBattery = batteryVoltage/batteryHighVolt
LEDdisplay = int(percentageOfBattery*LEDMeterRange)
log.write("high check percentageOfBattery: {0} LEDdisplay: {1}".format(percentageOfBattery, LEDdisplay))
if LEDdisplay > LEDMeterRange:
LEDdisplay = LEDMeterRange
for i in range(0,LEDMeterRange):
time.sleep(0.07)
if i < LEDdisplay:
LEDSegDisplay[i].high()
if holdvalue < i:
holdvalue = i
#print("LED ", i, " on")
else:
LEDSegDisplay[i].low()
#print("for LED ", i, " off")
if LEDdisplay > 0:
time.sleep(.5)
break
except KeyboardInterrupt:
print("stopping program")
finally:
print("Graceful exit")
i = len(LEDSegDisplay)-1
while i >= 0:
if i != holdvalue:
LEDSegDisplay[i].low()
i -= 1
time.sleep(0.07)
time.sleep(1)
LEDSegDisplay[holdvalue].low()
if __name__ == '__main__':
main()