#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int lcdblst=0,plcdblst=0,timeadd=0,ptimeadd=0,pcursor=0,cursor=0,sel=0,psel=0;
int h,m,s,c=0,select=0,th=0,eh=0,em=0,es=0;
void setup() {
lcd.init();
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
if(digitalRead(2)==LOW && plcdblst==0)
{
if(lcdblst==0) {
lcd.backlight();
}
else {
lcd.noBacklight();
}
if(lcdblst==0)
lcdblst=1;
else
lcdblst=0;
plcdblst=1;
}
else if(digitalRead(2)==HIGH) {
plcdblst=0;
}
//------------------------------------------------//
if(digitalRead(6)==LOW && ptimeadd==0)
{
if(timeadd==0 && digitalRead(4)==HIGH) {
if(c==0 && h<90)
h+=10;
if(c==1)
h++;
if(c==3 && m<90)
m+=10;
if(c==4)
m++;
if(c==6 && s<90)
s+=10;
if(c==7)
s++;
}
else if(timeadd==0 && digitalRead(4)==LOW){
if(c==0 && h>9)
h-=10;
if(c==1 && h>0)
h--;
if(c==3 && m>9)
m-=10;
if(c==4 && m>0)
m--;
if(c==6 && s>9)
s-=10;
if(c==7 && s>0)
s--;
}
ptimeadd=1;
}
else if(digitalRead(6)==HIGH) {
ptimeadd=0;
}
//--------------Cursor-------------//
if(digitalRead(5)==LOW && pcursor==0)
{
if(cursor==0 && digitalRead(4)==HIGH) {
if(c<7)
c++;
else
c=0;
}
else if(cursor==0 && digitalRead(4)==LOW){
if(c>0)
c--;
else
c=7;
}
pcursor=1;
}
else if(digitalRead(5)==HIGH) {
pcursor=0;
}
//------------Time-----------//
if(m>=60 && h<100){
m=m-60;
h++;
}
if(s>=60 && m<100){
s=s-60;
m++;
}
//---------Stop clock--------//
th++;
if(th==50){
if(select==1){
if(s==0 && m==0 && s==0){
es++;
if(es>59 && em<98){
em++;
es=0;
}
if(em>59 && eh<98){
eh++;
em=0;
}
}
if(s>0)
s--;
if(s==0 && m>0){
m--;
s=59;
}
if(s==0){
if(m>0)
m--;
if(m==0 && h>0){
h--;
m=59;
s=59;
}
}
}
th=0;
}
//---------Pause/Resume---------//
if(digitalRead(3)==LOW && psel==0)
{
if(sel==0) {
select=1;
}
else {
select=0;
}
if(sel==0)
sel=1;
else
sel=0;
psel=1;
}
else if(digitalRead(3)==HIGH) {
psel=0;
}
//----------Display----------//
lcd.clear();
lcd.setCursor(c,1);
lcd.print("^");
lcd.setCursor(0,0);
lcd.print(h);
lcd.setCursor(2,0);
lcd.print(":");
lcd.setCursor(3,0);
lcd.print(m);
lcd.setCursor(5,0);
lcd.print(":");
lcd.setCursor(6,0);
lcd.print(s);
lcd.setCursor(9,0);
if(select==0)
lcd.print("PAUSED");
else
lcd.print("");
lcd.setCursor(8,1);
lcd.print(eh);
lcd.setCursor(10,1);
lcd.print(":");
lcd.setCursor(11,1);
lcd.print(em);
lcd.setCursor(13,1);
lcd.print(":");
lcd.setCursor(14,1);
lcd.print(es);
delay(20);
}