const int relayFogPin = 10; // pin that the relay is connected to relay for sprayer
const int buttonPin = 2; // pushbutton pin
int repeat = 0;
int buttonState = 0;
const long fogTime = 1 * 1000UL; // duration to keep the relay active, in milliseconds
const long fanTime = 5 * 60 * 1000UL;
const long interval = 10 * 1000UL; // Total Duration of cycle
unsigned long startTime; // variable to store the start time
unsigned long timeprint; // variable to store the start time
const long interprint = 2000; // interval at which the values are printed in serial monitor, use for debugging only
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //comment/uncommemt
pinMode(relayFogPin, OUTPUT);
//pinMode(relayFanPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buttonPin, INPUT);
startTime = millis(); // store the current time as the start time
timeprint = millis();
//1st run at startup
repeat++;
digitalWrite(relayFogPin, LOW); // activate the spray relay
//digitalWrite(relayFanPin, LOW); // activate the fan relay
digitalWrite(LED_BUILTIN, HIGH); //Switch onboard LED ON
delay(fogTime); // keep the spray relay active for the specified duration
digitalWrite(relayFogPin, HIGH); // deactivate the spray relay
//delay(fanTime - fogTime); // 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() {
// put your main code here, to run repeatedly:
if (millis() - startTime >= interval) { // check if an hour has passed
startTime += interval; // increment the start time by an hour
repeat ++;
if (repeat <=13){
digitalWrite(relayFogPin, LOW); // activate the spray relay
//digitalWrite(relayFanPin, LOW); // activate the fan relay
digitalWrite(LED_BUILTIN, HIGH); //Switch onboard LED ON
delay(fogTime); // keep the spray relay active for the specified duration
digitalWrite(relayFogPin, HIGH); // deactivate the spray relay
//delay(fanTime - fogTime); // 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
}}
if(repeat >=24){
repeat = 0;}
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(relayFogPin, LOW); // activate the spray relay
//digitalWrite(relayFanPin, LOW); // activate the fan relay
digitalWrite(LED_BUILTIN, HIGH); //Switch onboard LED ON
delay(fogTime); // keep the spray relay active for the specified duration
digitalWrite(relayFogPin, HIGH); // deactivate the spray relay
//delay(fanTime - fogTime); // 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
}
if (millis() - timeprint >= interprint) {
timeprint += interprint;
Serial.print("Interval: ");
Serial.println(interval);
Serial.print("Duration Fog: ");
Serial.println(fogTime);
Serial.print("Duration fan: ");
Serial.println(fanTime);
Serial.println(fanTime-fogTime);
Serial.print("Fog Relay Pin: ");
Serial.println(relayFogPin);
Serial.print("Rep: ");
Serial.println(repeat);
Serial.println(millis());
Serial.println();}
}