Hello peeps,
I have written my first Arduino project!
The setup is a basic dual timer for opening and closing the door on my chicken coop. So they chooks can be closed in safe at night (22:00/01 and let out in the morning (07:00/01)
Works 'perfectly' (ran for a week) on my bench, without the relay linked up to the actuator but when I link it up to the actuator it seems to get stuck either open or closed. ![]()
I did have lots of serial printing when I was testing, to be sure everything was working on my bench but I took it out for running outside as it is powered from a car battery not a USB linked to a computer that can read it.
Using an Uno, DS3231, 2 Relay module and two override push to make mini buttons.
2-Relay module is used to switch between neg/pos feed for the actuator, to change direction from Open to Close.
The relay module jumper is set to VCC/VCC (not GND) which works on the bench.
In my eyes it is very VERY simple code. (Could be better, with more global vars etc but I wrote it pretty quickly)
Still setup on a breadboard.
#include <Wire.h>
#include <DS3231.h>
#define DS3231_I2C_ADDRESS 0x68
DS3231 rtc(SDA, SCL);
/*
* GND - GND
* VCC - 5v
* SDA - SDA
* SCL - SCL
*/
#define RELAY1 2
#define RELAY2 3
byte bHour; //current hour
byte bMin; //current min
byte bSecond; //current second
byte bMonth; //current Month
byte bDate; //current Date
byte bDay; //current Date
byte bYear; //current Year
int dOpen = 1;
int dClose = 0;
#define BUTTONOp 8 // Button
#define BUTTONCl 9 // Button
void setup()
{
// Initialize the rtc object
rtc.begin();
readTime(&bSecond, &bMin, &bHour, &bDay, &bDate, &bMonth, &bYear);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
pinMode(BUTTONOp, INPUT); digitalWrite(BUTTONOp, HIGH);
pinMode(BUTTONCl, INPUT); digitalWrite(BUTTONCl, HIGH);
/*
// The following lines can be uncommented to set the date and time
rtc.setDOW(WEDNESDAY); // Set Day-of-Week
rtc.setTime(15, 23, 0); // Set the time (24hr format)
rtc.setDate(10, 7, 2016); // Set the date
*/
}
void loop()
{
readTime(&bSecond, &bMin, &bHour, &bDay, &bDate, &bMonth, &bYear);
if (digitalRead(BUTTONOp) == 0)
{
//Open Door button
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,HIGH);
}
else if (digitalRead(BUTTONCl) == 0)
{
//Close Door button
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,LOW);
}
else if (bHour == 7 && (bMin == 0 || bMin == 1))
{
//Open Door timer
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,HIGH);
}
else if (bHour == 22 && (bMin == 0 || bMin == 1))
{
//Close door timer
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,LOW);
}
else
{
//Stop
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
}
}
void readTime(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
Can anyone tell me if they can see anything in the code that would cause this ... and/or recommend a better/different way for me to write the code?
Looking forward to your comments. ![]()
I know it's messy but I am only breadboarding at the moment....
The Open relay is ON, the code dictates it should be all off (in the "else" of the if statement)


To recap, the buttons work, as does the timer.
Sometimes, when using the buttons, the relay stays on. Either open or closed.
And MOST times the same thing happens when the timer triggers an open or close.
Reset the arduino and it works perfectly.
Run it on the bench, from a USB, without the actuator attached to the relay, it works perfectly.
Really struggling to work out what this is... can anyone help?








