/*
* TimeRTCSet.pde
* example code illustrating Time library with Real Time Clock.
*
* RTC clock is set in response to serial port time message
* A Processing example sketch to set the time is included in the download
* On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone)
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
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");
}
void loop()
{
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
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.println();
}
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);
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
Hallo,
ich habe Deinen Code mit C&P bei mir eingefügt. Dann- überprüfen...
Ich habe keine FehlerMeldungen.
Hast Du die Lib´s richtig zugeordnet/installiert?
Gruß und Spaß
Andreas
Hallo,
kann es sein, dass Du den Code im eigenem Sketch nutzt. Und die Variablen in verschiedenen Funktionen liegen?
Sonst fällt mir da nichts mehr zu ein...
Gruß und Spaß
Andreas
Hallo,
"TimeRTCSet:18: error: 'RTC' was not declared in this scope"
Dieser Fehler läßt sich bei mir reproduzieren.
Wenn die "DS1307RTC.h", nicht oder falsch installiert ist, dann kommt der.
So sollte der Kopf u.a. dargestellt werden: #include <DS1307RTC.h> #include <Time.h> #include <Wire.h>
Gruß und Spaß
Andreas