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.0 //Voltage to Cutoff Motors to Protect the Batteries
- #define numReadsToAvg 3 //Number of Voltage Readings to Store and Take Average Of
- float batt1VArray[numReadsToAvg]; //Array to store batt1V readings i.e. battery 1 voltages
- float batt2VArray[numReadsToAvg]; //Array to store batt2V readings i.e. battery 2 voltages
- int batt1VIndex = 0, batt2VIndex = 0; //Keeps track of the array indexes for the batt1VArray and batt2VArray
- float batt1VAvg = 3.0, batt2VAvg = 3.0; //Initialize battery voltage reading averages to 3.0
- float batt1VSum = 0.0, batt2VSum = 0.0; //Sums all the elements in the batt1VArray and the batt2VArray for use in the average calculations
- 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
- void setup()
- { //start of void setup()
- ads_config(); //Needed to Use ADS1115 ADC - See Reference 12
- //Initialize elements of batt1VArray and batt2VArray to 3.5
- for(int m = 0; m < numReadsToAvg; m++)
- { //Start of for loop to initialize arrays to 3.5
- batt1VArray[m] = 3.5;
- batt2VArray[m] = 3.5;
- } //End of for loop to initialize arrays to 3.5
- } //end of void setup()
- void loop()
- { //start of void loop()
- unsigned long currentMillis = millis(); //Checks the current time
- 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)
- { //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;
- batt1VArray[batt1VIndex] = batt1V; //Store the current batt1V value into the element of the batt1VArray that matches batt1VIndex
- batt2VArray[batt2VIndex] = batt2V; //Store the current batt2V value into the element of the batt2VArray that matches batt2VIndex
- 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)
- 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)
- //Perform the average calculation with non-zero elements of the batt1VArray and batt2VArray
- for(int i=0; i<numReadsToAvg; i++)
- { //Start of for loop to step through the elements in the batt1VArray
- batt1VSum += batt1VArray[i]; //Add the current value to the running sum for the average calculation
- } //end of for loop to step through the elements in the batt1VArray
- batt1VAvg = batt1VSum / numReadsToAvg; //Calculates the average
- //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
- 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
- for(int j=0; j<numReadsToAvg; j++)
- { //Start of for loop to step through the elements in the batt2VArray
- batt2VSum += batt2VArray[j]; //Add the current value to the running sum for the average calculation
- } //end of for loop to step through the elements in the batt2VArray
- batt2VAvg = batt2VSum / numReadsToAvg; //Calculates the average
- //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
- 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
- //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(batt1VAvg);
- 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(batt2VAvg);
- 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
- if (batt1VAvg < battCutOff || batt2VAvg < 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