//http://www.youtube.com/user/greatscottlab //rev 3/26/2017 if statements change text sent to android based number of digits in voltage less than 10 100 or 1000 //rev 3/22/2107 debug circuit //Rev 3/12/2017 - Add 2nd Relay on Pin 8 to isolate Batt 1 from Batt 2 measurements ///rev 3/23/17 using smaller resistors 10K and 330 to do voltage split //rev 3/25/17 calibrated for circuit board 2 //Rev 3/8/2017 by Kraig Schultz // //Send voltage back as soon as connected via bluetooth //Change to first connection to read Connected to ReVolt Module via BlueTooth //Make Main LED red until bluetooth connection made //add reed relay on pin 12 - relay always on when power on to arduino //Using Common cathode LED that reguires output LOW to turn on LED //Remember to disconnect Blue Tooth Module to program chip int ledred=9; int ledblue=10; int ledgreen=11; int Relay1=8; //Pin to turn on Relay 1 int Relay2=12; //Pin to turn on Relay 2 int tx=1; int rx=0; char inSerial[15]; int sensePin4 = 4; // Analog pin to read 0-5volts representing battery 0-120v Battery 2 int sensePin5 = 5; // Analog pin to read 0-5volts representing battery 0-120v Battery 1 float pack1voltage=0.0; //pack1voltage is the voltage of pack one float pack2voltage=0.0; //pack2voltage is the voltage of pack two void setup(){ Serial.begin(9600); pinMode(ledred, OUTPUT); pinMode(ledblue, OUTPUT); pinMode(ledgreen, OUTPUT); pinMode(Relay1, OUTPUT); pinMode(Relay2, OUTPUT); pinMode(tx, OUTPUT); pinMode(rx, INPUT); pack1voltage = (100); pack2voltage = (100); //digitalWrite(rx, HIGH); //set RX pin high to start so that floating voltage levels can be tamed? This is work around for MEGA with HC-06 with pull down resistor that floats wout being set //http://stackoverflow.com/questions/23122294/arduino-mega-receiving-correct-data-through-serial-0-but-not-serial-1-3 allpinshigh(); digitalWrite(ledred, LOW); //Turn on red LED until bluetooth connection is made //digitalWrite(Relay1, HIGH); //Turn on Relay contact, commented out 3/12/17 //digitalWrite(Relay1, HIGH); //Turn on Relay1 contact, commented out 3/13 //digitalWrite(Relay2, HIGH); //Turn on Relay2 contact, commented out 3/13 } void loop(){ int i=0; int m=0; delay(500); if (Serial.available() > 0) { while (Serial.available() > 0) { inSerial[i]=Serial.read(); i++; } inSerial[i]='\0'; Check_Protocol(inSerial); }} void allpinshigh() //For common Anode LED, set output high to turn off LED { digitalWrite(ledred, HIGH); digitalWrite(ledgreen, HIGH); digitalWrite(ledblue, HIGH); } void checkvoltages() //sub program to check voltages { //Get Pack 1 Voltage Stored in Variable digitalWrite(Relay1, HIGH); //Turn on Relay1 contact delay(200); // waits x miliseconds to stabilize voltage readings //pack1voltage = ((analogRead(sensePin5))*.1521); pack1voltage = (pack1voltage + .1); digitalWrite(Relay1, LOW); //Turn off Relay1 contact //Get Pack 2 Voltage Stored in Variable digitalWrite(Relay2, HIGH); //Turn on Relay2 contact delay(200); // waits x miliseconds to stabilize voltage readings //pack2voltage = ((analogRead(sensePin4))*.1533); pack2voltage = (pack2voltage + .2); digitalWrite(Relay2, LOW); //Turn off Relay2 contact } void Check_Protocol(char inStr[]){ int i=0; int m=0; // Serial.println(inStr); prints extra line with the color on it if(!strcmp(inStr,"red")){ //Ledred ON allpinshigh(); digitalWrite(ledred, LOW); Serial.println("Red ON yes"); for(m=0;m<11;m++){ inStr[m]=0;} i=0;} if(!strcmp(inStr,"green")){ //Ledgreen ON allpinshigh(); digitalWrite(ledgreen, LOW); Serial.println("Green ON yes"); for(m=0;m<11;m++){ inStr[m]=0;} i=0;} if(!strcmp(inStr,"blue")){ //Ledblue ON allpinshigh(); digitalWrite(ledblue, LOW); Serial.println("Blue ON yes"); for(m=0;m<11;m++){ inStr[m]=0;} i=0;} if(!strcmp(inStr,"connected")){ //BT connection made to ReVolt App rev 3-2-17 allpinshigh(); digitalWrite(ledblue, LOW); //Serial.print((analogRead(sensePin4))*.1573); Serial.println ("0.0 Volts Pack 1"); //Serial.print((analogRead(sensePin5))*.1573); Serial.println ("0.0 Volts Pack 2"); Serial.println("ReVolt Module is Connected"); for(m=0;m<11;m++){ inStr[m]=0;} i=0;} if(!strcmp(inStr,"volt")){ //Ledblue ON //---allpinshigh(); //---digitalWrite(ledgreen, LOW); //turn on green LED while reading voltage //---digitalWrite(Relay1, HIGH); //Turn on Relay contact //---delay(2000); // waits x miliseconds to stabilize voltage readings // print voltage to LCD by kds 1-29-17 //normally 0-5vots = 0-1023 bits //and 1024 bits / 5 = 204.8 bits per volt //my voltage transducer is 0-150 volts = 0-5 volts so must multiply x 30 //and 30 / 204.8 = .1465 //2-23-17 CALIBRATION //correction factor for sensePin4=A4 = Yellow Blue = (.804 x .1478)=.1188 //correction factor for sensePin5=A5 = Black Green = (.801 x .1478)=.1184 //3-23-17 CALIBRATION //correction factor for sensePin4=A4 = Yellow Blue = (.804 x .1478)=.1188/.75=.1573 //correction factor for sensePin5=A5 = Black Green = (.801 x .1478)=.1184/.75=.1573 // Serial.println ("Going off to check voltages"); allpinshigh(); //turn off LEDs digitalWrite(ledgreen, LOW); //turn on green LED while reading voltage checkvoltages(); //run sub program to get voltages allpinshigh(); //turn off LEDs digitalWrite(ledblue, LOW); //turn blue light back on after checking voltages // Serial.println ("Sending info to Android with Bluetooth"); if (-1.0 < pack1voltage && pack1voltage < 10.0) {Serial.print (pack1voltage,1); // send one decial place Serial.println (" Volts Pack 1"); } if (10.0 < pack1voltage && pack1voltage < 99.99) {Serial.print (pack1voltage,1); // send one decial place Serial.println (" Volts Pack 1"); } if (99.99 < pack1voltage && pack1voltage < 1000.0) {Serial.print (pack1voltage,1); // send one decial place Serial.println (" Volts Pack 1"); } //Serial.print((analogRead(sensePin5))*.1184); if (-1.0 < pack2voltage && pack2voltage < 10.0) {Serial.print (pack2voltage,1); // send one decial place Serial.println (" Volts Pack 2"); } if (10.0 < pack2voltage && pack2voltage < 99.99) {Serial.print (pack2voltage,1); // send one decial place Serial.println (" Volts Pack 2"); } if (99.99 < pack2voltage && pack2voltage < 1000.0) {Serial.print (pack2voltage,1); // send one decial place Serial.println (" Volts Pack 2"); } //---delay(2000); // waits x miliseconds to for serial port to finish //---digitalWrite(Relay1, LOW); //Turn off Relay contact //---allpinshigh(); //turn off leds //---digitalWrite(ledblue, LOW); //go back to Blue LED for(m=0;m<11;m++){ inStr[m]=0;} i=0;} else{ for(m=0;m<11;m++){ inStr[m]=0; } i=0; }}