I am making an Arduino alarm clock. I am checking for the state of a button. If it is clicked, I increment the button counter. If the counter is divisible by 2, it is mode 0, if not, it is mode 1. I have small delays in the code for each mode to help the lcd clear itself. Those delays are preventing the buttons to reacting to my touch, because there is likely a delay during that time.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int hours=0;
int minutes=0;
const int modePin=6; //button
int prevModeState=0; //prev state of button
unsigned long timer;
int modeState;//current state
int mode=0;//mode of alarm
int buttonCounter=0;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(6, 0);
lcd.print("Time:");
lcd.setCursor(6, 1);
lcd.print("00:00");
pinMode(modePin, INPUT);
}
void loop() {
modeState=digitalRead(modePin);
if(prevModeState!=modeState){ //this is the part where I check for the state
if(modeState==HIGH){
buttonCounter++;
}
}
prevModeState=modeState;
if(buttonCounter%2==0){
mode=0;
}
else{
mode=1;
}
if(mode==0){
timer=millis();
if(timer%600==0){
minutes++;
if(minutes==60){
minutes=0;
hours++;
}
if(hours==24){
hours=0;
}
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("Time:");
lcd.setCursor(6, 1);
writeTime();
delay(200); //delay
}
}
else if(mode==1){
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Set Time:");
lcd.setCursor(6, 1);
writeTime();
delay(500); //delays
}
}
void writeTime(){
if(minutes<10){
if(hours<10){
lcd.print(String("0")+hours+":"+"0"+minutes);
}
else{
lcd.print(hours+String(":")+"0"+minutes);
}
}
else{
if(hours<10){
lcd.print(String("0")+hours+":"+minutes);
}
else{
lcd.print(hours+String(":")+minutes);//possibly split if problems arise
}
}
}
How can I edit my code?