]> git.friedersdorff.com Git - max/battery_tester.git/commitdiff
Stop counting up when batter low threshold is reached
authorMaximilian Friedersdorff <max@friedersdorff.com>
Tue, 21 Jun 2022 13:57:45 +0000 (14:57 +0100)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Tue, 21 Jun 2022 13:57:45 +0000 (14:57 +0100)
battery_tester.ino

index bed857ffe495bd70e35b76a9f91e3f7eea7eda78..913d434585e0f37b4a098cc648196ac6c28e00b9 100644 (file)
@@ -27,43 +27,43 @@ Chrono timer;
 void writeDigit(char d, char val) {
     const bool *segs;
     switch (val) {
-      case 0:
-        segs = zero;
-        break;
-      case 1:
-        segs = one;
-        break;
-      case 2:
-        segs = two;
-        break;
-      case 3:
-        segs = three;
-        break;
-      case 4:
-        segs = four;
-        break;
-      case 5:
-        segs = five;
-        break;
-      case 6:
-        segs = six;
-        break;
-      case 7:
-        segs = seven;
-        break;
-      case 8:
-        segs = eight;
-        break;
-      case 9:
-        segs = nine;
-        break;
+        case 0:
+            segs = zero;
+            break;
+        case 1:
+            segs = one;
+            break;
+        case 2:
+            segs = two;
+            break;
+        case 3:
+            segs = three;
+            break;
+        case 4:
+            segs = four;
+            break;
+        case 5:
+            segs = five;
+            break;
+        case 6:
+            segs = six;
+            break;
+        case 7:
+            segs = seven;
+            break;
+        case 8:
+            segs = eight;
+            break;
+        case 9:
+            segs = nine;
+            break;
     }
     for (int i = 0; i < 7; ++i) {
-      if (segs[i]) {
-        digitalWrite(segments[i], HIGH);      
-      } else {
-        digitalWrite(segments[i], LOW);
-      }
+        if (segs[i]) {
+            digitalWrite(segments[i], HIGH);      
+        } else {
+            digitalWrite(segments[i], LOW);
+        }
     }
     digitalWrite(digits[d], LOW);
     delay(1);
@@ -71,69 +71,70 @@ void writeDigit(char d, char val) {
 }
 
 void showNumber(float number) {
-  int n = number;
-  writeDigit(0, (n/1000U) % 10);
-  writeDigit(1, (n/100U) % 10);
-  writeDigit(2, (n/10U) % 10);
-  writeDigit(3, n % 10);
+    int n = number;
+    writeDigit(0, (n/1000U) % 10);
+    writeDigit(1, (n/100U) % 10);
+    writeDigit(2, (n/10U) % 10);
+    writeDigit(3, n % 10);
 }
 
 
 void setup() {
-  // put your setup code here, to run once:
-  Serial.begin(9600);
-  analogReference(EXTERNAL);
-
-  for (int i = 0; i < 4; ++i) {
-    pinMode(digits[i], OUTPUT);
-    digitalWrite(digits[i], HIGH);
-  }
-  for (int i = 0; i < 8; ++i) {
-    pinMode(segments[i], OUTPUT);
-    digitalWrite(segments[i], LOW);
-  }
+    // put your setup code here, to run once:
+    Serial.begin(9600);
+    analogReference(EXTERNAL);
+
+    for (int i = 0; i < 4; ++i) {
+        pinMode(digits[i], OUTPUT);
+        digitalWrite(digits[i], HIGH);
+    }
+    for (int i = 0; i < 8; ++i) {
+        pinMode(segments[i], OUTPUT);
+        digitalWrite(segments[i], LOW);
+    }
 }
 
 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();
-    timer.restart();
-
-    shunt_voltage = readVoltage(A1);
-    shunt_current = shunt_voltage/APPARENT_SHUNT_RESISTANCE;
-
-    battery_voltage = readVoltage(A0);
-
-    amps_in_period = shunt_current * passed/1000;
-    watts_in_period = amps_in_period * battery_voltage;
-    
-    ampseconds += amps_in_period;
-    wattseconds += watts_in_period;
-    mamphours = ampseconds/3.6;
-    mwatthours = wattseconds/3.6;
-
-    if (battery_voltage < 0.8) {
-      delay(200);
+    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)) {
+        timer.restart();
+        if (battery_voltage < 0.8) {
+            delay(200);
+        } else {
+            unsigned long passed = timer.elapsed();
+            
+
+            shunt_voltage = readVoltage(A1);
+            shunt_current = shunt_voltage/APPARENT_SHUNT_RESISTANCE;
+
+            battery_voltage = readVoltage(A0);
+
+            amps_in_period = shunt_current * passed/1000;
+            watts_in_period = amps_in_period * battery_voltage;
+
+            ampseconds += amps_in_period;
+            wattseconds += watts_in_period;
+            mamphours = ampseconds/3.6;
+            mwatthours = wattseconds/3.6;
+        }
     }
-  }
-  showNumber(mamphours);
+    showNumber(mamphours);
 }
 
 float readVoltage(int pin) {
-  
-  int val = 0;
-  for (int i = 0; i < 4; ++i) {
-    val = val + analogRead(pin);
-    delay(0.5);
-  }
-
-  return (val / 4) * VOLTAGE_REFERENCE/1023.0;
+
+    int val = 0;
+    for (int i = 0; i < 4; ++i) {
+        val = val + analogRead(pin);
+        delay(0.5);
+    }
+
+    return (val / 4) * VOLTAGE_REFERENCE/1023.0;
 }