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

Extra Zeros at the end of LCD display for timer.

Postby Bobby9002 » Sat Feb 02, 2013 1: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

#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 1:46 pm

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

Postby dennisma » Sat Feb 02, 2013 3:23 pm

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?
User avatar
dennisma
 
Posts: 22
Joined: Fri Feb 01, 2013 4:39 pm


Return to Arduino

Who is online

Users browsing this forum: No registered users and 12 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]