Help with my little clock thingy

Hey.

I started trying to make a little clock that counts the seconds since restart on a LCD up to 59, and at 60 goes back to 0.
But it goes up to 60, then it goes (for some reason) 09, 19, 29, 39, 49, 59, 69, 89, 99, 10 (at ten it continues as it should). Here's the code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sec;
void setup() {
  lcd.begin(16, 2);
}

void loop() {
  sec=(sec+1);
  lcd.setCursor(1, 0);
  lcd.print("00:00:"); //Just to make the thing more "clock-like"
  if (sec >= 60){
    sec=0;
  }
  lcd.print(sec);
  delay(1000);
}

you just have to send a clear message to the lcd. That 9 that is there when it shouldn't is just the 9 from 59 that is not erased. So when you erase your lcd, or just send two spaces to it, it will erase that 59 and restart as it should.

(deleted)

spycatcher, how do I? I'm somewhat of a noob at LCDs.

EDIT: I'll try myself first. Will post in next edit if I fail epicly or succeed.

EDIT2: FAAAAIL, it went 010, 011, 012, 013, etc. but atleast the zero appeared xD

EDIT3: Jeez, many edits, but however, now i'll try Steen's way.

EDIT4: Steen's way only cleared it when going from 59 to 60, and it didn't send 1, 2, 3, etc. and the screen locked up.

Post your current code, if you are still having problems.

Okay.

Steen's way:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sec;
void setup() {
  lcd.begin(16, 2);
}

void loop() {
  sec=(sec+1);
  lcd.setCursor(1, 0);
  lcd.print("00:00:");
  if (sec >= 60){
    lcd.print("  ");
  }
  else{
    lcd.print(sec);
}
  delay(1000);
}

spycatcher's way:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sec;
void setup() {
  lcd.begin(16, 2);
}

void loop() {
  sec=(sec+1);
  lcd.setCursor(1, 0);
  lcd.print("00:00:");
  if (sec >= 60){
    sec=0;
  }
  if (sec <= 9);{
    lcd.print("0");
  } 
  lcd.print(sec);
  delay(1000);
}

I think Steen intended you to use lcd.clear().

You seem to have removed the code to rollover from 59 to 0 seconds.

OK, I don't know about all the lcd. commands, but won't that remove my 00:00: ?

And for the rollover, where?

Ohmygodohmygodohmygod!

Thanks to you guys, it works after some tinkering with my code!

Here's the final code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sec;
void setup() {
  lcd.begin(16, 2);
}

void loop() {
  sec=(sec+1);
  lcd.setCursor(0, 0);
  lcd.print("00:00:");
  if (sec >= 60){
    sec=0;
    lcd.clear();
    lcd.print("00:00:");
    lcd.print(sec);
  }
  else{
    lcd.clear()
    lcd.print(00:00:
    lcd.print(sec);
}
  delay(1000);
}

Here's the library that your using, take a look at the command list and also look at the functions. The syntax is like this lcd.clear() you can also use the setCursur(col,row) this will set were the cursur will start that way you can just change one digit on the display at a time. The top left hand digit is 0,0.

#ifndef LiquidCrystal_h
#define LiquidCrystal_h

#include <inttypes.h>
#include "Print.h"

// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80

// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00

// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00

// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00

// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00

class LiquidCrystal : public Print {
public:
  LiquidCrystal(uint8_t rs, uint8_t enable,
		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
  LiquidCrystal(uint8_t rs, uint8_t enable,
		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);

  void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
	    uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
	    uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
    
  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);

  void clear();
  void home();

  void noDisplay();
  void display();
  void noBlink();
  void blink();
  void noCursor();
  void cursor();
  void scrollDisplayLeft();
  void scrollDisplayRight();
  void leftToRight();
  void rightToLeft();
  void autoscroll();
  void noAutoscroll();

  void createChar(uint8_t, uint8_t[]);
  void setCursor(uint8_t, uint8_t); 
  virtual size_t write(uint8_t);
  void command(uint8_t);
  
  using Print::write;
private:
  void send(uint8_t, uint8_t);
  void write4bits(uint8_t);
  void write8bits(uint8_t);
  void pulseEnable();

  uint8_t _rs_pin; // LOW: command.  HIGH: character.
  uint8_t _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
  uint8_t _enable_pin; // activated by a HIGH pulse.
  uint8_t _data_pins[8];

  uint8_t _displayfunction;
  uint8_t _displaycontrol;
  uint8_t _displaymode;

  uint8_t _initialized;

  uint8_t _numlines,_currline;
};

#endif

SystemTech:
Here's the library that your using, take a look at the command list and also look at the functions. The syntax is like this lcd.clear() you can also use the setCursur(col,row) this will set were the cursur will start that way you can just change one digit on the display at a time. The top left hand digit is 0,0.

#ifndef LiquidCrystal_h

#define LiquidCrystal_h

#include <inttypes.h>
#include "Print.h"

// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80

// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00

// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00

// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00

// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00

class LiquidCrystal : public Print {
public:
  LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
  LiquidCrystal(uint8_t rs, uint8_t enable,
uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);

void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
    uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
    uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
   
  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);

void clear();
  void home();

void noDisplay();
  void display();
  void noBlink();
  void blink();
  void noCursor();
  void cursor();
  void scrollDisplayLeft();
  void scrollDisplayRight();
  void leftToRight();
  void rightToLeft();
  void autoscroll();
  void noAutoscroll();

void createChar(uint8_t, uint8_t[]);
  void setCursor(uint8_t, uint8_t);
  virtual size_t write(uint8_t);
  void command(uint8_t);
 
  using Print::write;
private:
  void send(uint8_t, uint8_t);
  void write4bits(uint8_t);
  void write8bits(uint8_t);
  void pulseEnable();

uint8_t _rs_pin; // LOW: command.  HIGH: character.
  uint8_t _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
  uint8_t _enable_pin; // activated by a HIGH pulse.
  uint8_t _data_pins[8];

uint8_t _displayfunction;
  uint8_t _displaycontrol;
  uint8_t _displaymode;

uint8_t _initialized;

uint8_t _numlines,_currline;
};

#endif

[brainmelt]Now THAT'S much info.[/brainmelt]

Hi,

The eaises way is to use the lcd.clear() function,

What I did for one of mine though was clear off the line were the bit started, at the bit that goes

lcd.print("00:00:");

this is cursor position 6 where the text will carry on from.

so,

lcd.setCursor(0,6);
lcd.print("  "); //two spaces
lcd.print(sec); //next on in series.

This is the way I did it for a temperature measurement thing, I built.

I have not checked this for compile errors though.

then it goes (for some reason) 09, 19, 29, 39, 49, 59, 69, 89, 99, 10 (at ten it continues as it should).

try this in your code

void displayTimeDigits(int digits){
  // utility function for time display: displays leading 0.
   if(digits < 10)
    lcd.print('0');
    lcd.print(digits);
}

you should put that after the end of your loop()

and then in side the loop call the function like this

 // Display the seconds left in a minute.
     lcd.setCursor(7, 1);
     lcd.print("Sec ");
     displayTimeDigits(59 - second()); //this is the call to the function
     delay(1000);

if you want to use the second() function then you'll have to add the time library to your code.
#include <Time.h>