RTC DS1307 instead of Rtc_Pcf8563 for a automated chicken coop door

Hi Guys

I am trying to build and programme an automatic chicken coop door opener. I am struggling quite a lot with the RTC. The door is to open/close on fixed hours.

I found a nice code, but need to modify it to my hardware available.
I have a Arduino One and a RTC board with the DS1307 RTC. The code a have on hand is made for a RTC PCF8563 chip.

I therefore need help on the recoding of the RTC part. The rest of my setup is working.

Problem area:

void syncTime() {
setTime(rtc.getHour(), rtc.getMinute(), rtc.getSecond(),
rtc.getDay(), rtc.getMonth(), rtc.getYear());
}

Complete code:

#include <Wire.h>
//include <Rtc_Pcf8563.h> // original RTC
#include <Time.h>       //original Time lib
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

#define POS_UNKNOWN 0
#define POS_TOP 1
#define POS_BOT 2

int MOTOR_A_EN_PIN = 6;
int MOTOR_A_IN1_PIN = 7;
int MOTOR_A_IN2_PIN = 8;
int MOTOR_A_SPEED = 255;
int TOP_BUT_PIN = 9;
int BOT_BUT_PIN = 10;

//init the real time clock
//Rtc_Pcf8563 rtc;
DS1307RTC rtc;  //added

int pos = POS_UNKNOWN;
int desiredPos = POS_BOT;

// Time represents just hour/min and time = hour * 60 + min
int raiseTime = 430; // 7:10
int lowerTime = 1130; // 18:50

//int raiseTime = 678; // 7:10
//int lowerTime = 679; // 18:50

void setup()
{

Serial.begin(9600);
pinMode(MOTOR_A_EN_PIN, OUTPUT);
pinMode(MOTOR_A_IN1_PIN, OUTPUT);
pinMode(MOTOR_A_IN2_PIN, OUTPUT);

pinMode(TOP_BUT_PIN, INPUT); 
pinMode(BOT_BUT_PIN, INPUT); 

delay(1000);
//DO NOT UPDATE TIME ANYMORE
//clear out the registers
// rtc.initClock();
//set a time to start with.
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
//rtc.setDate(14, 1, 1, 0, 14);
//hr, min, sec
//rtc.setTime(3, 54, 0);
}

void loop()
{
desiredPos = calcDesiredPos();
if (desiredPos == pos) {
delay(1000);
return;
}
syncTime();

if (desiredPos == POS_BOT) {
runMotor(false);
while (!reachedBot()) {
delay(50);
} 
stopMotor();
pos = POS_BOT;
} else if (desiredPos == POS_TOP) {
runMotor(true);
while (!reachedTop()) {
delay(50);
} 
stopMotor();
pos = POS_TOP;
} 


}

void runMotor(boolean dir) {
digitalWrite(MOTOR_A_IN1_PIN, dir ? HIGH : LOW); 
digitalWrite(MOTOR_A_IN2_PIN, dir ? LOW : HIGH); 
digitalWrite(MOTOR_A_EN_PIN, MOTOR_A_SPEED);
}

void stopMotor() {
digitalWrite(MOTOR_A_IN1_PIN, LOW);
digitalWrite(MOTOR_A_IN2_PIN, LOW);
digitalWrite(MOTOR_A_EN_PIN, 0);
}

boolean reachedTop() {
return digitalRead(TOP_BUT_PIN) == LOW;
}

boolean reachedBot() {
return digitalRead(BOT_BUT_PIN) == LOW;
}

int calcDesiredPos() {
int timeNow = hour() * 60 + minute();
if (raiseTime <= timeNow && timeNow < lowerTime) {
return POS_TOP;
}
return POS_BOT;
}

void syncTime() {
setTime(rtc.getHour(), rtc.getMinute(), rtc.getSecond(),
rtc.getDay(), rtc.getMonth(), rtc.getYear());
}

I found the code here by the way. Here you can also se pictures of the wiring.

You are using the Time Library, and it can automatically sync with the rtc. You will not need to call the syncTime() function every pass of loop. The default library sync between the rtc and the time library millis() based clock is every 5 minutes. The synchronization is with this code in setup().

