Hello,
i've read a lot in this forum and found a lot of guidance for my projects. Now I started a new project - a time-based switching for 433MHz Power-Switches. I'm using the TinyRTC for the timing of my software. Unfortunately I recognized a time drift of about 2,5h in 24h runtime. The time is running to slow - so in real world it is 6pm and on my arduino it is 4pm....... How can I fix this? Has this someting to do with the Delay Command used in my Code?
Here is my Code
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// 2010-02-04 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <Wire.h>
#include <RTClib.h>
#include <RCSwitch.h>
RTC_DS1307 RTC;
RCSwitch mySwitch = RCSwitch();
/*****************************************************************************
Anzahl der Schaltzeiten
*****************************************************************************/
const int intSwitchTimesCount = 7;
char SystemCode[6] = "10101";
/*****************************************************************************
Array mit Schaltzeiten
{ [Tag der Woche], [Stunde], [Minute], [Steckdose], [On (1), Off (0)] }
*****************************************************************************/
typedef struct
{
int Day;
int Hour;
int Minute;
char SwitchAddress[6];
int SwitchAction;
} switchTimes;
switchTimes mySwitchTimes[intSwitchTimesCount] = {
{0,17,1,"00010",1},
{0,18,55,"00010",1},
{0,22,1,"00010",0},
{0,17,1,"01000",1},
{0,18,55,"01000",1},
{0,22,1,"01000",0}
};
/*****************************************************************************
setup()
*****************************************************************************/
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
// following line sets the RTC to the date & time this sketch was compiled
//RTC.adjust(DateTime(__DATE__, __TIME__));
}
/*****************************************************************************
loop()
*****************************************************************************/
void loop () {
DateTime now = RTC.now();
Serial.print("Now: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.println();
//Durch Schaltzeiten Array loopen
for (int x=0; x<intSwitchTimesCount; x++)
{
Serial.print("Tag: ");
Serial.print(mySwitchTimes[x].Day, DEC);
Serial.print(" ");
Serial.print(mySwitchTimes[x].Hour, DEC);
Serial.print(":");
Serial.print(mySwitchTimes[x].Minute, DEC);
Serial.print(" Address: ");
Serial.print(mySwitchTimes[x].SwitchAddress);
Serial.print(" Action: ");
Serial.print(mySwitchTimes[x].SwitchAction, DEC);
Serial.println("");
// Check if a Switch Action exists for current day
// Switch Day 0 = every day
if (mySwitchTimes[x].Day == now.dayOfWeek() || mySwitchTimes[x].Day == 0)
{
if (mySwitchTimes[x].Hour == now.hour() && mySwitchTimes[x].Minute == now.minute())
{
// Switch Action -> SwitchOn
if (mySwitchTimes[x].SwitchAction == 1)
{
Serial.println("On");
mySwitch.switchOn(SystemCode, mySwitchTimes[x].SwitchAddress);
}
// Switch Action -> SwitchOff
if (mySwitchTimes[x].SwitchAction == 0)
{
Serial.println("Off");
mySwitch.switchOff(SystemCode, mySwitchTimes[x].SwitchAddress);
}
}
}
Serial.println("--------------------------");
}
Serial.println();
delay(60000);
}