Go Down

Topic: millis() and lcd.clear() coding (Read 479 times) previous topic - next topic

ryan trussell

I was wondering if there was a way to use the millis() statement and work it so that it will count seconds and minutes. I tried simple division and statements such as
x = millis();
if ( x == 1000)
{
  x = 0;
  z++;
}
I was also wondering if it was at all possible to erase one character instead of the entire screen from an LCD using the lcd.clear()  statement which out clearing the screen and reprinting everything but the one character i do not want.

Thank you for your input.

jrraines

lcd.clear and the underlying hd44780 hardware erase the entire screen. printing a space or a few spaces over the code you want to cover up is faster. clear takes upwards of 2 msec. the hd44780 has to write all of its RAM.

millis is an unsigned long. typically you'd keep track of the starting value at the beginning of the interval you were timing, subtract that from the value of millis at some later time, then divide.

davekw7x

#2
Aug 21, 2010, 07:07 am Last Edit: Aug 21, 2010, 02:44 pm by davekw7x Reason: 1
One way: Use lcd.setCursor() to go the part that you want to change.  You can write spaces or whatever new data you want to show.  Leave the rest of the display alone.

Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
   Serial.begin(9600);
   lcd.begin(20, 2);
   
   // This is fixed at the first column of the top row
   lcd.print("Time = 0");
}

unsigned long secs;     // Automatically initialized to zero
unsigned long old_time; // Automatically initialized to zero

void loop()
{
   unsigned long now = millis();
   if ((now - old_time) > 1000) {
       ++secs;
       lcd.setCursor(7, 0); // Column 8 of first row
       lcd.print(secs);
       // Also show on serial monitor just for kicks
       Serial.print("Time = ");
       Serial.println(secs);
       old_time = now;
   }
}


Regards,

Dave

Go Up