Go Down

Topic: LCD Timer (Read 19628 times) previous topic - next topic

YourImaginaryFrien

I came acroos the idea of 'reusing' the HelloWorld sketch and replacing the text with " Hi! You Slept For" (It times how long you sleep btw...) and counts in hours (I changed 1000 mseconds to 600,000) but i thought i'd take it further and time in hours and minutes... it wont let me... Any Help?

gary_white

#1
Mar 28, 2010, 06:13 am Last Edit: Mar 28, 2010, 06:17 am by gary_white Reason: 1
Had the same idea a few weeks ago.  You can try this and see if it fits what you want.  It counts sec, min, hrs & days.  Depending on your
"clock" time you may have to adjust a few ms in the delays.  The way it's setup  is working perfect for me.

Code: [Select]
/* This sketch is basically a clock function even though it doesn't
"tell" time.  I designed it to perform operations at a certain time
of the day.  Depending on what time you start the program, you would
have to adjust the time of the operation accordingly. (unless you want
to start the program at midnight) I start by (Scount) counting milli-
seconds, then (Mcount) minutes etc....  It prints to the LCD (4 x 20)
sec, min, hrs, days.
garym
*/
#include <LiquidCrystal.h>          // lib. for LCD

int Scount = 0;                     // count seconds                
int Mcount = 0;                     // count minutes
int Hcount =0;                      // count hours
int Dcount = 0;                     // count days
LiquidCrystal lcd(7,8,9,10,11,12);  // pins connected to LCD

void setup()
{
 lcd.begin(20,4);                   // open the LCD
}
void loop()

{
 lcd.setCursor(0,2);                // sets cursor to 3rd line
 lcd.print ("Hour ");
 lcd.print (Hcount);
 lcd.print ("   ");
 lcd.setCursor (0,3);               // sets cursor to 4th line
 lcd.print ("DAY ");
 lcd.print (Dcount);

 if ( Mcount == 60)                 // if Mcount is 60 do this operation
 {
   delay (32);                      // good place to fine tune timing
   Mcount = 0;                      // reset Mcount
   Hcount ++;
 }
 if (Hcount> 23)                  
 {
   Dcount++;
   Hcount = 0;                      // have to reset Hcount to "0" after 24hrs
 }

 lcd.setCursor (0,1);               // sets cursor to 2nd line
 lcd.print (Mcount);
 lcd.print (" min ");

 lcd.setCursor (0,0);               // sets cursor to 1st line
 lcd.print (Scount);
 lcd.print (" SEC ");

 if (Scount >59)                    // if 60 do this operation
 {
   Mcount ++;                       // add 1 to Mcount
   Scount = 0;                      // reset Scount
   delay (58);                      // changes ms per min

 }

 if (Scount < 60)                   // do this oper. 59 times  
   // to count the seconds
 {
   delay (988);                     // changing by one = 60 ms a min
   Scount ++;                       // add 1 to Scount
 }
}







mem

Another way to do this is to use the Time library in the playground: http://www.arduino.cc/playground/Code/Time

Here is the sketch above modifed to use this library
Code: [Select]
#include <LiquidCrystal.h>          // lib. for LCD
#include <Time.h>

time_t elapsedTime;

//to restart the time counter to Zero:  setTime(0);

LiquidCrystal lcd(7,8,9,10,11,12);  // pins connected to LCD

void setup()
{
 lcd.begin(20,4);                   // open the LCD
}
void loop()
{
 elapsedTime = now();
 lcd.setCursor(0,2);                // sets cursor to 3rd line
 lcd.print("Hour ");
 lcd.print(hour(elapsedTime));
 lcd.print("   ");
 lcd.setCursor (0,3);               // sets cursor to 4th line
 lcd.print("DAYS ");             // elapsed days
 lcd.print(elapsedTime / SECS_PER_DAY);
 lcd.setCursor (0,1);               // sets cursor to 2nd line
 lcd.print(minute(elapsedTime));
 lcd.print(" min ");
 lcd.setCursor(0,0);               // sets cursor to 1st line
 lcd.print(second(elapsedTime));
 lcd.print(" SEC ");
}

Using the Time library has the advantage that it keeps correct time without having to compensate for delays in the loop code. You can also achieve this using the millis function but the Time library makes the sketch code simpler

yashar

Hi Mem,

