Ciao a tutti,
ho seguito il consiglio di Leo72,
e dalla sua libreria ho estrapolato la sua funzione "getTimestamp" per confrontarne/capirna il funzionamento, l'ho adattata all'esempio della libreria time che si chiama "TimeSerial" per fare delle prove:
/*
* TimeSerial.pde
* example code illustrating Time library set through serial port messages.
*
* Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
* you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2010
T1262347200
*
* A Processing example sketch to automatically send the messages is inclided in the download
*/
#include <Time.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
////////// VARIABILI DI LEO72
byte daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned long time=0;
unsigned long tmstp;
void setup() {
Serial.begin(9600);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}
void loop(){
time = 0;
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus()!= timeNotSet)
{
digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
tmstp = now();
Leo72();
digitalClockDisplay();
}
//delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.print(" Timestamp = ");
Serial.print(now());
Serial.print(" LeoTimestamp = ");
Serial.print(time);
Serial.print(" LeoTimestamp-Timestamp = ");
Serial.println(time - tmstp);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}
void Leo72()
{ time += (year()- 1970)*365.2422;
for (int i=0; i < month()-1; i++){
time += daysPerMonth[i]; //find day from month
}
time = ( time + day() ) * 24; //find hour from day
time = ( time + hour() ) * 60; //find minute from hours
time = ( time + minute() ) * 60; //find seconds from minute
time += second(); // add seconds
if (time>951847199) { time +=86400; } //year 2000 is a special leap year, so 1 day must be added if date is greater than 29/02/2000
time -=86400UL; //because years start at day 0.0, not day 1.
}
dal serial monitor, passando "T1347738804" che corrisponde come data 15/09/2012 ore 19:53:24 ecco la conferma:
aWaiting for sync message
T19:53:24 15 9 2012 Timestamp = 1347738804 LeoTimestamp = 1347738804 LeoTimestamp-Timestamp = 0
19:53:25 15 9 2012 Timestamp = 1347738805 LeoTimestamp = 1347738805 LeoTimestamp-Timestamp = 0
19:53:26 15 9 2012 Timestamp = 1347738806 LeoTimestamp = 1347738806 LeoTimestamp-Timestamp = 0
19:53:27 15 9 2012 Timestamp = 1347738807 LeoTimestamp = 1347738807 LeoTimestamp-Timestamp = 0
19:53:28 15 9 2012 Timestamp = 1347738808 LeoTimestamp = 1347738808 LeoTimestamp-Timestamp = 0
Ora non mi rimane che scrivere la procedurina alla quale passare i valori che voglio per calcolare i timestamp futuri, che schiappa che sono due giorni per far girare sto sketchino...
Grazie a tutti, ma soprattutto a Leo72.
Riccardo.