Advertisement
JonD1988

Reading Battery Voltages Code Rev 2

Jul 7th, 2023
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h> //Needed to Use ADS1115 ADC - See Reference 12
  2. #include <ADS1115_lite.h> //Needed to Use ADS1115 ADC - See Reference 12
  3. ADS1115_lite ads(ADS1115_DEFAULT_ADDRESS); //0x48 addr pin connected to GND. Needed to Use ADS1115 ADC - See Reference 12
  4. long raw1 = 0, raw2 = 0; //Needed to Use ADS1115 ADC - See Reference 12
  5. double resolution=0; //Needed to Use ADS1115 ADC - See Reference 12
  6. int batt1Analog = 0, batt2Analog=0; //Analog readings from batt1Pin and batt2Pin and then actual battery voltages
  7. float batt1V=0, batt2V=0;
  8. unsigned long previousMillis = 0;      // will store last time the battery voltages were read
  9. const long interval = 10000;           // interval at which to read the battery voltage (milliseconds)
  10. #define battCutOff 3.0 //Voltage to Cutoff Motors to Protect the Batteries
  11. #define numReadsToAvg 3 //Number of Voltage Readings to Store and Take Average Of
  12. float batt1VArray[numReadsToAvg]; //Array to store batt1V readings i.e. battery 1 voltages
  13. float batt2VArray[numReadsToAvg]; //Array to store batt2V readings i.e. battery 2 voltages
  14. int batt1VIndex = 0, batt2VIndex = 0; //Keeps track of the array indexes for the batt1VArray and batt2VArray
  15. float batt1VAvg = 3.0, batt2VAvg = 3.0; //Initialize battery voltage reading averages to 3.0
  16. float batt1VSum = 0.0, batt2VSum = 0.0; //Sums all the elements in the batt1VArray and the batt2VArray for use in the average calculations
  17. int batt1VCount = 0, batt2VCount = 0; //Variable to keep track of the count of how many elements have been stored in the batt1VArray and batt2VArray - really only meant to be used initially before all elements of the arrays have been updated with non-zero values
  18.  
  19. void setup()
  20. { //start of void setup()
  21.   ads_config(); //Needed to Use ADS1115 ADC - See Reference 12
  22.   //Initialize elements of batt1VArray and batt2VArray to 3.5
  23.   for(int m = 0; m < numReadsToAvg; m++)
  24.   { //Start of for loop to initialize arrays to 3.5
  25.       batt1VArray[m] = 3.5;
  26.       batt2VArray[m] = 3.5;
  27.   } //End of for loop to initialize arrays to 3.5
  28. } //end of void setup()
  29.  
  30. void loop()
  31. { //start of void loop()
  32.  
  33. unsigned long currentMillis = millis(); //Checks the current time
  34.  
  35.     if (currentMillis - previousMillis >= interval) //Check to see if it's time to read the battery voltages (if the difference between the current time and last time you read the battery voltages is bigger than the interval at which you want to read the battery voltages)
  36.     { //Start of if (currentMillis - previousMillis >= interval)
  37.         previousMillis = currentMillis; //save the last time you read the battery voltages before you read the battery voltages
  38.         readBatts(); //Read the battery voltages
  39.     } //End of if (currentMillis - previousMillis >= interval)
  40.  
  41. } //end of void loop()
  42.  
  43. void readBatts()
  44. { //Start of readBatts function definition
  45.   raw1 = ads_read1(); //Reads A0 from ADS1115 - Reference 12
  46.   resolution=4096.00/32752.00;
  47.   delay(200);
  48.   float result1=(raw1*resolution)/1000.00; //batt1 voltage + batt2 voltage
  49.   raw2 = ads_read2(); //Reads A0 from ADS1115 - Reference 12
  50.   delay(200);
  51.   float result2=(raw2*resolution)/1000.00; //batt2 voltage alone
  52.   batt1V = result1 - result2; //Assigns battery 1 voltage alone to batt1V variable
  53.   batt1V = 11.03*batt1V;
  54.   batt2V = result2; //Assigns result2 to the batt2V variable
  55.   batt2V = 11.0*batt2V;
  56.  
  57.   batt1VArray[batt1VIndex] = batt1V; //Store the current batt1V value into the element of the batt1VArray that matches batt1VIndex
  58.   batt2VArray[batt2VIndex] = batt2V; //Store the current batt2V value into the element of the batt2VArray that matches batt2VIndex
  59.   batt1VIndex = (batt1VIndex + 1) % numReadsToAvg; //Increments batt1VIndex by 1 each time this line runs and the % numReadsToAvg means that the batt1VIndex will take on values from 0 up to but not including numReadsToAvg (i.e. 0, 1, 2, 0, 1, 2, etc. if numReadsToAvg = 3)
  60.   batt2VIndex = (batt2VIndex + 1) % numReadsToAvg; //Increments batt2VIndex by 1 each time this line runs and the % numReadsToAvg means that the batt2VIndex will take on values from 0 up to but not including numReadsToAvg (i.e. 0, 1, 2, 0, 1, 2, etc. if numReadsToAvg = 3)
  61.  
  62.   //Perform the average calculation with non-zero elements of the batt1VArray and batt2VArray
  63.   for(int i=0; i<numReadsToAvg; i++)
  64.   { //Start of for loop to step through the elements in the batt1VArray
  65.           batt1VSum += batt1VArray[i]; //Add the current value to the running sum for the average calculation
  66.   } //end of for loop to step through the elements in the batt1VArray
  67.  
  68.       batt1VAvg = batt1VSum / numReadsToAvg; //Calculates the average
  69.   //Without the following line of code batt1VSum could increase without limit (as much as that variable type can hold) retaining impact from values beyond the current set of stored readings in the array and therefore wouldn't react to newer voltage readings after a while
  70.   batt1VSum = 0.0; //After the sum is used in the calculation of the current batt1VAvg set it back to its initial value so that it isn't impacted by past readings. The sum is meant to be used for the current set of numReadsToAvg
  71.  
  72.   for(int j=0; j<numReadsToAvg; j++)
  73.   { //Start of for loop to step through the elements in the batt2VArray
  74.           batt2VSum += batt2VArray[j]; //Add the current value to the running sum for the average calculation
  75.   } //end of for loop to step through the elements in the batt2VArray
  76.  
  77.       batt2VAvg = batt2VSum / numReadsToAvg; //Calculates the average
  78.   //Without the following line of code batt2VSum could increase without limit (as much as that variable type can hold) retaining impact from values beyond the current set of stored readings in the array and therefore wouldn't react to newer voltage readings after a while
  79.   batt2VSum = 0.0; //After the sum is used in the calculation of the current batt2VAvg set it back to its initial value so that it isn't impacted by past readings. The sum is meant to be used for the current set of numReadsToAvg
  80.  
  81.   //This section of code sends the battery voltages to the Android app
  82.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  83.   SerialBT.print("W");  //Delimeter separating values which the receiving app is programmed to recognize
  84.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  85.   //SerialBT.print(batt1V);
  86.   SerialBT.print(batt1VAvg);  
  87.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  88.   SerialBT.print("w");  //Delimeter separating values which the receiving app is programmed to recognize
  89.  
  90.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  91.   SerialBT.print("Z");  //Delimeter separating values which the receiving app is programmed to recognize
  92.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  93.   //SerialBT.print(batt2V);
  94.   SerialBT.print(batt2VAvg);
  95.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize      
  96.   SerialBT.print("z");  //Delimeter separating values which the receiving app is programmed to recognize
  97.  
  98.   //if (batt1V < battCutOff || batt2V < battCutOff) //If either battery drops below the cutoff voltage
  99.   if (batt1VAvg < battCutOff || batt2VAvg < battCutOff) //If either battery drops below the cutoff voltage
  100.   { //Start of if (batt1V < battCutOff || batt2V < battCutOff)
  101.     stopVehicle(); //Stop Power to the Motors
  102.     lightFState = 0; //Turns Off Front Lights
  103.     lightBState = 0; //Turns Off Back Lights
  104.     hornH(); //Turns horn on briefly
  105.     delay(100);
  106.     hornL();
  107.    } //End of if (batt1V < battCutOff || batt2V < battCutOff)
  108.  
  109. } //End of readBatts function definition
  110.  
  111. void ads_config(){
  112.   ads.setGain(ADS1115_REG_CONFIG_PGA_4_096V); // GAIN_ONE and resolution to ± 4.096V
  113.   ads.setSampleRate(ADS1115_REG_CONFIG_DR_8SPS); // Set to the fastest MODE 860Samples per sec
  114. }
  115.  
  116. int16_t ads_read1(){
  117.   ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_0);  // Single mode input on AIN0 (AIN0 - GND)
  118.   ads.triggerConversion();  // Triggered mannually
  119.   return ads.getConversion();  // returns int16_t value
  120. }
  121.  
  122. int16_t ads_read2(){
  123.   ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_1);  // Single mode input on AIN1 (AIN1 - GND)
  124.   ads.triggerConversion();  // Triggered mannually
  125.   return ads.getConversion();  // returns int16_t value
  126. }
  127. //Reference 12- Reading Analog Voltages Using ADS1115 https://www.youtube.com/watch?v=u-1TRpLGH04
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement