So… I tried to build automatic coop door with arduino by copying this guy here and because I dont have PCF8563 RTC I tried do implement code of ds1307 but I havent succceed… when I upload code motor just starts spining until i touch botom button. Hope someone can help me
This is Original code
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#include <Time.h>
#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;
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());
}
And this is my modified code
#include <Wire.h>
//#include <Rtc_Pcf8563.h>
#include "RTClib.h"
#include <TimeLib.h>
#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_DS1307 rtc;
int pos = POS_UNKNOWN;
int desiredPos = POS_BOT;
// Time represents just hour/min and time = hour * 60 + min
int raiseTime = 1378; // 7:10
int lowerTime = 1377; // 18:50
//int raiseTime = 678; // 7:10
//int lowerTime = 679; // 18:50
void setup()
{
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
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() {
float timeNow = hour() * 60 + minute();
if (raiseTime <= timeNow && timeNow < lowerTime) {
return POS_TOP;
}
return POS_BOT;
}
void syncTime() {
DateTime now = rtc.now();
setTime(hour(), minute(), second(), day(), month(), year());
setTime(now.hour(), now.minute(), now.second(),
now.day(), now.month(), now.year());
}
I have included libraries used in code
Rtc_Pcf8563-master.zip (12.9 KB)
RTClib.zip (14.5 KB)