Hi, I have been trying to use a RTC DS1307 with an arduino for controlling the lights on my aquarium. The problem is that the RTC cannot keep up the time accurately. I uploaded this program yesterday, with the time from my computer (the RTC has been running on battery), but now the time is about 7 minutes ahead of the actual time. I have read that it is a problem of the chip itself. Is there any solution to this problem other than replacing the RTC?
The RTC is in a board I bought from ebay. Also the ‘SQW’ pin in the DS1307 is left open, as I have read in tutorials that it is not required.
Here is the code I am using:
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
RTC_DS1307 RTC;
const int buttonPin = 2;
int buttonState = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode(buttonPin, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
if (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
}
}
void loop()
{
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
Serial.write(':');
Serial.print(now.minute(), DEC);
Serial.write(':');
Serial.print(now.second(), DEC);
Serial.println();
delay (1000);
//pin 2= button
//pin 3= blue LED
//pin 4= red LED
//pin 5= small LED
//pin 6= big LED
//pin 7= pumps
//pin 8,9,10,11= motor
buttonState = digitalRead(buttonPin);
{
if (buttonState == HIGH)
{
digitalWrite(6, LOW);
if((now.hour() >= 8 && now.hour()< 14 ) || (now.hour() >= 15 && now.hour()< 23 ))
{
digitalWrite(3,HIGH);
Serial.println("blue LED on");
}
else
{
digitalWrite(3,LOW);
}
if((now.hour() >= 10 && now.hour()< 14 ) || (now.hour() >= 15 && now.hour()< 19 ))
{
digitalWrite(4,HIGH);
Serial.println("red LED on");
}
else
{
digitalWrite(4,LOW);
}
if((now.hour() >= 9 && now.hour()< 14 ) || (now.hour() >= 15 && now.hour()< 20 ))
{
digitalWrite(5,HIGH);
Serial.println("SMALL LED on");
}
else
{
digitalWrite(5,LOW);
}
if((now.hour() >= 10 && now.hour()< 14 ) || (now.hour() >= 15 && now.hour()< 19 ))
{
digitalWrite(6,HIGH);
Serial.println("BIG LED on");
}
else
{
digitalWrite(6,LOW);
}
if(now.hour() >= 9 && now.hour()< 22 )
{
digitalWrite(7,HIGH);
Serial.println("Pump on");
}
else
{
digitalWrite(7,LOW);
}
if (( now.hour() >=8 && now.hour()< 9) || (now.hour()>=21 && now.hour()<22))
{
if ((now.minute() >= 30 && now.minute()< 32))
{
digitalWrite(8, HIGH);
delay(1);
digitalWrite(8, LOW);
delay(1);
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
digitalWrite(10, HIGH);
delay(1);
digitalWrite(10, LOW);
delay(1);
digitalWrite(11, HIGH);
delay(1);
digitalWrite(11, LOW);
delay(1);
}
}
}
else {
digitalWrite(6, HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
Serial.println("BIG LED on");
}
}
}