Browse Source

Optimized voltage reading average logic

Toby Chui 6 months ago
parent
commit
2256a7e1ed
1 changed files with 12 additions and 9 deletions
  1. 12 9
      firmware/pd-psu_v3/pd-psu_v3.ino

+ 12 - 9
firmware/pd-psu_v3/pd-psu_v3.ino

@@ -46,24 +46,27 @@ void setup() {
 }
 
 void loop() {
-  //Read the analog reading of both pins
-  voltageReading = analogRead(VOLTAGE_PIN);
+  //Get Output Voltage
+  voltageReading = 0;
+  for (int i = 0; i < 10; i++) {
+    voltageReading += map(analogRead(VOLTAGE_PIN), 0, 255, 0, 500) * 4;  //Convert ADC to Volt
+  }
+  voltageReading /= 10;
+
+  //Get output current
   currentReading = analogRead(CURRENT_PIN);
+  currentReading = (2.5 - (currentReading * (5.0 / 255))) / 0.185;  //Max range -5A to 5A
   isCCMode = !digitalRead(CCCV_PIN);
 
-  //Convert them to actual voltage / current value
-  voltageReading = map(voltageReading, 0, 255, 0, 500) * 4;  //30k - 10k voltage divider
-  currentReading = (2.5 - (currentReading * (5.0 / 255)) )/0.185;      //Max range 0 to 5A
-
   //Cap the range of digits to make sure it won't overflow
   voltageReading = min(voltageReading, 9999);
   currentReading = min(currentReading, 999);
 
   //Print result on USB Serial
-  USBSerial_print(((float)voltageReading/100));
+  USBSerial_print(((float)voltageReading / 100));
   USBSerial_print(",");
-  USBSerial_println(((float)currentReading/100));
-  
+  USBSerial_println(((float)currentReading / 100));
+
   //Update OLED display
   PS_Screen(voltageReading, currentReading, isCCMode);
 }