#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define YES_PIN 2
#define WAIT_PIN 3
int TIMER_PIN[] = { 4, 5, 6, 7 };
#define RESET_PIN 8
#define FORGET_PIN 9
#define PLUS_PERIOD_PIN 10
#define MINUS_PERIOD_PIN 11
#define FULL_PERIOD 1000L
#define PERIOD 1000L
// #define PERIOD (FULL_PERIOD / 3600.0)
#define ONE_HOUR (1L * 60 * 60 * PERIOD)
#define TOTAL_WAIT (24 * ONE_HOUR)
#define FF_STEP TOTAL_WAIT / 100.0 // 1%
unsigned long waitRound;
unsigned long startTime;
void setup() {
startTime = millis();
waitRound = TOTAL_WAIT;
//pinMode(ledPins[i], OUTPUT);
pinMode(RESET_PIN, INPUT_PULLUP);
pinMode(FORGET_PIN, INPUT_PULLUP);
pinMode(PLUS_PERIOD_PIN, INPUT_PULLUP);
pinMode(MINUS_PERIOD_PIN, INPUT_PULLUP);
// Init
lcd.init();
lcd.backlight();
}
void loop() {
unsigned long now = millis();
unsigned long elapsedMs = now - startTime;
unsigned long elapsedSec = elapsedMs / 1000;
long remainMs = waitRound - elapsedMs;
long remainHours = remainMs / ONE_HOUR;
bool take = remainMs < 0;
int percRemain = 100.0 * remainMs / waitRound;
digitalWrite(YES_PIN, take ? HIGH : LOW);
digitalWrite(WAIT_PIN, !take ? HIGH : LOW);
digitalWrite(TIMER_PIN[0], percRemain > 0 ? HIGH : LOW);
digitalWrite(TIMER_PIN[1], percRemain > 25 ? HIGH: LOW);
digitalWrite(TIMER_PIN[2], percRemain > 50 ? HIGH : LOW);
digitalWrite(TIMER_PIN[3], percRemain > 75 ? HIGH : LOW);
if (elapsedSec % 2 == 0) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
} else {
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
if (digitalRead(RESET_PIN) == LOW) {
startTime = millis();
}
if (digitalRead(FORGET_PIN) == LOW) {
startTime = millis() - waitRound;
}
if (digitalRead(PLUS_PERIOD_PIN) == LOW) {
startTime += FF_STEP;
}
if (digitalRead(MINUS_PERIOD_PIN) == LOW) {
startTime -= FF_STEP;
}
char buf[64];
sprintf(buf, "Cycle : %12ld", waitRound);
lcd.setCursor(0, 0);
lcd.print(buf);
sprintf(buf, "Remain: %12ld", remainMs);
lcd.setCursor(0, 1);
lcd.print(buf);
sprintf(buf, "Elapse: %12ld", elapsedMs);
lcd.setCursor(0, 2);
// lcd.print(sprintf("%d", 12345));
lcd.print(buf);
sprintf(buf, "Cntdwn: %12ld", remainHours);
lcd.setCursor(0, 3);
lcd.print(buf);
}