I am designing a project using relay and submersible pump(currently not connected will be at Normally Open) in esp-12f where it should turn on after 1second and turn off after 1 minute, the relay is connected to GPIO5 and powered by breadboard power supply MB102.But don't know why it turns on but won't turn off and stay as it is.. kindly guide
int passflag = 0; // to let the loop execute only once
const int relayInput = 4; // the input to the relay pin
unsigned long time1 = 0;
boolean relayStatus = false;
unsigned long turnOnDelay = 1000;
unsigned long turnOffDelay = 60000;
void setup() {
pinMode(relayInput, OUTPUT); // initialize pin as OUTPUT
digitalWrite(relayInput, HIGH);
}
void loop() {
if ((passflag==0 && relayStatus == false) && (millis() - time1 >= turnOnDelay)) {
time1 = millis();
digitalWrite(relayInput, HIGH); // turn relay on
relayStatus = !relayStatus;
passflag++;
}
else if ((passflag==0 && relayStatus == true) && (millis() - time1 >= turnOffDelay)) {
time1 = millis();
digitalWrite(relayInput, LOW); // turn relay off
relayStatus = !relayStatus;
passflag++;
}
}
passFlag never returns to zero, so neither of the if statements are going to be true. Actually i suggest to just remove passFlag all together.
Still if i would want something to be turned on for 1 second every minute, i would use the modulo 60000 of millis() and turn on if lower than 1000, else turn off.