# include <Wire.h>
# include <LiquidCrystal_I2C.h>
# include <TimeLib.h>
# define BUTTON_PIN A0
# define BUTTON1_PIN A0
# define BUTTON2_PIN A1
# define BUTTON3_PIN A2
# define NO_BUTTON 0
# define BUTTON1 1
# define BUTTON2 2
# define BUTTON3 3
# define DELAY_TIME 100
# define STARTING " starting ... "
# define TIME_OFFSET 1672531200
# define TIME_LINE 0
# define TIME_MAX_DELAY 10
void setup_time();
void capture_button_click();
void capture_button_click_v2();
void handle_button_clicked();
void handle_no_action_since_last_long();
void get_current_time();
void display_current_time();
void detect_action();
LiquidCrystal_I2C lcd (0x27, 16, 2);
time_t current_time = 0;
time_t action_time = 0;
bool time_has_moved = false;
bool screen_is_up = false;
unsigned short button_pressed = 0;
unsigned short button_clicked = 0;
bool action_detected = false;
//const char *ALL_MENU[] = {};
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.display();
lcd.clear();
lcd.print(STARTING);
setup_time();
Serial.begin(9600);
handle_no_action_since_last_long();
}
void setup_time(){
setTime(TIME_OFFSET);
}
void loop()
{
get_current_time();
capture_button_click_v2();
if (button_clicked != NO_BUTTON) handle_button_clicked();
if (screen_is_up){
if (action_detected && time_has_moved) {
display_current_time();
}
else if (! action_detected) {
handle_no_action_since_last_long();
}
}
delay(DELAY_TIME);
}
void capture_button_click(){
int temp = analogRead(BUTTON_PIN);
if (temp < 200){
if (button_pressed == NO_BUTTON){
button_clicked = NO_BUTTON;
}
else if (button_clicked == NO_BUTTON){
button_clicked = button_pressed;
}
button_pressed = NO_BUTTON;
}
else if (temp < 400)
button_pressed = BUTTON1;
else if (temp < 1000)
button_pressed = BUTTON2;
else
button_pressed = BUTTON3;
Serial.println(button_clicked);
}
void capture_button_click_v2(){
if (analogRead(BUTTON1_PIN) == 0)
button_pressed = BUTTON1;
else if (analogRead(BUTTON2_PIN) == 0)
button_pressed = BUTTON2;
else if (analogRead(BUTTON3_PIN) == 0)
button_pressed = BUTTON3;
else {
if (button_pressed == NO_BUTTON){
button_clicked = NO_BUTTON;
}
else if (button_clicked == NO_BUTTON){
button_clicked = button_pressed;
}
button_pressed = NO_BUTTON;
}
detect_action();
}
void detect_action(){
if (button_clicked != NO_BUTTON){
action_time = current_time;
action_detected = true;
}
else if (action_detected && ((current_time - TIME_MAX_DELAY) > action_time)){
action_detected = false;
}
}
void handle_button_clicked(){
screen_is_up = true;
}
void handle_no_action_since_last_long(){
lcd.clear();
lcd.noBacklight();
screen_is_up = false;
}
void get_current_time(){
time_t t = now();
if (t != current_time ){
current_time = t;
time_has_moved = true;
}
else
time_has_moved = false;
}
void display_current_time(){
char buffer[16];
int h = hour(current_time);
int m = minute(current_time);
int s = second(current_time);
int w = weekday(current_time);
int D = day(current_time);
int M = month(current_time);
sprintf(buffer, "%02d/%02d %02d:%02d:%02d", D, M, h, m, s);
lcd.setCursor(0, TIME_LINE);
lcd.print(buffer);
}