I'm a beginner and I see I'm not the only one to have understanding problems with RTC clock...
I have a RTC module DS1307 (MR005-001.2) on my Arduino Uno.
I use the Wire.h / Time.h / DS1307RTC.h libraries.
The date and time returned is completely wrong ("17:18:9 2 4 2036") ... What do I do wrong ??
Here is my partial code for setup() :
setSyncProvider(RTC.get);
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
timeStatus() returns TimeSet when time is set and is synced and that's what i get...
I tried several scripts given with the Time Library without changing a line :
TimeRTC
Time RTCLog
TimeRTCSet
And this is my code :
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
int Ah = 16;
int Am = 6;
int Th = 17;
int Tm = 3;
// RVB veille
int Rs = 0;
int Gs = 0;
int Bs = 150;
// RVB alarme
int Ra = 150;
int Ga = 0;
int Ba = 0;
// RVB Transition
int Rt = 0;
int Gt = 255;
int Bt = 0;
// RVB courant
int Rc = 0;
int Gc = 0;
int Bc = 0;
int Actif = 1; // ACTIF ou INACTIF (réveil programmé ou non)
int Encours = 0; // Alarme en cours de sonnerie
// ON ou OFF du bouton
int State = 1;
int Prev = 0;
int Stop = 0;
int Transition = 0;
void setup(){
Serial.begin(9600);
time_t t = now();
setSyncProvider(RTC.get);
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
//if (Actif==1){setTime(16,05,50,31,12,14);} else{setTime(0,0,0,0,0,0);}
pinMode(6, OUTPUT);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(6,HIGH);
digitalWrite(11,HIGH);
}
void loop(){
int On = digitalRead(7);
if (Prev==1 && On==0){
if (State==1){
if (Encours==1){
Encours=0;
Stop=1;
}
State = 0;
} else{State = 1;}
}
if (Th<=hour() && Tm<=minute()){Transition = 1;}
if (Ah==hour() && Am==minute() && Stop==0){
Encours = 1;
State = 1;
}
if(Encours == 1){
if (Gc<Ga){Gc++;}
if (Gc>Ga){Gc--;}
if (Bc<Ba){Bc++;}
if (Bc>Ba){Bc--;}
if (Rc<Ra){Rc++;}
if (Rc>Ra){Rc--;}
} else{
if (Transition==1){
if (Gc<Gt){Gc++;}
if (Gc>Gt){Gc--;}
if (Bc<Bt){Bc++;}
if (Bc>Bt){Bc--;}
if (Rc<Rt){Rc++;}
if (Rc>Rt){Rc--;}
}
else{
if (Gc<Gs){Gc++;}
if (Gc>Gs){Gc--;}
if (Bc<Bs){Bc++;}
if (Bc>Bs){Bc--;}
if (Rc<Rs){Rc++;}
if (Rc>Rs){Rc--;}
}
}
if (State == 0){
Gc = 0;
Bc = 0;
Rc = 0;
}
analogWrite(8,Gc);
analogWrite(9,Bc);
analogWrite(10,Rc);
//Serial.println(Prev);
Prev = On;
// digital clock display of the time
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
delay(50);
}
The library example SetTime sets the RTC time to the compile time on your computer. Perhaps the time on the computer is not correct. Here is a sketch to set the RTC to a unix epoch time stamp.You can get the time stamp from this website http://www.epochconverter.com/
Thanks for your replies.
I tried RTClib and the "softrtc" example file worked !
I don't know why the DS1307RTC library and other example files in RTClib didn't work, but it's ok now.
That is odd.... I've got 2x1307s, one loose and one on a breakout, both worked fine with the DS1307RTC library, but as the man said, All's Well That Ends Well.