//Record time as long as button is pressed + add it
#include <arduino-timer.h>
#include <LiquidCrystal_I2C.h>
struct Tstruct {
//bool buttonPrevState = 0;
bool state;
//bool buttonReleased = 0;
unsigned long seconds;
unsigned long minutes;
unsigned long hours;
unsigned long days;
unsigned long buttonPressedAt = 0;
};
struct Tstruct TimerTest; // make struct TimerTest
Timer<2, millis, const char *> b_timer;
Timer<2, millis, const char *> butt_timer;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
TimerTest.state = 0; // is button pressed
TimerTest.seconds = 0;
TimerTest.minutes = 0;
TimerTest.hours = 0;
TimerTest.days = 0;
TimerTest.buttonPressedAt = 0;
b_timer.every(2000, PrintTime);
butt_timer.every(100, read_butt);
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
pinMode(2, INPUT_PULLUP); // set pin 2 input - Filter tank low
}
void loop() {
// put your main code here, to run repeatedly:
b_timer.tick();
butt_timer.tick();
Serial.print("butt_timer.expires: ");Serial.println(butt_timer.ticks());
}
bool read_butt(void *) {
TimerTest.state = ReadDigitalButton(2);
/*
if (foo3 == true){
Serial.print("FOO FTH: ");
Serial.println(foo3);
Seconds(0); // Reset seconds reading
}
*/
CheckTime(); // Checks the TimerTest.state and adds time if pressed
return true;
}
void CheckTime(){
static unsigned long SumOfTime = 0; // this is persistent
Serial.println("in timertest");
unsigned long Mn = millis();
if (TimerTest.state == 1){ // Button is pressed
SumOfTime = SumOfTime + Mn - TimerTest.buttonPressedAt;
TimerTest.buttonPressedAt = Mn;
}
if (TimerTest.state == 0){ // Button is not pressed
TimerTest.buttonPressedAt = Mn;
}
counttime(SumOfTime);
return true;
}
void counttime(unsigned int TimeSinceStart){
unsigned long seconds = TimeSinceStart / 1000UL;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
unsigned long days = hours / 24;
seconds %= 60;
minutes %= 60;
hours %= 24;
TimerTest.seconds = seconds;
TimerTest.minutes = minutes;
TimerTest.hours = hours;
TimerTest.days = days;
}
void PrintTime(void *){
char buffer[30];
sprintf (buffer, "%03lu %02lu:%02lu:%02lu",TimerTest.days, TimerTest.hours, TimerTest.minutes, TimerTest.seconds);
Serial.println(buffer);
//Serial.println(buttonPressedAt);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(buffer);
//lcd.setCursor(0,1);
//lcd.print(S2);
return true;
}
bool ReadDigitalButton(int dport){
bool foo = !digitalRead(dport); //
Serial.print(" Output: ");Serial.println(foo);
return foo;
}