USE UPS NEXT DAY AIR FOR ANY USA ORDER BEFORE 11AM ET *TODAY* 12/19/2014 TO GET YOUR PACKAGE IN TIME FOR XMAS - PLEASE SEE OUR SHIPPING DEADLINE NOTICE FOR MORE DETAILS!

Extra Zeros at the end of LCD display for timer.
Moderators: adafruit_support_bill, adafruit

Extra Zeros at the end of LCD display for timer.

by Bobby9002 on Sat Feb 02, 2013 2:52 pm

Hey guys.

So I'm working on this project and when I'm launching the code for this timer, the LCD display connected displays an extra zero at the end of the timer (00:00:000 when initialized on the arduino). I've looked through the code, but I don't know why there's a third zero attached to the end. Any help would be greatly appreciated. Thanks.

Code: Select all | TOGGLE FULL SIZE

#include <TimerOne.h>
#include <LiquidCrystal.h>

int secs = 0;
int secsForMin = 0;
int secsForHours = 0;

const int buttonPin = 2;

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
 
void setup()

 
  Timer1.initialize(1000000); // set a timer of length 1000000 microseconds (or 1 sec - or 1Hz)
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  attachInterrupt(0, resetTimer, FALLING);
}
 
void loop()
{
  timerLCD();
}

void timerLCD() {
  if(secsForHours < 216000) {
    if(secsForHours < 36000) {
      lcd.print("0");
      lcd.setCursor(1, 1);
      lcd.print(secsForHours / 3600);
    }
    else {
      lcd.print(secsForHours / 3600);
    }
  }
  else{
    lcd.print("00");
    secsForHours = 0;
  }
  lcd.setCursor(2, 1);
  lcd.print(":");
  lcd.setCursor(3, 1);
  if(secsForMin < 3600) {
    if(secsForMin < 600) {
       lcd.print("0");
       lcd.setCursor(4, 1);
       lcd.print(secsForMin / 60);
    }
    else {
      lcd.print(secsForMin / 60);
    }
  }
  else {
    lcd.print("00");
    secsForMin = 0;
  }
  lcd.setCursor(5, 1);
  lcd.print(":");
  lcd.setCursor(6, 1);
  if(secs < 60) {
    if(secs < 10) {
       lcd.print("0");
       lcd.setCursor(7, 1);
       lcd.print(secs);
    }
    else {
      lcd.print(secs * 1);
    }
  }
  else {
    lcd.print("00");
    secs = 0;
  }
}
 
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr() {
  secs ++;
  secsForMin ++;
  secsForHours ++;
}
void resetTimer () {
  secs = 0;
  secsForMin = 0;
  secsForHours = 0;
}

Bobby9002
 
Posts: 1
Joined: Sat Feb 02, 2013 2:46 pm

Re: Extra Zeros at the end of LCD display for timer.

by dennisma on Sat Feb 02, 2013 4:23 pm

At the beginning of timerLCD() you have

Code: Select all | TOGGLE FULL SIZE
 if(secsForHours < 216000) {
    if(secsForHours < 36000) {
      lcd.print("0");


Since you don't call setCursor() beforehand it will print where you left off the second time you call timerLCD() from within your loop. I 'think' this is why you have the extra '0';

Should you not call lcd.setCursor(1, 1); beforehand?
User avatar
dennisma
 
Posts: 31
Joined: Fri Feb 01, 2013 5:39 pm