Pages: 1 ... 4 5 [6] 7   Go Down
Author Topic: Who needs a Software Real Time Clock?  (Read 8981 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the following sketch should be close to what you want. I have modifed some code that I use to sync a clock to a GPS to add an offset number of hours to the time from a gps. Not tested but should get you going in the right direction.

Code:
// tinyGPS sketch to sync time from a gps

#include <TinyGPS.h>
#include <NewSoftSerial.h>
#include <DateTime.h>

 const int tzOffset = 10; // offset hours from gps time

TinyGPS gps;
NewSoftSerial serial_gps =  NewSoftSerial(3, 2);  // receive on pin 3

void setup()  {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  // set the data rate for the SoftwareSerial port
  serial_gps.begin(4800);
  Serial.println("Waiting for GPS time ... ");
}

void loop(){
  static unsigned long prevTime = 0;  
  while (serial_gps.available()) {
    if (gps.encode(serial_gps.read()))
       gpsTimeSync();  
  }
  unsigned long  prevtime;
  if(DateTime.available()) { // update clocks if time has been synced
    digitalWrite(13,LOW);  // first flash the LED
    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
    digitalWrite(13,HIGH);
  }
  else{
    if(gpsTimeSync()){    // syncs clock and returns true if valid time avail from gps        
      Serial.println("Clock set by GPS");    
    }
  }
  delay(100);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(DateTime.Hour,DEC);
  printDigits(DateTime.Minute);
  printDigits(DateTime.Second);
  Serial.println();
}

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

boolean gpsTimeSync(){
  //  syncs time and returns true if valid time avail from gps
  unsigned long fix_age = 0 ;
  gps.get_datetime(NULL,NULL, &fix_age);
  unsigned long time_since_last_fix;
//  gps.stats(NULL, &time_since_last_fix,NULL,NULL,NULL);
//  Serial.print("time since last fix = ");
//  Serial.println(time_since_last_fix);
  if(fix_age < 1100){
    DateTime.sync(gpsTimeToTime_t());
    return true;
  }  
  return false;
}

time_t gpsTimeToTime_t(){
  // returns time_t from gps date and time
 // uses global tzOffset to calculate the local time
  byte month, day, hour, minute, second, hundredths;
  int year;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, NULL);
  time_t time = DateTime.makeTime( second, minute, hour, day, month, year );
  return time + (tzOffset * SECS_PER_HOUR);
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MEM

thanks heaps, this sketch clarified a lot. My GPS Clock thingy is now working nicely on an LCD display. It should be the predecessor to a Bike GPS system using a graphics display. It should display a compass and the usual info (av moving speed, trip time, trip odometer etc.). I would like to add a altitude over time and speed over time graph. Will see how far I get (and how much time I will waste). Of course I have seen this as the 'Sportstracker' app on Nokia S60 but home brew is more fun.

Cheers Jampac
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

jampac, good to hear that you have it going.

Quote
home brew is more fun.
absolutely!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi --

I am trying to use your DateTime Library to create a timer to turn different lights on and off .... running into what I a sure is a dumb mistake in my end... when I got to compile, I get an error

 "22: error: DateTime.h: No such file or directory In function 'void loop()':"

I have tried everything I know to, and the files most certainly are in the correct place....I even tried downloading the example sketch and have the exact same problem with it.  I think it has something to do with the fact that you create an instance of the class within the header file (which should work totally fine).  But I know a ton of others are using this library, so I am sure there is something silly I am doing wrong, any help would be much appreciated.  thank you.

Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That error message indicates that the library files have not been found in the expected place.
You should have a directory named Datetime alongside all the other arduino libraries and this directory should contain DateTime.h and the other library files.  The download  has the DateTime directory made for you and is intended to be extracted into the Libraries directory, so if you check that you don't have two levels of directories named DateTime.

Looking at some of the other arduino libraries should give you a model of how library directories should be organized.

Good luck!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MEM - just to double check. I am using the dayofweek DayofWeek member from the datetime lib to display the name of the day on my clock thingy and I was wondering if the calculation of DayofWeek has an error.

    DateTime.available();
    lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
    lcd.print(DateTime.DayofWeek,DEC);

Today is Friday 7.8.09 and the DateTime.DayofWeek returns 1 (Monday). Yesterday it returned 0 (Sunday). The value of time is 1252354856 (sec sinc 1970). Where is my error in understanding?

Thanks for your help
Cheer Jampac
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I plugged 1252354856 into the online conversion here: http://www.onlineconversion.com/unix_time.htm
And got: Mon, 7 Sep 2009 20:20:56 UTC

are you sure you have the clock set to the correct time and date?
« Last Edit: August 07, 2009, 05:35:09 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



This is strange - I cut and paste from the Arduino IDE into the conversion URL and get Mon, 7 Sep 2009 20:51:38 UTC

This means that my time (seconds) are exactly one month ahead of time ... although the LCD display has the absolut correct time. I get the tiem through the TinyGPS library from the RMC sentences. The code I am using is:

  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, NULL, NULL);
  time_t time = DateTime.makeTime( second, minute, hour, day, month, year );
  time += (UTCOffset * SECS_PER_HOUR);
  DateTime.sync(time);
  DateTime.available();

  Serial.println(page);

    lcd.setCursor(0, 0);
    twodigits(DateTime.Hour);
    lcd.print(":");
    twodigits(DateTime.Minute);
    lcd.print(":");
    twodigits(DateTime.Second);
    lcd.print("        ");
    

    lcd.setCursor(0, 1);
    twodigits(DateTime.Day);
    lcd.print("/");
    twodigits(DateTime.Month);
    lcd.print("/");
    twodigits(DateTime.Year-100);
    lcd.print(" ");

    DateTime.available();

    lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
    lcd.print(DateTime.DayofWeek,DEC);
    Serial.println(time);