setSyncProvider(RTC.get);

See the time library example "TimeRTC".

Thanks for the feedback. I have altered the code and get no errors. I hope I did it correctly.

#include <Wire.h>
//include <Rtc_Pcf8563.h> // original RTC
#include <Time.h>       //original Time lib
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

 

#define POS_UNKNOWN 0
#define POS_TOP 1
#define POS_BOT 2

int MOTOR_A_EN_PIN = 6;
int MOTOR_A_IN1_PIN = 7;
int MOTOR_A_IN2_PIN = 8;
int MOTOR_A_SPEED = 255;
int TOP_BUT_PIN = 9;
int BOT_BUT_PIN = 10;

//init the real time clock
//Rtc_Pcf8563 rtc;
time_t rtc;  //added

int pos = POS_UNKNOWN;
int desiredPos = POS_BOT;

// Time represents just hour/min and time = hour * 60 + min
int raiseTime = 430; // 7:10
int lowerTime = 1130; // 18:50

//int raiseTime = 678; // 7:10
//int lowerTime = 679; // 18:50

void setup()
{

setSyncProvider(RTC.get);

Serial.begin(9600);
pinMode(MOTOR_A_EN_PIN, OUTPUT);
pinMode(MOTOR_A_IN1_PIN, OUTPUT);
pinMode(MOTOR_A_IN2_PIN, OUTPUT);

pinMode(TOP_BUT_PIN, INPUT); 
pinMode(BOT_BUT_PIN, INPUT); 

delay(1000);
//DO NOT UPDATE TIME ANYMORE
//clear out the registers
// rtc.initClock();
//set a time to start with.
//day, weekday, month, century(1=1900, 0=2000), year(0-99)
//rtc.setDate(14, 1, 1, 0, 14);
//hr, min, sec
//rtc.setTime(3, 54, 0);
}

void loop()
{
desiredPos = calcDesiredPos();
if (desiredPos == pos) {
delay(1000);
return;
}
//syncTime();

if (desiredPos == POS_BOT) {
runMotor(false);
while (!reachedBot()) {
delay(50);
} 
stopMotor();
pos = POS_BOT;
} else if (desiredPos == POS_TOP) {
runMotor(true);
while (!reachedTop()) {
delay(50);
} 
stopMotor();
pos = POS_TOP;
} 


}

void runMotor(boolean dir) {
digitalWrite(MOTOR_A_IN1_PIN, dir ? HIGH : LOW); 
digitalWrite(MOTOR_A_IN2_PIN, dir ? LOW : HIGH); 
digitalWrite(MOTOR_A_EN_PIN, MOTOR_A_SPEED);
}

void stopMotor() {
digitalWrite(MOTOR_A_IN1_PIN, LOW);
digitalWrite(MOTOR_A_IN2_PIN, LOW);
digitalWrite(MOTOR_A_EN_PIN, 0);
}

boolean reachedTop() {
return digitalRead(TOP_BUT_PIN) == LOW;
}

boolean reachedBot() {
return digitalRead(BOT_BUT_PIN) == LOW;
}

int calcDesiredPos() {
int timeNow = hour() * 60 + minute();
if (raiseTime <= timeNow && timeNow < lowerTime) {
return POS_TOP;
}
return POS_BOT;
}

//void syncTime() {
//setTime(rtc.hour(), rtc.minute(), rtc.second(),
//rtc.day(), rtc.month(), rtc.year());
//}

I don't think this is needed, it does not seem to be used anywhere:

time_t rtc;  //added

It also "smells wrong". Variables of type time_t are specific times like 10:30 or the current time. An rtc is a piece of hardware.

In setup(), you have this line:

setSyncProvider(RTC.get);

Your code does not define "RTC" anywhere, but luckily it is defined for you inside DS1307.h. Variable names in C are case sensitive, so rtc is not the same as RTC.

You really should correct the indentation in your code. Click Tools-->Auto Format

Thank you for the feedback. I will adjust my code when I am back in my home laboratory.

Br. Henrik