#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int m,ss,sm,sh,ms,mm,mh,typ;
bool st,rl,run,sec,led;
unsigned long systime,lasttime;
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(13,OUTPUT);
// initialize the LCD
lcd.init();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("S-00:00:00 TYP-A");
lcd.setCursor(0,1);
lcd.print("P-00:00:00 R-OFF");
}
void loop()
{
systime = millis();
if(run){
if(systime-lasttime>1000){lasttime=systime; sec=1;}
if(typ==0){
run=1;
rl=0;
}else if(typ==1){
run=1;
rl=1;
}
if(ss==ms&sm==mm&sh==mh){
if(typ==0){
run=0;
rl=1;
} else if(typ==1){
run=0;
rl=0;
} else if(typ==2){
rl=!rl;
}
ms=mm=mh=0;
}
if(sec){
led=!led;
sec=0;
ms++;
if(ms>59){
ms=0;
mm++;
}
if(mm>59){
mm=0;
mh++ ;
}
if(mh>99){
mm=0;
}
}
}
else {led=0;}
//-------
if(digitalRead(3)&digitalRead(4)&digitalRead(5)){st=0;}
if(!digitalRead(3)&!st&!run){
st=1;
m++;
rl=0;
if(m>4)m=0;
}
if(!digitalRead(4)&!st){
st=1;
if(m==0){
run=0;
ms=mm=mh=0;
rl=0;
} else if(m==1){
typ++;
if(typ>2)typ=0;
}else if(m==2){
ss++;
if(ss>59)ss=0;
}else if(m==3){
sm++;
if(sm>59)sm=0;
}else if(m==4){
sh++;
if(sh>99)sh=0;
}
}
if(!digitalRead(5)&!st){
st=1;
if(m==0&(ss+sm+sh)>0){
run=1;
lasttime=systime;
} else if(m==1){
typ--;
if(typ<0)typ=2;
}else if(m==2){
ss--;
if(ss<0)ss=59;
}else if(m==3){
sm--;
if(sm<0)sm=59;
}else if(m==4){
sh--;
if(sh<0)sh=99;
}
}
//-------------
lcd.setCursor(2,0);
if(m==0|m==4){if(sh<10)lcd.print("0");
else lcd.print(sh/10);
lcd.print(sh%10);
}else lcd.print(" ");
lcd.setCursor(5,0);
if(m==0|m==3){
if(sm<10)lcd.print("0");
else lcd.print(sm/10);
lcd.print(sm%10);
}else lcd.print(" ");
lcd.setCursor(8,0);
if(m==0|m==2){
if(ss<10)lcd.print("0");
else lcd.print(ss/10);
lcd.print(ss%10);
}else lcd.print(" ");
lcd.setCursor(15,0);
if(m==0|m==1){
if(typ==0)lcd.print("A");
else if(typ==1)lcd.print("B");
else if(typ==2)lcd.print("C");
}else lcd.print(" ");
//-------------
lcd.setCursor(2,1);
if(mh<10)lcd.print("0");
else lcd.print(mh/10);
lcd.print(mh%10);
lcd.setCursor(5,1);
if(mm<10)lcd.print("0");
else lcd.print(mm/10);
lcd.print(mm%10);
lcd.setCursor(8,1);
if(ms<10)lcd.print("0");
else lcd.print(ms/10);
lcd.print(ms%10);
lcd.setCursor(13,1);
if(rl)lcd.print("ON ");
else lcd.print("OFF");
//----------
digitalWrite(2,rl);
digitalWrite(13,led);
}