Go Down

Topic: HELP: Countdown Timer using Arduino + 16x2 HD44780 LCD + Tact Switches (Read 2264 times) previous topic - next topic

brutal_magnet

Aug 22, 2011, 10:35 am Last Edit: Aug 22, 2011, 10:43 am by brutal_magnet Reason: 1
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: http://code.google.com/p/clickbutton/downloads/list

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.
Code: [Select]
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;
}

liudr

I can see your hour inc and min inc are identical except for an led. What problem did you have with hours? If you press up, what happens?
By the way, you can use this to take care of always having two digits:

lcd.setCursor(7,0);
if (min<10) lcd.write('0'); // Or use ' ' to print leading space instead of leading zero.
lcd.print(min);

If you don't clear screen and go from 10 to 9, you will see 90 in your code, will you?

dc42

In setup(), did you initialize those 4 pins as inputs and enable their internal pullup resistors? What value do you initialize hourCounter to? If hourCounter goes way out of range, that would cause a similar problem.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up