#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int upButton = 6;
int downButton = 7;
int selectButton = 8;
int selectButton2 = 8;
const int debounceDelay = 50;
const int longPressDelay = 500;
int lastButtonState = HIGH;
long lastDebounceTime = 0;
int menu = 1;
int LedOut1 = 3;
int LedOut2 = 4;
int LedOut3 = 5;
int LedOut4 = 2;
int Timer = 0;
int Timer_1 = 0;
int Timer2 = 0;
int Timer3 = 0;
int n=0;// time in seconds
int r1=0;//rest1
int r2=0;
byte np; //nr. of program
byte zm=0;//minutesX10
byte m=0;//minutes
byte zs=0;//secX10
byte s=0;//sec.
byte md=3;// modulo and number total of programs(in this case,3)
int counter;
#include <SimpleRotary.h>
SimpleRotary rotary(6,7,8);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
pinMode(selectButton2, INPUT_PULLUP);
pinMode(LedOut1, OUTPUT);
pinMode(LedOut2, OUTPUT);
pinMode(LedOut3, OUTPUT);
pinMode(LedOut4, OUTPUT);
digitalWrite(selectButton, HIGH);
updateMenu();
}
void loop() {
if (!digitalRead(downButton)){
menu++;
updateMenu();
delay(100);
while (!digitalRead(downButton));
}
if (!digitalRead(upButton)){
menu--;
updateMenu();
delay(100);
while(!digitalRead(upButton));
}
if (!digitalRead(selectButton)){
executeAction();
updateMenu();
delay(100);
while (!digitalRead(selectButton));
}
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
lcd.clear();
lcd.print(">COUNT DOWN");
lcd.setCursor(0, 1);
lcd.print(" LED2");
break;
case 2:
lcd.clear();
lcd.print(" COUNT DOWN");
lcd.setCursor(0, 1);
lcd.print(">LED2");
break;
case 3:
lcd.clear();
lcd.print(">LED3");
lcd.setCursor(0, 1);
lcd.print(" LED4");
break;
case 4:
lcd.clear();
lcd.print(" LED3");
lcd.setCursor(0, 1);
lcd.print(">LED4");
break;
case 5:
menu = 4;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}
void action1() {
byte i;
byte o;
// 0 = not turning, 1 = CW, 2 = CCW
o = rotary.rotate();
//if ( i == 1 ) Serial.println("CW");
//if ( i == 2 )Serial.println("CCW");
// Check to see if button is pressed for 1 second.
i = rotary.pushLong(1000);
counter=0;
lcd.clear();
lcd.print(" COUNT DOWN");
lcd.setCursor(0,1);
do{ //choose progr
while(digitalRead(downButton) &digitalRead(upButton) & i==1);
if(digitalRead(downButton)==0){
return counter;
}
switch(i) {
case HIGH:
while(i==0);
while(digitalRead(upButton)==0);
counter ++;
counter=counter%md;
break;
case LOW:
while(digitalRead(upButton)==0);
while(i==0);
counter --;
if(counter==-1){//left begin with md-1
counter=md-1;
}
counter=abs(counter)%md;
break;
}
return counter;
}
while(i==HIGH);//"Left" button
delay(10); //out of set routine and Start timer
delay(1500);
}
void action2() {
lcd.clear();
lcd.print(">COUNTER");
digitalWrite(LedOut2, HIGH);
Timer_1++;
if(Timer_1 ==2){
digitalWrite(LedOut2,LOW);
Timer_1=0;
}
else{
digitalWrite(LedOut2,HIGH);
}
delay(1500);
}
void action3() {
lcd.clear();
lcd.print(">Toggle Led #3");
digitalWrite(LedOut3, HIGH);
Timer2++;
if(Timer2 ==2){
digitalWrite(LedOut3,LOW);
Timer2=0;
}
else{
digitalWrite(LedOut3,HIGH);
}
delay(1500);
}
void action4() {
lcd.clear();
lcd.print(">Toggle Led #4");
digitalWrite(LedOut4,HIGH);
Timer3++;
if(Timer3 ==2){
digitalWrite(LedOut4,LOW);
Timer3=0;
}
else{
digitalWrite(LedOut4,HIGH);
}
delay(1500);
}
//TIMER VOID
void fTime(int ns,byte nc){// time function with parameters ns= nr. seconds
//nc number of cycles
for(int j=0 ; j<nc ; j++){
digitalWrite(9,HIGH);
for(int i=0 ;i< ns ; i++){
while(millis() % 1000 != 0);// Time Base, 1s.
n=n-1;
zm=n/600;
r1=n%600;
m=r1/60;
r2=r1%60;
zs=r2/10;
s=r2%10;
lcdpr();
}
digitalWrite(9,LOW);
for(int k=0 ;k< ns ; k ++){
while(millis() % 1000 != 0);// Time Base, 1s.
n=n-1;
zm=n/600;
r1=n%600;
m=r1/60;
r2=r1%60;
zs=r2/10;
s=r2%10;
lcdpr();
}
}
}
void lcdpr(){ //lcd print routine
lcd.setCursor(1,1);
lcd.print("Remain ");
lcd.print(zm);
lcd.print(m);
lcd.print("m");
lcd.print(zs);
lcd.print(s);
lcd.print("s");
}