I am relatively new to Arduino Duemilanove platform, but have managed a few LED projects and now would like to learn how to display time on my 16x2 LCD.

I hooked up the LCD and Arduino circuits as follow:

   * LCD RS pin to digital pin 12
   * LCD Enable pin to digital pin 11
   * LCD D4 pin to digital pin 5
   * LCD D5 pin to digital pin 4
   * LCD D6 pin to digital pin 3
   * LCD D7 pin to digital pin 2
   * VSS pin to GND
   * VDD pin to +5V
   * Vo pin to 2.2kOhm then to GND for contrast purpose
   * RS pin to digital pin 11
   * R/W pin to GND

I managed to get "Hello World" displayed, but using both of the following codes, I don't get the time displayed on the 16x2 LCD Hitachi hd44780. Can please help me why my LCD isn't displaying the time?

Code: [Select]
#include <LiquidCrystal.h>          // lib. for LCD
#include <Time.h>

time_t elapsedTime;

//to restart the time counter to Zero:  setTime(0);

LiquidCrystal lcd(7,8,9,10,11,12);  // pins connected to LCD

void setup()
{
 lcd.begin(20,4);                   // open the LCD
}
void loop()
{
 elapsedTime = now();
 lcd.setCursor(0,2);                // sets cursor to 3rd line
 lcd.print("Hour ");
 lcd.print(hour(elapsedTime));
 lcd.print("   ");
 lcd.setCursor (0,3);               // sets cursor to 4th line
 lcd.print("DAYS ");             // elapsed days
 lcd.print(elapsedTime / SECS_PER_DAY);
 lcd.setCursor (0,1);               // sets cursor to 2nd line
 lcd.print(minute(elapsedTime));
 lcd.print(" min ");
 lcd.setCursor(0,0);               // sets cursor to 1st line
 lcd.print(second(elapsedTime));
 lcd.print(" SEC ");
}


/* This sketch is basically a clock function even though it doesn't
"tell" time.  I designed it to perform operations at a certain time
of the day.  Depending on what time you start the program, you would
have to adjust the time of the operation accordingly. (unless you want
to start the program at midnight) I start by (Scount) counting milli-
seconds, then (Mcount) minutes etc....  It prints to the LCD (4 x 20)
sec, min, hrs, days.
garym
*/
#include <LiquidCrystal.h>          // lib. for LCD

int Scount = 0;                     // count seconds                
int Mcount = 0;                     // count minutes
int Hcount =0;                      // count hours
int Dcount = 0;                     // count days
LiquidCrystal lcd(7,8,9,10,11,12);  // pins connected to LCD

void setup()
{
 lcd.begin(20,4);                   // open the LCD
}
void loop()

{
 lcd.setCursor(0,2);                // sets cursor to 3rd line
 lcd.print ("Hour ");
 lcd.print (Hcount);
 lcd.print ("   ");
 lcd.setCursor (0,3);               // sets cursor to 4th line
 lcd.print ("DAY ");
 lcd.print (Dcount);

 if ( Mcount == 60)                 // if Mcount is 60 do this operation
 {
   delay (32);                      // good place to fine tune timing
   Mcount = 0;                      // reset Mcount
   Hcount ++;
 }
 if (Hcount> 23)
 {
   Dcount++;
   Hcount = 0;                      // have to reset Hcount to "0" after 24hrs
 }

 lcd.setCursor (0,1);               // sets cursor to 2nd line
 lcd.print (Mcount);
 lcd.print (" min ");

 lcd.setCursor (0,0);               // sets cursor to 1st line
 lcd.print (Scount);
 lcd.print (" SEC ");

 if (Scount >59)                    // if 60 do this operation
 {
   Mcount ++;                       // add 1 to Mcount
   Scount = 0;                      // reset Scount
   delay (58);                      // changes ms per min

 }

 if (Scount < 60)                   // do this oper. 59 times  
   // to count the seconds
 {
   delay (988);                     // changing by one = 60 ms a min
   Scount ++;                       // add 1 to Scount
 }
}








&

Code: [Select]


Regards,
Yashar

mem

Yashar, try this version of setup in your code.
If you don't see the Hello World display followed by the time then check your connections.

Code: [Select]

void setup()
{
 lcd.begin(20,4);                   // open the LCD
 // Print a message to the LCD.
 lcd.print("hello, world!");
 delay(2000);  
 lcd.clear();
}


Go Up