Displaying time using LCD ( NO RTC or any external device)

Hello,

So I'm working on a project where am supposed to display time on an lcd and having a timer interrupter !!

I had hard time working on Time Library as I will always get such an error, "DateTime was not declared in the scope " !!!

even that i have my time.h file as the following : C:\Users\ad\Documents\Arduino\libraries\Time

Right now am working on the following code :

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
void setup() {
lcd.print("What time is it?");
delay(1000);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Jan, 02, 2014"); // print out the date
}
//thhis is a list of int variables used in this clock program
int s=0;
int sec=0;
int hrs=0;
int minutes=0;
int initialHours = 02;//variable to initiate hours
int initialMins = 0;//variable to initiate minutes
int initialSecs = 00;//variable to initiate seconds
 
//this method is for seconds
int seconds()
{
s = initialHours*3600;
s = s+(initialMins*60);
s = s+initialSecs;
s = s+(millis()/1000);
return s;
}
//this method is for hours
int hours()
{
hrs = seconds();
hrs = hrs/3600;
hrs = hrs%24;
return hrs;
}
//this method is for minutes
int mins()
{
minutes = seconds();
minutes = minutes/60;
minutes = minutes%60;
return minutes;
}
 
int secs()
{
sec = seconds();
sec = sec%60;
return sec;
}
//this loop will conintue to keep looping so that the time can go as follows
void loop(){
digitalClockDisplay();
}
void printDigits(byte digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
char sep()
{
s = millis()/1000;
if(s%2==0)
{
lcd.print(":");
}
else {
lcd.print(" ");
}
}
void digitalClockDisplay(){
lcd.setCursor(0,1);
printDigits(
hours());
sep();
printDigits(mins());
sep();
printDigits(secs());
}

and I have two questions:

  1. how can i get the time to update automatically?? in other words to take the real time from the PC and dispaly it on the lcd, instead of intializing the time maually !!

  2. I dont really get what the timer interrupter should do exactly ?? even our teacher had hard time explanning that !!

Am working on Arduino Duemilanove !!

attached : a photo of the project !!

With Time.h included you can do this

setTime(10, 11, 12, 1, 2, 2015); // set time to hh,mm,ss,dd,mm,yy

Then do

  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.println(second());

So now all you need is a way of setting the date and time to now. The date and time that the code was compiled is available in the DATE and TIME variables. If you use these to derive the parameters for setTime() then at least the clock will be right when it is compiled and run. Unfortunately when it is run again the date and time will revert back to the date and time of compilation.

So, what to do ?

  1. Use a Real Time Clock (RTC) attached to the Arduino
    or
  2. Write the Arduino program so that it prompts for the date and time in the Serial monitor when run
    or
  3. Write (or find) an program that runs on the PC and sends the date and time through the serial port to the Arduino.

Of these, option 2 is the only one that requires no extra hardware or software.

As to your question about the timer interrupt, this is not implemented in the program that you posted.

You have not #included Time.h in the program you posted but when you say

i have my time.h file as the following : C:\Users\ad\Documents\Arduino\libraries\Time

what is the exact name of the file ? Is it Time.h or time.h ? The latter version is wrong.

Incidentally I see this in your program

  lcd.print("Jan, 02, 2014"); // print out the date

Is that the correct date ?

Hello UKHeliBob,

thank you for your answers. I have been reading around and all are agreeing on what you have said that I need to write a code that can gets the time, and that is what Iam trying to do.

the time library is not working with me and I saw that its not only me who has that problem !!

i did download the time library as zip file and I have unzipped it, it contains 3 files DS1307RTC, Time and TimeAlarms. I took the Time file which of course Time.h and placed it into libraries as mentioned above and yes its called Time.h !!!!!! but still not working and still getting this erroe DateTime was not declared in the scope !!

you have suggested to use something like

Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.println(second());

why ?? why do i need to use serial while am using LCD ?

and you were asking about :

lcd.print("Jan, 02, 2014"); // print out the date

if it was a correct date? that was two days back date :slight_smile:

and if i want to update it i have to do that manually as i have said before !!

so what to do know ?

any suggestions ?

Thanks in advance

why do i need to use serial while am using LCD ?

Because it is a very quick way to test the program. You can print to the LCD as well or instead of the serial monitor.

lcd.print("Jan, 02, 2014"); // print out the date

if it was a correct date? that was two days back date :slight_smile:

No it wasn't :slight_smile:

To be clear, do you have Time.h in a folder named Time in the sketches libraries folder ?
On my Windows system this is C:\Users\Bob\Documents\Arduino\libraries\Time

If so, if you have stopped and started the IDE since installing Time.h then it should be available to be used.
Please post the full program that gave you the " DateTime was not declared in the scope " error.

To be clear, do you have Time.h in a folder named Time in the sketches libraries folder ?
On my Windows system this is C:\Users\Bob\Documents\Arduino\libraries\Time

Time.cpp and some other files should be in there as well.

Time.cpp and some other files should be in there as well.

You are right of course. My fault for not being more specific.

Thanks guys for your help !!

It´s really appreciated !!

The Time folder contains : Time.h, Time.cpp, Readme.txt,keywords.txt, DateStrings.cpp and another folder called Examples!! each of these files has this path C:\Users\ad\Documents\Arduino\libraries\Time

when i go to arduino IDE i can easily see the time library there under Sketch>Import libraries i can see time library there !!

as well as, i can see examples of that library under File>SketchBook>libraries>time>examples !!

I was using the following code :

#include <Time.h>

#include <DateTime.h>
#include <DateTimeStrings.h>


#define TIME_MSG_LEN  11   // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup(){
  Serial.begin(19200);
}

