Hello all,
I'm new to arduino but have managed to piece together a timer program to control some blinds.
I want to rolldown at night and rollup in the morning to conserve heat in winter.
My problem is the rolldown operation. When I upload the program to my arduino uno with Sainsmart 8 channel relay it will drive my 12vdc motor and rollup then rolldown when I set the times only a few minutes apart. When i set it for morning and evening it works every morning but never does the rolldown at 19:00 hrs. I have a RTC DS1307 connected to the arduino.
Anyway, I can't find any problem with the programming but it doesn't make sense that it works when I set times close together but not when I set what I would like it to be
Any help would be appreciated, I'm lost :~
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
int output3 = 3; // Set our output pin
int output4 = 4; // Set our output pin
//rolldown bytes
byte startHour = 19; // Set our start and end times
byte startMinute = 38; // set start minute
byte startSecond = 0; // set start second
byte endHour = 19; // Use 24h format for the hour, without leading zero
byte endMinute = 42; // set end minute
byte endSecond = 0; // Set end second
//rollup bytes
byte startHour2 = 7; // Set our start and end times
byte startMinute2 = 1; // set start minute
byte startSecond2 = 0; // set start second
byte endHour2 = 7; // Use 24h format for the hour, without leading zero
byte endMinute2 = 5; // set end minute
byte endSecond2 = 0; // Set end second
//rolldown
byte validStart = 0; // Declare and set to 0 our start flag
byte poweredOn = 0; // Declare and set to 0 our current power flag
byte validEnd = 0; // Declare and set to 0 our end flag
//rollup
byte validStart2 = 0; // Declare and set to 0 our end flag
byte poweredOn2 = 0; // Declare and set to 0 our end flag
byte validEnd2 = 0; // Declare and set to 0 our end flag
void setup () {
pinMode(output3, OUTPUT); // Set our relay as an output pin
digitalWrite(output3, LOW); // Set the output to LOW (off)
pinMode(output4, OUTPUT); // Set our output as an output pin
digitalWrite(output4, LOW); // Set the output to LOW (off)
Wire.begin(); // Start our wire and real time clock
RTC.begin();
if (! RTC.isrunning()) { // Make sure RTC is running
Serial.println("RTC is NOT running!");
//RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment to set the date and time
}
}
void loop () {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
//if (now.second() == 0) // Only process if we have ticked over to new minute
if (poweredOn == 0) { // Check if motor currently powered on
checkStartTime(); // If they are not, check if it's time to turn it on
}
else {
checkEndTime(); // Otherwise, check if it's time to stop roll
}
if (poweredOn2 == 0) { // Check if lights currently powered on
checkStartTime2(); // If they are not, check if it's time to turn on
}
else {
checkEndTime2(); // Otherwise, check if it's time to turn off
}
if (validStart == 1) { // If our timer is flagged to start, roll down
rolldown();
}
if (validEnd == 1) { // If our time is flagged to end, stop roll
stoproll();
}
// start rollup
if (validStart2 == 1) { // If our timer is flagged to start, rollup
rollup();
}
if (validEnd2 == 1) { // If our time is flagged to end, stop roll
stoproll();
}
delay(750);
}
byte checkStartTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() == startHour && now.minute() == startMinute && now.second() == startSecond) {
validStart = 1; // If our start hour and minute match the current time, set 'on' flags
poweredOn = 1;
}
else {
validStart = 0; // Otherwise we don't need to power up the motor
}
return validStart; // Return the status for powering up
}
byte checkEndTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() == endHour && now.minute() == endMinute && now.second() == endSecond) {
validEnd = 1; // If our end hour and minute match the current time, set the 'off' flags
poweredOn = 0;
}
else {
validEnd = 0; // Otherwise we don't need to power off the motor yet
}
return validEnd; // Return the status for powering off
}
byte checkStartTime2() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() == startHour2 && now.minute() == startMinute2 && now.second() == startSecond2) {
validStart2 = 1; // If our start hour and minute match the current time, set 'on' flags
poweredOn2 = 1;
}
else {
validStart2 = 0; // Otherwise we don't need to power up the motor yet
}
return validStart2; // Return the status for powering up
}
byte checkEndTime2() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() == endHour2 && now.minute() == endMinute2 && now.second() == endSecond2) {
validEnd2 = 1; // If our end hour and minute match the current time, set the 'off' flags
poweredOn2 = 0;
}
else {
validEnd2 = 0; // Otherwise we don't need to power off the motor yet
}
return validEnd2; // Return the status for powering off
}
void rolldown() {
digitalWrite(output4, HIGH);
; // roll down cover
}
void rollup() {
digitalWrite(output3, HIGH);
; // roll up cover
}
void stoproll() {
digitalWrite(output3, LOW);
; // roll up cover
digitalWrite(output4, LOW);
; // roll up cover
}