#include "Wire.h" // Library for I2C communication
#include "LiquidCrystal_I2C.h" // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,20,4) for 20x4 LCD.
int period = 100; //time between increments- currently set to tenths of a second
float secondCounter = 0; //counts number of times the pump comes on- not currently implemented
int floatSwitchPin = 7; //which pin the float switch is connected to
boolean pumpOn = false; // State of the pump- starts as "off" (False) and switches to "on" (True when pin7 is shorted to ground)
boolean lastPumpState = false;
float onTimeAccumulator = 3599; // total amount of time the pump is on
float offTimeAccumulator = 3599; // total amount of time the pump is off
void setup() {
//start LCD screen
lcd.init();
lcd.backlight();
// Initialize sensor pin
pinMode(floatSwitchPin, INPUT_PULLUP);
// Open Serial monitor for debugging
Serial.begin(9600);
}
void loop() {
unsigned long time_now = millis();
if (pumpOn == true) { // Check to see if the pump is on
onTimeAccumulator = onTimeAccumulator + 0.1; //increase the onTimeAccumulator by 0.1 seconds each loop when pump is on
}
else { //pump is off
offTimeAccumulator = offTimeAccumulator + 0.1; //increase the offTimeAcccumulator by 0.1 seconds each loop when pump is off
}
//Debugging Serial Print Statements
printTime(0, 0, onTimeAccumulator);
printTime(0, 1, offTimeAccumulator);
/* Serial.print("onTimeAccumulator=");
Serial.print(onTimeAccumulator);
Serial.print(" offTimeAccumulator=");
Serial.print(offTimeAccumulator);
Serial.print(" pumpOn=");
Serial.println(pumpOn);
*/
while (millis() < time_now + period) { //non blocking delay
pumpOn = !digitalRead(floatSwitchPin);
}
}
void printTime(int x, int y, float seconds) { //Convert Seconds into user friendly display of HH:MM:SS.T
if (secondsToHours(seconds) > 99) {
x = x + 1; //If Seconds is bigger than 99 move over one column
}
lcd.setCursor(x, y); // Set col 0
if (secondsToHours(seconds) < 10) { //if hours is less than 10 add a leading 0
lcd.print("0"); //After print move to col 1
//lcd.setCursor(x + 1, y); //move over one column Set col 1 (unnecessary)
}
lcd.print(secondsToHours(seconds)); //print number of hours (using subroutine below)
lcd.setCursor(x + 2, y); //move over 2 columns // Set col 2
lcd.print(":"); //print the colon //After print move to col 3
//lcd.setCursor(x + 3, y); //move over 3 columns Set col 3 (unnecessary)
if (secondsToMinutes(seconds) - (secondsToHours(seconds) * 60) < 10) { //if number of minutes is less than 10 add a deading 0
lcd.print("0"); //After print move to col 4
// lcd.setCursor(x + 4, y); //move over Set col 4 (unnecessary)
}
lcd.print(secondsToMinutes(seconds) - (secondsToHours(seconds) * 60)); //print number of minutes (minus minutes accounted for as hours)
lcd.setCursor(x + 5, y); //position cursor Set col 5
lcd.print(":"); //print colon //After print move to col 6
//lcd.setCursor(x + 6, y); // position cursor Set col 6 (unnecessary)
// I don't understand what should happen with this part.
// I have modify to avoid neg. numbers
// ==========================================================================
/*
if (secondCounter - ((secondsToMinutes(seconds) * 60) + secondsToHours(seconds) * 3600) < 10) { //if number of seconds (minus seconds accounted for as minutes and as hours) is less than 10 add a leading zero
lcd.print("0"); //add a leading zeor
lcd.setCursor(x + 7, y); //move the cursor over
}
lcd.print(secondCounter - (((secondsToMinutes(seconds) * 60) + secondsToHours(seconds) * 3600)), 1); //print number of seconds (minus seconds accounted for as hours and minutes)
*/
// ==========================================================================
float newSecondCounter = secondCounter - ((secondsToMinutes(seconds) * 60) + secondsToHours(seconds) * 3600);
if (newSecondCounter < 0 )
newSecondCounter = newSecondCounter * -1;
if (newSecondCounter < 10) { //if number of seconds (minus seconds accounted for as minutes and as hours) is less than 10 add a leading zero
lcd.print("0"); //add a leading zeor //After print move to col 7
// lcd.setCursor(x + 7, y); //move the cursor over Set col 7 (unnecessary)
}
lcd.setCursor(x + 6, y); //move the cursor over
lcd.print(newSecondCounter, 1); //print number of seconds (minus seconds accounted for as hours and minutes)
// ===========================================================================
}
int secondsToMinutes(float seconds) { //converts seconds into minutes
int minutes;
minutes = floor(seconds / 60);
return (minutes);
}
int secondsToHours(float seconds) { //converts seconds into hours
int hours;
hours = floor(seconds / 3600);
return (hours);
}