void  loop(){  
  getPCtime();   // try to get time sync from pc        
  if(DateTime.available()) { // update clocks if time has been synced
    unsigned long prevtime = DateTime.now();
    while( prevtime == DateTime.now() )  // wait for the second to rollover
      ;
    DateTime.available(); //refresh the Date and time properties
    digitalClockDisplay( );   // update digital clock

    // send our time to an app listening on the serial port
    Serial.print( TIME_HEADER,BYTE); // this is the header for the current time
    Serial.println(DateTime.now());      
  }
}

void getPCtime() {
  // if time available from serial port, sync the DateTime library
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message
    if( Serial.read() == TIME_HEADER ) {        
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){  
        char c= Serial.read();          
        if( c >= '0' && c <= '9')  
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number            
      }  
      DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
    }  
  }
}

void digitalClockDisplay(){
  // digital clock display of current time
  Serial.print(DateTime.Hour,DEC);  
  printDigits(DateTime.Minute);  
  printDigits(DateTime.Second);
  Serial.print(" ");
  Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
  Serial.print(" ");  
  Serial.print(DateTimeStrings.monthStr(DateTime.Month));  
  Serial.print(" ");
  Serial.println(DateTime.Day, DEC);  
}

void printDigits(byte digits){
  // utility function for digital clock display: prints colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits,DEC);  
}

if(DateTime.available()) { and this line will give me DateTime was not declared in the scope !!

I actually found that code somewhere, and since i found it am trying to modify it but still not working !!

Thanks alot for your time and your effort !! :grin: :grin: :grin:

Have you got the file named DateTimeStrings.h that you are #including ?
If so, where is it located ?

Incidentally, the code is ancient
    Serial.print( TIME_HEADER,BYTE); // this is the header for the current timeThe BYTE form of Serial.print() was replaces ages ago by Serial.write(value);

#include <DateTime.h>
#include <DateTimeStrings.h>

These libraries have been superceded by the Time library. Your sketch is also an example using these older libraries. I would suggest that you learn how to use the Time library and forget about the other.

If you are trying to set the time from the serial monitor you need to use the example in the Time library called TimeSerial. You enter the character 'T' and 10 digit unix time in the top line of the serial monitor and press send. http://www.epochconverter.com/