Automatic Timer Chicken Coop Door with L298N Driver And ds1307

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)

When I upload code motor just starts spining until i touch botom button.

Well, that's what your code says to do. You calculate the desired position, which comes out to being POS_BOT. Your code says "if we desire to be at the bottom, and we are not there (bumper sensed), then run the motor". So that's what it does.

You'd find that if you uploaded your sketch during the time of day when the desired poisition is POS_TOP, it would run the motor the other way until the top button was pressed.

I mean - isn't your sketch doing exactly what it should be doing? You've told us what it does do, but not what it should do. Seems to me that everything is working exactly right.

Well you are correct but when I set desired time to go up or down it wont raise or close door at that time so thats the problem

Legend_ary:
Well you are correct but when I set desired time to go up or down it wont raise or close door at that time so thats the problem

In your code, you have this:

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

But 1378 is 10:58PM, not 7:10. And since your raise time is greater than your lower time, this condition

raiseTime <= timeNow && timeNow < lowerTime

will never be true.

Why not

// Time represents just hour/min and time = hour * 60 + min
int raiseTime = 7 * 60 + 10 ; // 7:10
int lowerTime = raiseTime + 5;  // raise for 5 minutes