Extra Zeros at the end of LCD display for timer.

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Bobby9002
 
Posts: 1
Joined: Sat Feb 02, 2013 2:46 pm

Extra Zeros at the end of LCD display for timer.

Post by Bobby9002 »

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


#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;
}


User avatar
dennisma
 
Posts: 31
Joined: Fri Feb 01, 2013 5:39 pm

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

Post by dennisma »

At the beginning of timerLCD() you have

Code: Select all

 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?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”