Advertisement
JonD1988

Battery Reading Code Rev 0

Jul 6th, 2023
935
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.2 //Voltage to Cutoff Motors to Protect the Batteries
  11.  
  12. void setup()
  13. { //start of void setup()
  14.   ads_config(); //Needed to Use ADS1115 ADC - See Reference 12
  15. } //end of void setup()
  16.  
  17. void loop()
  18. { //start of void loop()
  19.  
  20. unsigned long currentMillis = millis(); //check to see if it's time to read the battery voltages; that is, 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.
  21.  
  22.     if (currentMillis - previousMillis >= interval)
  23.     { //Start of if (currentMillis - previousMillis >= interval)
  24.         previousMillis = currentMillis; //save the last time you read the battery voltages before you read the battery voltages
  25.         readBatts(); //Read the battery voltages
  26.     } //End of if (currentMillis - previousMillis >= interval)
  27.  
  28. } //end of void loop()
  29.  
  30. void readBatts()
  31. { //Start of readBatts function definition
  32.   raw1 = ads_read1(); //Reads A0 from ADS1115 - Reference 12
  33.   resolution=4096.00/32752.00;
  34.   delay(200);
  35.   float result1=(raw1*resolution)/1000.00; //batt1 voltage + batt2 voltage
  36.   raw2 = ads_read2(); //Reads A0 from ADS1115 - Reference 12
  37.   delay(200);
  38.   float result2=(raw2*resolution)/1000.00; //batt2 voltage alone
  39.   batt1V = result1 - result2; //Assigns battery 1 voltage alone to batt1V variable
  40.   batt1V = 11.03*batt1V;
  41.   batt2V = result2; //Assigns result2 to the batt2V variable
  42.   batt2V = 11.0*batt2V;
  43.  
  44.   //This section of code sends the battery voltages to the Android app
  45.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  46.   SerialBT.print("W");  //Delimeter separating values which the receiving app is programmed to recognize
  47.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  48.   SerialBT.print(batt1V);  
  49.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  50.   SerialBT.print("w");  //Delimeter separating values which the receiving app is programmed to recognize
  51.  
  52.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  53.   SerialBT.print("Z");  //Delimeter separating values which the receiving app is programmed to recognize
  54.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize
  55.   SerialBT.print(batt2V);
  56.   SerialBT.print("|");  //Delimeter separating values which the receiving app is programmed to recognize      
  57.   SerialBT.print("z");  //Delimeter separating values which the receiving app is programmed to recognize
  58.  
  59.   if (batt1V < battCutOff || batt2V < battCutOff) //If either battery drops below the cutoff voltage
  60.   { //Start of if (batt1V < battCutOff || batt2V < battCutOff)
  61.     stopVehicle(); //Stop Power to the Motors
  62.     lightFState = 0; //Turns Off Front Lights
  63.     lightBState = 0; //Turns Off Back Lights
  64.     hornH(); //Turns horn on briefly
  65.     delay(100);
  66.     hornL();
  67.    } //End of if (batt1V < battCutOff || batt2V < battCutOff)
  68.  
  69. } //End of readBatts function definition
  70.  
  71. void ads_config(){
  72.   ads.setGain(ADS1115_REG_CONFIG_PGA_4_096V); // GAIN_ONE and resolution to ± 4.096V
  73.   ads.setSampleRate(ADS1115_REG_CONFIG_DR_8SPS); // Set to the fastest MODE 860Samples per sec
  74. }
  75.  
  76. int16_t ads_read1(){
  77.   ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_0);  // Single mode input on AIN0 (AIN0 - GND)
  78.   ads.triggerConversion();  // Triggered mannually
  79.   return ads.getConversion();  // returns int16_t value
  80. }
  81.  
  82. int16_t ads_read2(){
  83.   ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_1);  // Single mode input on AIN1 (AIN1 - GND)
  84.   ads.triggerConversion();  // Triggered mannually
  85.   return ads.getConversion();  // returns int16_t value
  86. }
  87. //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