const int relaySprayPin = 10; // pin that the relay is connected to relay for sprayer
const int relayFanPin = 8; // pin that the relay is connected to relay for fan
const int buttonPinSpray = 2; // pushbutton pin
const int buttonPinFan = 4; // pushbutton pin
int repeat = 12; // Equivalent to time (hour) of start
//int buttonState = 0;
const long sprayTime = 3 * 1000UL; // duration to keep the relay active, in milliseconds
const long fanTime = 5 * 1000UL;
const long interval = 1 * 10 * 1000UL; // Total Duration of cycle
unsigned long startTime; // variable to store the start time
unsigned long startcyclespray; //variable to store each spray cycle start time
unsigned long startcyclefan; //variable to store each fan cycle start time
unsigned long currentime; // store time at each loop start
int relayspray = 0;
int relayfan = 0;
int LED = 0;
void setup() {
Serial.begin(9600); //comment/uncommemt
pinMode(relaySprayPin, OUTPUT);
pinMode(relayFanPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buttonPinSpray, INPUT);
pinMode(buttonPinFan, INPUT);
startTime = millis(); // store the current time as the start time
/*/1st run at startup
repeat++;
digitalWrite(relaySprayPin, LOW); // activate the spray relay
digitalWrite(relayFanPin, LOW); // activate the fan relay
digitalWrite(LED_BUILTIN, HIGH); //Switch onboard LED ON
delay(sprayTime); // keep the spray relay active for the specified duration
digitalWrite(relaySprayPin, HIGH); // deactivate the spray relay
delay(fanTime - sprayTime); // keep the fan relay active for the remaining specified duration after spary went OFF
digitalWrite(relayFanPin, HIGH); // deactivate the fan relay
digitalWrite(LED_BUILTIN, LOW); //Switch onboard LED OFF*/
}
void loop() {
currentime = millis();
if (currentime - startTime >= interval) { // check if an hour has passed
startTime += interval; // increment the start time by an hour
repeat ++;
Serial.print("Current Repeat #: ");
Serial.print(repeat);
Serial.println("h");
if (repeat >=7 and repeat <=20){
digitalWrite(relaySprayPin, LOW); // activate the spray relay
digitalWrite(relayFanPin, LOW); // activate the fan relay
startcyclespray = millis();
startcyclefan = millis();
relayspray = 1;
relayfan = 1;
Serial.println("Spray ON");
Serial.println("Fan ON");
} else {
Serial.println("Nothing happening");
}
}
if (currentime - startcyclespray >= sprayTime and relayspray){
digitalWrite(relaySprayPin, HIGH); // spray relay OFF
relayspray = 0;
Serial.println("Spray OFF");
}
if (currentime - startcyclefan >= fanTime and relayfan){
digitalWrite(relayFanPin, HIGH); // fan relay OFF
relayfan = 0;
Serial.println("Fan OFF");
}
if ((relayspray or relayfan) and LED == 0){
digitalWrite(LED_BUILTIN, HIGH); //Switch onboard LED ON
LED = 1;
Serial.println("On board LED ON");
}
if (relayspray == false and relayfan == false and LED){
digitalWrite(LED_BUILTIN, LOW); //Switch onboard LED OFF
LED = 0;
Serial.println("On board LED OFF");
}
if(repeat >=24){
repeat = 0;
}
if (digitalRead(buttonPinSpray) == HIGH and relayspray == 0) {
digitalWrite(relaySprayPin, LOW); // activate the spray relay
startcyclespray = millis();
relayspray = 1;
Serial.println("Spray ON");
}
if (digitalRead(buttonPinFan) == HIGH and relayfan == 0) {
digitalWrite(relayFanPin, LOW); // activate the fan relay
startcyclefan = millis();
relayfan = 1;
Serial.println("Fan ON");
}
}