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());
}