Hi,
I'm trying to control the water heater for my pool with the arduino, so it can turn on and turn off the water valve and the power supply certain hours during the day. So far the code works,in the morning, around 8am, Arduino turns on the relays that control the valve and the power supply and it turns them off around 9pm at night. My problem is when the arduino loses power or resets. When it loses power or resets, it always comes back with the relays on. That's ok if it loses power during the day (when my relays are supposed to be on). But,when it loses power during the night (my relays are supposed to be off), it turns them relays on.
This is the code that i'm using.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define ANCHO 128
#define ALTO 64
#define OLED_RESET 4
Adafruit_SSD1306 oled(ANCHO, ALTO, &Wire, OLED_RESET);
#include <DS3231.h>
int Relay1=8;
int Relay2=7;
DS3231 rtc(SDA,SCL);
const int OnHourR1=8;
const int OnMinR1=00;
const int OffHourR1=21;
const int OffMinR1=05;
const int OnHourR2=8;
const int OnMinR2=02;
const int OffHourR2=21;
const int OffMinR2=00;
Time t;
void setup() {
Wire.begin ();
oled.begin (SSD1306_SWITCHCAPVCC, 0x3C);
rtc.begin();
// The following lines can be uncommented to set the date and time
//rtc.setDOW(TUESDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(21, 05, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(5, 12, 2020); // Set the date to January 1st, 2014
pinMode (Relay1,OUTPUT);
pinMode (Relay2,OUTPUT);
//if( OnHourR1 <= t.hour <= OffHourR1){
// digitalWrite (Relay2,LOW);
// digitalWrite (Relay1,LOW);
//}
// else if (OnHourR1 > t.hour & t.hour > OffHourR1) {
// digitalWrite (Relay1,HIGH);
// digitalWrite (Relay2,HIGH);
//}
}
void loop() {
t=rtc.getTime();
oled.clearDisplay();
oled.setTextColor(WHITE);
oled.setTextSize(2);
oled.setCursor(10, 16);
oled.print(rtc.getTimeStr());
oled.setCursor(0, 0);
oled.print(rtc.getDateStr());
oled.display();
if(t.hour == OnHourR1 & t.min == OnMinR1){
digitalWrite (Relay1,LOW);
oled.setCursor(0, 32);
oled.print("Timer On");
oled.display();
}
else if(t.hour==OffHourR1 & t.min== OffMinR1){
digitalWrite (Relay1,HIGH);
oled.setCursor(0, 32);
oled.print("Timer Off");
oled.display();
}
if(t.hour == OnHourR2 & t.min == OnMinR2){
digitalWrite (Relay2,LOW);
oled.setCursor(0, 48);
oled.print("Valve On");
oled.display();
}
else if(t.hour==OffHourR2 & t.min== OffMinR2 ){
digitalWrite (Relay2,HIGH);
oled.setCursor(0, 48);
oled.print("Valve Off");
oled.display();
}
}
I thought putting an IF statement in the Void Setup, so when it resets it can check if it is between the hours that is supposed to be on or off, but i wasn't successful.
//if( OnHourR1 <= t.hour <= OffHourR1){
// digitalWrite (Relay2,LOW);
// digitalWrite (Relay1,LOW);
//}
// else if (OnHourR1 > t.hour & t.hour > OffHourR1) {
// digitalWrite (Relay1,HIGH);
// digitalWrite (Relay2,HIGH);
//}
I'm pretty new to coding in arduino, i would appreciate the help.
Thank in advance.