Hi, it's me again,
So want to press button to close relay and then it opens after fixed time or I press button again, whichever comes first. I can do the time, but I can't figure how to interrupt or halt the delay prematurely with a second button press.
I have tried replacing
int stayON = 10000;
delay(stayON);
with
int stayON = 100;
int var = 0;
while(var < 100 && stateButton == LOW)
{
{
delay(stayON);
}
var++;
}
trying to break delay down into smaller increments and make it so if I press button then the while stops. It works fine if I don't have any stateButton evaluations, but as soon as I add the button eval it just blinks when I press button but relay doesn't ever close. I have tried a bazillion different ways of doing this with no luck and it's the only concept I have found, that I understand anyway.
Any help would be appreciated, thank you!
Full unmodified code below:
| int pinButton = 10; | ||
| int Relay = 11; | ||
| int stateRelay = LOW; | ||
| int stateButton; | ||
| int previous = LOW; | ||
| long time = 0; | ||
| long debounce = 500; | ||
| int stayON = 10000; | ||
| void setup() { | ||
| pinMode(pinButton, INPUT); | ||
| pinMode(Relay, OUTPUT); | ||
| Serial.begin(9600); | ||
| } | ||
| void loop() | ||
| { | ||
| stateButton = digitalRead(pinButton); | ||
| if(stateButton == HIGH && previous == LOW && millis() - time > debounce) | ||
| { | ||
| if(stateRelay == HIGH) | ||
| { | ||
| digitalWrite(Relay, LOW); | ||
| } | ||
| else | ||
| { | ||
| digitalWrite(Relay, HIGH); | ||
| Serial.print(1); | ||
| delay(stayON); | ||
| digitalWrite(Relay, LOW); | ||
| Serial.print(2); | ||
| } | ||
| time = millis(); | ||
| } | ||
| previous == stateButton; | ||
| } |