/****************************************************************************** Tank Level Indicator (TLI) app by Chet Myrick Credits: Thanks to David Dixon for doing the preliminary setup for this app and getting a bunch of us started at the East Coast Heavy Duty Truck Rally in Crossville, TN. The serial graphic library was courtesy of Sparkfun. This is a Tank Level Indicator for 4 tanks on my RV. The tank levels will be displayed on a 128 x 64 SparkFun Serial Graphic Display which can be found at (https://www.sparkfun.com/products/9351). A tank level alarm is incorporated to sound off when the waste tanks are getting full and when the fresh water tank is either nearing full (to assist in filling) or empty. A relay is used to send 12V DC to a sonalert. Warning: If you just copy/paste this app into the Particle IDE, it will fail verification. You must copy it into the IDE and then comment out the #include statement. Then go to the built-in Library and find the SPARKFUN_SERIAL_GRAPHIC_LCD library and INCLUDE IN APP. DO NOT select the SERIAL_LCD_SPARKFUN library, it will not work with this LCD. *******************************************************************************/ // This #include statement was automatically added by the Particle IDE. #include "SparkFun_Serial_Graphic_LCD/SparkFun_Serial_Graphic_LCD.h" #define maxX 127 #define maxY 63 //Each maximum value is one less than the stated value to account for position 0,0 //Thus, position 127 is actually the 128th pixel. //Create an instance of the LCD class named LCD. We will use this instance to call all the //subsequent LCD functions LCD LCD; //Establish some variables int tnklvlalarm = D0; //Sounds alarm at 85% full (waste) or 20% (fresh) and 95% (fresh) //Bathroom Tank int bath = A0; int bathempty = 003; int bathfull = 4092; int bathdiff = bathfull - bathempty; //Precalculate difference between bath empty and full values //Kitchen Tank int kit = A1; int kitempty = 003; int kitfull = 4092; int kitdiff = kitfull - kitempty; //Precalculate difference between kit empty and full values //Black Tank int black = A2; int blackempty = 003; int blackfull =4092; int blackdiff = blackfull - blackempty; //Precalculate difference between black empty and full values //Fresh water tank int fresh = A3; //Fresh water TLI input. int freshempty = 003; //The 12 bit message from the sensor when empty int freshfull = 4092; //The 12-bit message from the sensor when full as seen by the Photon. int freshdiff = freshfull - freshempty; //Precalculate difference between tank empty and full values //String variable that will store the text uploaded to Particle servers. char resultstr[64]; //------------------------------------------------------------------------------------------- void setup() { pinMode(tnklvlalarm, OUTPUT); pinMode(bath, INPUT); // setup A0 as analog input for bath gray water tank pinMode(kit, INPUT); // setup A1 as analog input for kitchen gray water tank pinMode(black, INPUT); // setup A2 as analog input for black tank pinMode(fresh, INPUT); // setup A3 as analog input for fresh water tank Serial1.begin(115200); //Set the baud rate for the LCD serial connection. delay(1200);///wait for the one second spalsh screen before anything is sent to the LCD. LCD.setHome();//set the cursor back to 0,0. LCD.clearScreen();//clear anything that may have been previously printed to the screen. } //------------------------------------------------------------------------------------------- void loop() { int data1 = analogRead(bath); //data1 is what the current bath gray tank level is reading. double bathpctfull = float(data1 - bathempty)/float(bathdiff)*100.0; //Calculate the percent full by using values established above. float bathtnklevel = data1 - bathempty; //Calculate difference between the voltage the sensor sees vs. the voltage when the tank is empty. int data2 = analogRead(kit); //data2 is what the current kit gray tank level is reading. double kitpctfull = float(data2 - kitempty)/float(kitdiff)*100.0; //Calculate the percent full by using values established above. float kittnklevel = data2 - kitempty; //Calculate difference between the voltage the sensor sees vs. the voltage when the tank is empty. int data3 = analogRead(black); //data3 is what the current black gray tank level is reading. double blackpctfull = float(data3 - blackempty)/float(blackdiff)*100.0; //Calculate the percent full by using values established above. float blacktnklevel = data3 - blackempty; //Calculate difference between the voltage the sensor sees vs. the voltage when the tank is empty. int data4 = analogRead(fresh); //data4 is what the current fresh water tank level is reading. double freshpctfull = float(data4 - freshempty)/float(freshdiff)*100.0; //Calculate the percent full by using values established above. float freshtnklevel = data4 - freshempty; //Calculate difference between the voltage the sensor sees vs. the voltage when the tank is empty. float bathGray = bathpctfull; float kitGray = kitpctfull; float blackT = blackpctfull; float freshT = freshpctfull; //The following statements are used to read what the pressure sensor is sending to the Photon. //The values will be displayed on the LCD at the bottom. //They should be commented out after calibration is complete. float bathData = data1; float kitData = data2; float blackData = data3; float freshData = data4; //The following routine runs to see if the gray and black tanks are at or above 85% full //and if the fresh water tank is either less than 20% full or over 95% full. if (bathpctfull > 85) // bath gray is > 85% full { digitalWrite(tnklvlalarm, HIGH); delay(120); digitalWrite(tnklvlalarm, LOW); delay(1200); } else if (kitpctfull > 85) // kitchen gray is > 85% full { digitalWrite(tnklvlalarm, HIGH); delay(1200); digitalWrite(tnklvlalarm, LOW); delay(1200); } else if (blackpctfull > 85) // black tank is > 85% full { digitalWrite(tnklvlalarm, HIGH); delay(1200); digitalWrite(tnklvlalarm, LOW); delay(1200); } else if (freshpctfull < 20 || freshpctfull > 95) // fresh to alarm if it has less than 20% or more than 95% { digitalWrite(tnklvlalarm, HIGH); delay(1200); digitalWrite(tnklvlalarm, LOW); delay(1200); } //The following routine will display the tank status on the LCD screen. LCD.clearScreen(); Serial1.println("Tank Level Indicator"); delay(200); //Display the Bathroom Gray Tank level. Serial1.print("Bathroom Gray = "); Serial1.print(int(bathGray)); Serial1.println("%"); delay(150); //Display the Kitchen Gray Tank. Serial1.print("Kitchen Gray = "); Serial1.print(int(kitGray)); Serial1.println("%"); delay(150); //Display the Black Tank. Serial1.print("Black Tank = "); Serial1.print(int(blackT)); Serial1.println("%"); delay(150); //Display the Fresh Water Tank. Serial1.print("Fresh Water = "); Serial1.print(int(freshT)); Serial1.println("%"); Serial1.println(); delay(150); //These entries are only to permit calibration. They will read the input coming from the pressure sensors //for each tank and display the data on the bottom two lines of the LCD screen. //This section can be commented out after calibration. Serial1.print("Bath:"); Serial1.print(int(bathData)); delay(100); Serial1.print(" Kit:"); Serial1.println(int(kitData)); delay(100); Serial1.print("Blk:"); Serial1.print(int(blackData)); delay(100); Serial1.print(" Fresh:"); Serial1.print(int(freshData)); //Leave this delay in if/when the above is commented out. delay(2500); //And do it again! }