Hi there,
i am a complete novice so bear with me.
i want to make a hydroponic system and use a relay to swtich on pumps, fans, light etc thats the plan anyway.
so far i have managed to connect a Tiny RTC and altered some code i found to switch a relay on and off at 1 certain time a day. it works great when plugged in to the computer and also stadalone so i know i have the basics right, now the thing is, would i be able to add more on and off times to this code? or is there a better way i could do it? some code some one has already done?
i am totally new to all this sort of thing. so basically all i need to do is to be able to control 4 relays at certain times of the day to switch on and off.
i used a piece of code to control an LED switching on at set times and just changed the code a bit to enable the relay. beleive me when i say that is a major thing for me ha ha!
heres the code i am using:
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
#define RELAY1 6 // Set our relay pin
byte startHour = 10; // Set our start and end times
byte startMinute = 59; // Don't add leading zero to hour or minute - 7, not 07
byte endHour = 11; // Use 24h format for the hour, without leading zero
byte endMinute = 3;
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
void setup () {
pinMode(RELAY1, OUTPUT); // Set our LED as an output pin
digitalWrite(RELAY1, HIGH); // Set the LED 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 lights currently powered on
checkStartTime(); // If they are not, check if it's time to turn them on
} else {
checkEndTime(); // Otherwise, check if it's time to turn them off
}
if (validStart == 1) { // If our timer is flagged to start, turn the lights on
turnLightsOn();
}
if (validEnd == 1) { // If our time is flagged to end, turn the lights off
turnLightsOff();
}
}
delay(1000);
}
byte checkStartTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() == startHour && now.minute() == startMinute) {
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 lights yet
}
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) {
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 lights yet
}
return validEnd; // Return the status for powering off
}
void turnLightsOn() {
digitalWrite(RELAY1, LOW); // Turn on the relay
}
void turnLightsOff() {
digitalWrite(RELAY1, HIGH); // Turn off the relay
}
any help would be much appreciated and hopefully easy enough for me to follow, thanks in advance
rich