浏览代码

Added battery voltage monitor

Toby Chui 11 月之前
父节点
当前提交
b54e244486
共有 2 个文件被更改,包括 21 次插入6 次删除
  1. 1 1
      InkyDash/InkyDash.ino
  2. 20 5
      InkyDash/battery.ino

+ 1 - 1
InkyDash/InkyDash.ino

@@ -121,7 +121,7 @@ float currentHumd = 50.0;
 float currentRain = 0.0;
 float maxTemp = 0.0;
 float minTemp = 0.0;
-int batRemain = 20; //In percentage
+int batRemain = 0; //In percentage
 
 /* Schedulers */
 void datetimeUpdateCallback();

+ 20 - 5
InkyDash/battery.ino

@@ -7,18 +7,33 @@
 
 */
 
+float mapfloat(float x, float in_min, float in_max, float out_min, float out_max){
+  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
 //Update the battery reading from ADC pin
-void updateBatteryReading(){
-  batRemain = 19;
+void updateBatteryReading() {
+  int rawValue = analogRead(A0);
+  // rawValue shd be within 0 - 0.854v range
+  // aka 0 - 875 (ADC Output)
+  float voltage = rawValue * (1 / 1023.0);
+  voltage *= 4.91; //Using a 4.7k-1.2k voltage divider
+
+  //Convert voltage to percentage
+  int percentage = mapfloat(voltage, 3.2, 4.2, 0, 100);
+  batRemain = percentage;
+  Serial.println(rawValue);
+  Serial.println(voltage);
+  Serial.println(batRemain);
 }
 
 //Draw the battery icon at the bottom right hand corner
 void drawBatteryIcon() {
   updateBatteryReading();
-  
+
   //Draw the battery icon
   uint16_t barColor = GxEPD_BLACK;
-  if (batRemain <= 20){
+  if (batRemain <= 20) {
     barColor = GxEPD_RED;
   }
   int iconBaseX = display.width() - 50;
@@ -27,7 +42,7 @@ void drawBatteryIcon() {
   display.fillRoundRect(iconBaseX + 40, iconBaseY + 5, 4, 10, 3, GxEPD_BLACK);
   //Internal bar
   int maxBarWidth = 34;
-  int barWidth = int(float(batRemain)/100.0 * maxBarWidth);
+  int barWidth = int(float(batRemain) / 100.0 * maxBarWidth);
   display.fillRoundRect(iconBaseX + 3, iconBaseY + 3, barWidth, 14, 3, barColor);
 
   //Draw percentage