#include <arduino-timer.h>
#include <LiquidCrystal_I2C.h>
Timer<16, millis, const char *> t_timer;
Timer<16, millis, const char *> b_timer;
Timer<16, millis, const bool *> Sec_timer;
Timer<16, millis, const bool *> UpdateLCD;
struct TimerS {
float LastTimer = 1;
float DayTimer = 0;
float Sec = 0;
float FilterTime = 0;
};
float LastTimer = 0;
float DayTimer = 0;
float Sec = 0;
float FilterTime = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
struct TimerS TimerTest;
int LCDloop = 0;
String S1 = "";
String S2 = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
pinMode(2, INPUT_PULLUP); // set pin 2 input - Filter tank low
pinMode(3, INPUT_PULLUP); // set pin 3 input - Filter tank high
t_timer.every(500, toggle_led); // Blink
b_timer.every(100, read_butt);
Sec_timer.every(1000, Seconds,1);
UpdateLCD.every(2000, PrintLCD);
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
}
void loop() {
// put your main code here, to run repeatedly:
t_timer.tick();
b_timer.tick();
Sec_timer.tick();
UpdateLCD.tick();
}
bool toggle_led(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // repeat? true
}
bool read_butt(void *) {
bool foo2 = ReadDigitalButton(2);
bool foo3 = ReadDigitalButton(3);
if (foo2 == true){
Serial.print("FOO FTL: ");
Serial.println(foo2);
FilterTime = Sec;
}
if (foo3 == true){
Serial.print("FOO FTH: ");
Serial.println(foo3);
Seconds(0); // Reset seconds reading
}
return true;
}
bool Seconds(bool Do) {
if (Do == true) {
long Mnow = millis();
long Slast = (Mnow -LastTimer)/1000L;
Sec = Sec + Slast;
LastTimer = Mnow; // Global
DayTimer = DayTimer + Slast;
}
if (Do == false) {
Sec = 0;
}
if (DayTimer == 24*60*60L){
// Savefunction here
DayTimer = 0;
}
//Serial.print(LastTimer, 3);
//Serial.print(" ");
//Serial.print("DayTimer: ");Serial.println(DayTimer);
//Serial.print("Sec: ");Serial.println(Sec, 3);
//Serial.print("FilterTime: ");Serial.println(FilterTime, 3);
//displayDetail(TimerTest);
return true; // repeat? true
}
void displayDetail(struct TimerS foo){
Serial.print("LasttrImerstruct: ");Serial.println(foo.LastTimer, 3);
}
bool ReadDigitalButton(int dport){
bool foo = !digitalRead(dport); //
if (foo == true){
Serial.print("FOO @ port " );Serial.print(dport);Serial.print(" Output: ");Serial.println(foo);
}
return foo;
}
void PrintLCD(void *){
switch (LCDloop) {
case 0:
S1 = "DayTimer: " + String(DayTimer,2);
S2 = "Sec: " + String(Sec,2);
LCDloop = LCDloop + 1;
break;
case 1:
S1 = "FilterT : " + String(FilterTime,2);
S2 = "Foo12: " + String(0,2);
LCDloop = LCDloop + 1;
break;
case 2:
S1 = "Foo21: " + String(0,2);
S2 = "Foo22: " + String(0,2);
LCDloop = 0;
break;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(S1);
lcd.setCursor(0,1);
lcd.print(S2);
return true;
}