Hi all...I'm working at my first project using Arduino and it's IDE and I want to use a macro which is inside the Time.h..For example,after a reset or a power supply fault to the board, I want to calculate the second of the day when the board recovers, therefore to find in which cycle of a finite state machine the program is..For this I want to use the elapsedSecsToday, but I'm far from knowing how to use it, efectivelly...Here is my code,until now, and I want to run this macro in the set-up sequence..Can you give me a clue how to do it?
// GPS + RTC FSM clock
//
#include "Wire.h"
#include <DS1307RTC.h>
#include <Time.h>
#include <Timezone.h> //Jack Christensen's Timezone
#include <TinyGPS.h>//GPS and NewSoftSerial libraries are the work of Mikal Hart
#define DS1307_ADDRESS 0x68
//Timezone settings..=============================.
TimeChangeRule myDST = {
"ROM", Last, Sun, Mar, 3, +180}; //Daylight time = UTC - 4 hours
TimeChangeRule mySTD = {
"ROM", First, Sun, Nov, 3, +120}; //Standard time = UTC - 5 hours
Timezone myTZ(myDST, mySTD);
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
time_t utc, local,t;//var that must be seen by others functions
//Tiny GPS====================================
TinyGPS gps;
boolean valid = false;
//***interrupt*********************
volatile boolean interruptComplete = false;
volatile boolean rtcisrunning = false;
//=========GPS checking=============================================
time_t gpsTimeSync()
{
unsigned long age = 0 ;
unsigned long date, time;
gps.get_datetime(NULL, NULL, &age);
//No time update to DS1307 if GPS signal is lost....
if (age == TinyGPS::GPS_INVALID_AGE || age >1500 ){
//Serial.println("GPS lost..");
valid = false;
}
else{
valid = true ;
gpsToArduino();
}
}
// GPS to DS1307 local time =====================================
time_t gpsToArduino(){
tmElements_t tm;
int year;
gps.crack_datetime(&year, &tm.Month, &tm.Day, &tm.Hour, &tm.Minute, &tm.Second, NULL, NULL);
tm.Year = year-1970 ;
time_t time = makeTime(tm);//GPS data--> time_t....
utc = time;// time from GPS....
local = myTZ.toLocal(utc, &tcr);
//RTC.set(local);// moved from here to the loop...
}
// read a register from DS1307....
void readreg() {
byte reg;
Wire.beginTransmission(0x68); // write the control register
Wire.write(0x00); // register address 07H)
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
reg=Wire.read();
Serial.println (reg,HEX);
//delay(1000);
}
//Debug on serial monitor..=================================================
void monitor_out(){
char dateTime[20];
sprintf(dateTime, "%4d-%02d-%02d %02d:%02d:%02d", year(),
month(), day(), hour(),minute(), second()) ;
Serial.print(dateTime);
Serial.print(" - day of week: ");
Serial.println(dayStr(weekday()));
}
//======================================
void getGPS(){
bool newdata = false;
//unsigned long start = millis();
// Every second we print an update
//while (millis() - start < 1000)
//{
if (feedgps()) newdata = true;
gpsTimeSync();
//}
}
// ***********************************************
// read GPS serial data and send to tinyGPS
// ***********************************************
static bool feedgps() {
while (Serial1.available()) {
if (gps.encode(Serial1.read())) {
return true;
}
}
return false;
}
//=================================
void setup()
{
Serial.begin(19200);//monitor
Serial1.begin(19200); // for GPS
Serial1.println ("$PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28");//RMC only...
Serial1.println ("$PMTK220,1000*1F");//fix at 1000 ms
attachInterrupt(0, interruptHandler, FALLING);
Wire.begin();
delay(1000);//debug only..
while ( ! valid ){
getGPS();
}
if (valid){
RTC.set(local);// DS1307 set to the local time from timezone..
Serial.println ( "DS set");//debug only
delay(1000);//debug only
}
//Serial.println (elapsedSecsToday());
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
delay(500);//debug only
}
void loop()
{
if (interruptComplete){
if (valid){ // If GPS sentence is valid,write the time, if not, skip..
RTC.set(local);// DS1307 set to the local time from timezone..
Serial.println ( "beep");//debug only
}
if (! valid){
Serial.println ("No GPS");// Serial2---->Ethernet...
}
RTC.get();// time from DS1307...
monitor_out(); //print it...
//readreg();
interruptComplete = false;
}
getGPS();// get data from GPS
}
//interrupt vector=============================
void interruptHandler()
{
rtcisrunning = true;
interruptComplete = true;
}