Hello! I'm making a countdown timer for our class experiment so using 4 tact switches I am able to set the value for hh:mm:ss by doing increment or decrement. There are no pull up/pull down resistor attached to every switches and each is connected from pins 8,9,10 and 11 (for buttons 1,2,3,4 respectively) to ground.
Button 1: Since I'd like to do a multi-click switch that can do SINGLE,DOUBLE and TRIPLE CLICK, I've used <ClickButton.h> in my program. I found the library here: Google Code Archive - Long-term storage for Google Code Project Hosting.
Button 2: Is for Increment (e.g hh=0-99,mm=0-59 and ss=0-59)
Button 3: Is for decrement (e.g hh=98-0, mm=58-0 and ss=58-0)
Button 4: is the start button. So after setting the value for hh:mm:ss, it will start the countdown.
HERE'S THE PROBLEM: the counter for mm and ss is working, but for hh I'm having trouble coz it is NOT WORKING! I don't know why is it like that, I used the same code for the counter for hh,mm and ss. Below is my code.
void loop()
{
//for Set button
// update button state
// Note: Will return the last click decoded, even if no new clicks are received!
button1.Update();
// Toggle LED on single clicks. But only if the click code is new
//LED Turns on if HIGH and off if LOW
if(button1.click ==CLICK_SINGLECLICK && button1.click != lastClickCode)
{
ledState = !ledState;
lcd.setCursor(0,1);
lcd.print("Set the hour....");
// Checks if button is increment
hourStateInc = digitalRead(btnInc);
if (hourStateInc!=hourLastStateInc)
{
if (hourStateInc== HIGH)
{
hourCounter++;
hour=hourCounter;
hour = constrain(hour,0,99);
if (hour<=9)
{
lcd.setCursor(5,0);
lcd.print(hour);
}
else
{
lcd.setCursor(4,0);
lcd.print(hour);
}
}
}
//decrement hour
hourStateDec = digitalRead(btnDec);
if (hourStateDec!=hourLastStateDec)
{
//when btnInc is pressed only ONCE
if (hourStateDec== HIGH)
{
hourCounter--;
hour=hourCounter;
hour = constrain(hour,0,98);
if (hour<=9)
{
lcd.setCursor(4,0);
lcd.print("0");
lcd.setCursor(5,0);
lcd.print(hour);
}
else
{
lcd.setCursor(4,0);
lcd.print(hour);
}
}
}
// save the current state as the last state,
//for next time through the loop
hourLastStateInc = hourStateInc;
hourLastStateDec = hourStateDec;
}
// blink faster if double clicked
if(button1.click == CLICK_DOUBLECLICKED)
{
ledState = (millis()/500)%2;
lcd.setCursor(0,1);
lcd.print("Set the minutes.");
// Checks if button is increment
minsStateInc = digitalRead(btnInc);
if (minsStateInc!=minsLastStateInc)
{
if (minsStateInc== HIGH)
{
minsCounter++;
mins=minsCounter;
mins = constrain(mins,0,59);
digitalWrite(ledPin2,HIGH);
if (mins<=9)
{
lcd.setCursor(8,0);
lcd.print(mins);
}
else
{
lcd.setCursor(7,0);
lcd.print(mins);
}
}
}
//decrement mins
minsStateDec = digitalRead(btnDec);
if (minsStateDec!=minsLastStateDec)
{
//when btnInc is pressed only ONCE
if (minsStateDec== HIGH)
{
minsCounter--;
mins=minsCounter;
mins = constrain(mins,0,58);
digitalWrite(ledPin2,HIGH);
if (mins<=9)
{
lcd.setCursor(7,0);
lcd.print("0");
lcd.setCursor(8,0);
lcd.print(mins);
}
else
{
lcd.setCursor(7,0);
lcd.print(mins);
}
}
}
// save the current state as the last state,
//for next time through the loop
minsLastStateInc = minsStateInc;
minsLastStateDec = minsStateDec;
}
// blink even faster if triple clicked
if(button1.click == CLICK_TRIPLECLICKED)
{
ledState = (millis()/200)%2;
lcd.setCursor(0,1);
lcd.print("Set the seconds.");
// Checks if button is increment
secStateInc = digitalRead(btnInc);
if (secStateInc!=secLastStateInc)
{
if (secStateInc== HIGH)
{
secCounter++;
sec=secCounter;
sec = constrain(sec,0,59);
if (sec<=9)
{
lcd.setCursor(11,0);
lcd.print(sec);
}
else
{
lcd.setCursor(10,0);
lcd.print(sec);
}
}
}
//decrement sec
secStateDec = digitalRead(btnDec);
if (secStateDec!=secLastStateDec)
{
//when btnInc is pressed only ONCE
if (secStateDec== HIGH)
{
secCounter--;
sec=secCounter;
sec = constrain(sec,0,58);
if (sec<=9)
{
lcd.setCursor(10,0);
lcd.print("0");
lcd.setCursor(11,0);
lcd.print(sec);
}
else
{
lcd.setCursor(10,0);
lcd.print(sec);
}
}
}
// save the current state as the last state,
//for next time through the loop
secLastStateInc = secStateInc;
secLastStateDec = secStateDec;
}
// update the LED
digitalWrite(ledPin,ledState);
// Save previous button click code
lastClickCode = button1.click;
}