Pages: [1]   Go Down
Author Topic: HELP: Countdown Timer using Arduino + 16x2 HD44780 LCD + Tact Switches  (Read 1634 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
« Last Edit: August 22, 2011, 03:43:53 am by brutal_magnet » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6908
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 23, 2011, 09:47:55 am by dc42 » Logged

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.

Pages: [1]   Go Up
Jump to: