#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
byte sec, minu, h, d;
int upButton = 9;
int downButton = 8;
int selectButton = 7;
unsigned long rtc_start;
unsigned long rtc_current;
unsigned long rtc_interval = 1000;
unsigned long m_start;
unsigned long m_current;
unsigned long m_interval = 200;
void setup() {
lcd.begin(20, 4);
lcd.backlight();
// lcd.createChar(0, select);
// lcd.createChar(1, nichts);
//lcd.createChar(1, back);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
// pinMode(13, OUTPUT);
rtc_start = millis();
}
void loop() {
//rtc();
setTime();
}
void setTime(){
m_current = millis();
if (!digitalRead(upButton) && h < 24){
if (m_current - m_start >= m_interval){
m_start = m_current;
minu = minu + 5;
}
}
if (!digitalRead(downButton) && minu > 0){
if (m_current - m_start >= m_interval){
m_start = m_current;
minu = minu - 5;
}
}
if (!digitalRead(downButton) && minu == 0 && h >= 1){
if (m_current - m_start >= m_interval){
m_start = m_current;
minu = 55;
h = h - 1;
}
}
if(minu >= 60){
if(h < 24){
h = h + 1;
minu = 0;
}
else
{
h = 24;
}
}
afficheDecale(13, 1, sec);
afficheDecale(10, 1, minu);
afficheDecale(7, 1, h);
afficheDecale(4, 1, d);
////Separation sec, min, heure et jour
afficheText(6, 1, "/" );
afficheText(9, 1, "/" );
afficheText(12, 1, "/" );
}
///////////////
void rtc(){
rtc_current = millis();
if (rtc_current - rtc_start >= rtc_interval){
rtc_start += rtc_interval;
sec = sec + 1;
}
if(sec >= 60){
minu = minu + 1;
sec = 0;
}
if(minu >= 60){
h = h + 1;
minu = 0;
}
if(h >= 24){
d = d + 1;
h = 0;
}
afficheDecale(13, 1, sec);
afficheDecale(10, 1, minu);
afficheDecale(7, 1, h);
afficheDecale(4, 1, d);
////Separation sec, min, heure et jour
afficheText(6, 1, "/" );
afficheText(9, 1, "/" );
afficheText(12, 1, "/" );
}
//Gestion de l'affiche avec decalage de chiffres
void afficheDecale(int col, int row, byte value){
if (value > -10 && value < 10){
afficheValue(col, row, 0);
afficheValue((col + 1), row, value);
}
else{
afficheValue(col, row, value);
}
}
// affichage du texte à une positon donnée
void afficheText(int col, int row, String text){
lcd.setCursor(col, row);
lcd.print(text);
}
// affichage de la valeur d'une variable Integer à une positon donnée
void afficheValue(int col, int row, byte value){
lcd.setCursor(col, row);
lcd.print(value);
}