From dff18e081110ddd4a612add3f321337f3429c0f6 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Wed, 22 Jun 2022 21:51:27 +0100 Subject: [PATCH] Fix decimal point and stupid initial state --- battery_tester.ino | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/battery_tester.ino b/battery_tester.ino index 8a78fe2..9b7865a 100644 --- a/battery_tester.ino +++ b/battery_tester.ino @@ -29,6 +29,15 @@ unsigned char display_state = 0; Chrono timer; Chrono debounce; +Chrono blank_timer; + +float shunt_voltage = 0; +float shunt_current = 0; +float battery_voltage = 0; +float amps_in_period = 0; +float watts_in_period = 0; +float mamphours = 0; +float mwatthours = 0; void writeDigit(char d, char val, bool dec) { @@ -73,12 +82,12 @@ void writeDigit(char d, char val, bool dec) { } } if (dec) { - digitalWrite(15, HIGH); + digitalWrite(segments[7], HIGH); } digitalWrite(digits[d], LOW); delay(1); digitalWrite(digits[d], HIGH); - digitalWrite(15, LOW); + digitalWrite(segments[7], LOW); } void writeLetter(char d, const bool val[]) { @@ -117,6 +126,14 @@ void showCurrent(float current) { writeLetter(3, I); } +void showWh(float watthours) { + int w = watthours; + writeDigit(0, (w/1000U) % 10, true); + writeDigit(1, (w/100U) % 10, false); + writeDigit(2, (w/10U) % 10, false); + writeLetter(3, P); +} + void setup() { // put your setup code here, to run once: @@ -141,13 +158,7 @@ void setup() { } void loop() { - float shunt_voltage; - float shunt_current; - float battery_voltage; - float amps_in_period; - float watts_in_period; - float mamphours; - float mwatthours; + if (timer.hasPassed(1000)) { unsigned long passed = timer.elapsed(); @@ -155,9 +166,8 @@ void loop() { battery_voltage = readVoltage(A0); if (battery_voltage < 0.8) { - delay(200); + blank_timer.restart(); } else { - shunt_voltage = readVoltage(A1); shunt_current = shunt_voltage/APPARENT_SHUNT_RESISTANCE; @@ -173,9 +183,8 @@ void loop() { } if (debounce.hasPassed(300) && !digitalRead(19)) { display_state = (display_state + 1) % 4; - Serial.println(display_state); debounce.restart(); - } else { + } else if (blank_timer.hasPassed(200)) { switch (display_state) { case 0: showNumber(mamphours); @@ -187,8 +196,7 @@ void loop() { showCurrent(shunt_current); break; case 3: - showNumber(mwatthours); - writeLetter(3, P); + showWh(mwatthours); break; } } -- 2.46.2