Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h> //Needed to Use ADS1115 ADC - See Reference 12
- #include <ADS1115_lite.h> //Needed to Use ADS1115 ADC - See Reference 12
- ADS1115_lite ads(ADS1115_DEFAULT_ADDRESS); //0x48 addr pin connected to GND. Needed to Use ADS1115 ADC - See Reference 12
- long raw1 = 0, raw2 = 0; //Needed to Use ADS1115 ADC - See Reference 12
- double resolution=0; //Needed to Use ADS1115 ADC - See Reference 12
- int batt1Analog = 0, batt2Analog=0; //Analog readings from batt1Pin and batt2Pin and then actual battery voltages
- float batt1V=0, batt2V=0;
- unsigned long previousMillis = 0; // will store last time the battery voltages were read
- const long interval = 10000; // interval at which to read the battery voltage (milliseconds)
- #define battCutOff 3.2 //Voltage to Cutoff Motors to Protect the Batteries
- void setup()
- { //start of void setup()
- ads_config(); //Needed to Use ADS1115 ADC - See Reference 12
- } //end of void setup()
- void loop()
- { //start of void loop()
- 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.
- if (currentMillis - previousMillis >= interval)
- { //Start of if (currentMillis - previousMillis >= interval)
- previousMillis = currentMillis; //save the last time you read the battery voltages before you read the battery voltages
- readBatts(); //Read the battery voltages
- } //End of if (currentMillis - previousMillis >= interval)
- } //end of void loop()
- void readBatts()
- { //Start of readBatts function definition
- raw1 = ads_read1(); //Reads A0 from ADS1115 - Reference 12
- resolution=4096.00/32752.00;
- delay(200);
- float result1=(raw1*resolution)/1000.00; //batt1 voltage + batt2 voltage
- raw2 = ads_read2(); //Reads A0 from ADS1115 - Reference 12
- delay(200);
- float result2=(raw2*resolution)/1000.00; //batt2 voltage alone
- batt1V = result1 - result2; //Assigns battery 1 voltage alone to batt1V variable
- batt1V = 11.03*batt1V;
- batt2V = result2; //Assigns result2 to the batt2V variable
- batt2V = 11.0*batt2V;
- //This section of code sends the battery voltages to the Android app
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("W"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print(batt1V);
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("w"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("Z"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print(batt2V);
- SerialBT.print("|"); //Delimeter separating values which the receiving app is programmed to recognize
- SerialBT.print("z"); //Delimeter separating values which the receiving app is programmed to recognize
- if (batt1V < battCutOff || batt2V < battCutOff) //If either battery drops below the cutoff voltage
- { //Start of if (batt1V < battCutOff || batt2V < battCutOff)
- stopVehicle(); //Stop Power to the Motors
- lightFState = 0; //Turns Off Front Lights
- lightBState = 0; //Turns Off Back Lights
- hornH(); //Turns horn on briefly
- delay(100);
- hornL();
- } //End of if (batt1V < battCutOff || batt2V < battCutOff)
- } //End of readBatts function definition
- void ads_config(){
- ads.setGain(ADS1115_REG_CONFIG_PGA_4_096V); // GAIN_ONE and resolution to ± 4.096V
- ads.setSampleRate(ADS1115_REG_CONFIG_DR_8SPS); // Set to the fastest MODE 860Samples per sec
- }
- int16_t ads_read1(){
- ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_0); // Single mode input on AIN0 (AIN0 - GND)
- ads.triggerConversion(); // Triggered mannually
- return ads.getConversion(); // returns int16_t value
- }
- int16_t ads_read2(){
- ads.setMux(ADS1115_REG_CONFIG_MUX_SINGLE_1); // Single mode input on AIN1 (AIN1 - GND)
- ads.triggerConversion(); // Triggered mannually
- return ads.getConversion(); // returns int16_t value
- }
- //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