Any idea what is going wrong here? Thanks for your help ...

Cheers Jampac



Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The GPS NMEA sentence for date returns the months starting from 1, DateTime starts months from 0. I must have missed that in the example I posted above. Try subtracting 1 from the month value returned by gps.crack_datetime
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great hint, I changed the code by decrementing the month before
dateTime.makeTime and adding one month to DateTime.Month as follow:

  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, NULL, NULL);
  time_t time = DateTime.makeTime( second, minute, hour, day, month-1, year );
  time += (UTCOffset * SECS_PER_HOUR);
  DateTime.sync(time);
  DateTime.available();

    lcd.setCursor(0, 1);
    twodigits(DateTime.Day);
    lcd.print("/");
    twodigits(DateTime.Month+1);
    lcd.print("/");
    twodigits(DateTime.Year-100);
    lcd.print(" ");
    DateTime.available();
    lcd.print(DateTimeStrings.dayStr(DateTime.DayofWeek));

Now my little waste of time shows the correct day_of_the_week, date and time.

Next thing is to connect the graphics display (http://www.arduino.cc/playground/Code/GLCDks0108), resuing the code and creating a bike odometer / spedometer / compass GPS thingy.

Cheers
Jampac
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds like fun, I will look forward to reading about your progress.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks very much for your help....i knew it was something stupid, I guess i just did not understand how the directory structure was suppose to work, thanks so much!  Got a new question.... I have it working to the point where if i run the processing app, i can sync the clock, what I would like to do is incorporate something like that in the adruino code, so that when you upload the code, it sends / gets the initial time when the sketch starts, any ideas on how to accomplish that?  thanks
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I doubt it's practical to modify the IDE to combine the upload process with clock sync.

However, there is a program called GoBetWino that gets the time from a PC. If you are running windows you may want to look at keeping using this program and having your sketch request the time in setup when it starts. I have not used it recently but the author, MikMo , is a forum member so you can contact him if you need some help.

You can get the program here http://mikmo.dk/gobetwino.html
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible that the library is missing August 31?

Check this sketch, will explain the problem.

Here's the output:

2000-08-30 23:59:50 2000-8-30 23:59:50
2000-09-01 00:00:00 2000-9-1 0:0:0
2000-09-01 00:00:10 2000-9-1 0:0:10
...


2009-08-30 23:59:50 2009-8-30 23:59:50
2009-09-01 00:00:00 2009-9-1 0:0:0
2009-09-01 00:00:10 2009-9-1 0:0:10
...


Code:
#include <DateTime.h>

void setup () {
  Serial.begin(9600);
  DateTime.sync (DateTime.makeTime (50, 59, 23, 30, 8, 2000));
}

void loop () {
    DateTime.available();
    printTimestamp();
    byte sec;
    byte min;
    byte hour;
    byte day;
    byte wday;
    byte month;
    byte year;
    time_t now = DateTime.now();
    DateTime.localTime (&now, &sec, &min, &hour, &day, &wday, &month, &year);
    Serial.print (year+1900, DEC);
    Serial.print ("-");
    Serial.print (month, DEC);
    Serial.print ("-");
    Serial.print (day, DEC);
    Serial.print (" ");
    Serial.print (hour, DEC);
    Serial.print (":");
    Serial.print (min, DEC);
    Serial.print (":");
    Serial.println (sec, DEC);
    delay (10000);
}

void printTimestamp(){
  Serial.print(DateTime.Year+1900,DEC);
  Serial.print("-");
  printDigits(DateTime.Month, 0);
  Serial.print("-");
  printDigits(DateTime.Day, 0);
  Serial.print(" ");
  printDigits(DateTime.Hour, 0);
  printDigits(DateTime.Minute, 1);
  printDigits(DateTime.Second, 1);
  Serial.print(" ");
}

void printDigits(int digits, int useColon){
  if (useColon == 1) {
    Serial.print(":");
  }
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Months start from 0 so August is month 7, see http://www.arduino.cc/playground/Code/DateTime

Using :    DateTime.sync (DateTime.makeTime (50, 59, 23, 30, 7, 2008));
I get:
  2008-07-31 23:59:50 2008-7-31 23:59:50
  2008-08-01 00:00:00 2008-8-1 0:0:0
Logged

Pages: 1 ... 4 5 [6] 7   Go Up
Jump to: