Advertisement
CountRogula

Untitled

Nov 29th, 2023
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.32 KB | Source Code | 0 0
  1. /*************************************************************************************
  2. /*                                Battery Monitoring                                 *
  3. /************************************************************************************/
  4. void checkBattery() {
  5.  
  6.   float previousBatteryVoltage = batteryVoltage;
  7.   float vRefScale = (3.3f / 4096.0f) * ((batteryR1 + batteryR2) / batteryR2);
  8.  
  9.   adc1_get_raw(ADC1_CHANNEL_6); //read and discard
  10.   batteryPinValue = float( adc1_get_raw(ADC1_CHANNEL_6) ); //take a raw ADC reading
  11.  
  12.   batteryVoltage = batteryPinValue * vRefScale;
  13.  
  14.   realBatteryPercent  = map(batteryVoltage,3.3f,4.2f,0,100);
  15.   Serial.println("\Battery pinValue: "   + String(batteryPinValue) + " vRefScale: " + String(vRefScale,7)+ " Voltage: " + String(batteryVoltage));
  16.  
  17.   if (previousBatteryVoltage != 0) { // if the volate isnt at least 0.05 volts diference dont change the value to compenste for slight variations
  18.     if (abs(previousBatteryVoltage - batteryVoltage) < 0.05f) {
  19.       batteryVoltage = previousBatteryVoltage;
  20.     }
  21.   }
  22.  
  23.   realBatteryPercent  = map(batteryVoltage*100,360,420,0,100);
  24.   Serial.println("Adjusted Voltage: " + String(batteryVoltage) + " Percent: " + String(batteryPercent));
  25.  
  26.   if (batteryVoltage <= 3.6) {
  27.     batteryPercent = 0;
  28.   } else if (batteryVoltage <= 3.65) {
  29.     batteryPercent = 10;
  30.   } else if (batteryVoltage <= 3.7) {
  31.     batteryPercent = 20;
  32.   } else if (batteryVoltage <= 3.75) {
  33.     batteryPercent = 30;
  34.   } else if (batteryVoltage <= 3.8) {
  35.     batteryPercent = 40;
  36.   } else if (batteryVoltage <= 3.85) {
  37.     batteryPercent = 50;
  38.   } else if (batteryVoltage <= 3.9) {
  39.     batteryPercent = 60;
  40.   } else if (batteryVoltage <= 3.95) {
  41.     batteryPercent = 70;
  42.   } else if (batteryVoltage <= 4) {
  43.     batteryPercent = 80;
  44.   } else if (batteryVoltage <= 4.1) {
  45.     batteryPercent = 90;
  46.   } else {
  47.     batteryPercent = 100;
  48.   }
  49. }
  50.  
  51. void checkSolar() {
  52.  
  53.   float vRefScale = (3.3f / 4096.0f) * ((solarR1 + solarR2) / solarR2);
  54.  
  55.   adc1_get_raw(ADC1_CHANNEL_7); //read and discard
  56.   float pinValue = float( adc1_get_raw(ADC1_CHANNEL_7) ); //take a raw ADC reading
  57.  
  58.   solarVoltage = pinValue * vRefScale;
  59.   Serial.println("Solar pinValue: " + String(pinValue) + " vRefScale: " + String(vRefScale,7) + " Voltage: " + String(solarVoltage));
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement