Button interupt

Hello all,

I have just started with Arduino this week. Doing lots of reading and research, its alot to take in all at once. But if I remember 1 thing each time I do something, I'll be good to go.

What i am working on now is controlling 2 or more relays that shut off at separate times. IE both relays turn on at once, relay 1 turns off after 5 seconds, 2nd relay turns off after 20 seconds.

How can I get the loop to restart if there is a button press in between the 1st relay off and the 2nd relay off.

int pinButton = 12;
int Relay = 2;
int Relay2 = 3;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
int stayON = 5000; //stay on for 5000 ms
int stayON2 = 15000;

void setup() {
pinMode(pinButton, INPUT);
pinMode(Relay, OUTPUT);
}

void loop() {
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateRelay == HIGH){
digitalWrite(Relay, LOW);
digitalWrite(Relay2, LOW);
} else {
digitalWrite(Relay, HIGH);
digitalWrite(Relay2, HIGH);
delay(stayON);
digitalWrite(Relay, LOW);
delay(stayON2);
digitalWrite(Relay2, LOW);
}
time = millis();
}
previous == stateButton;
}

Good job for a state machine, have you come across that concept in your research?

Need to get away from using delay() ... have you seen this example? Very, very important to understand:

Finally, please use code tags when posting code.

Welcome to the forum!

PS: Technically, you don't want or need an interrupt, that might sound like an applicable term but it may mean something different than what you think. Interrupt - Wikipedia

Thanks for the heads up, i was looking for that button and couldnt find it to post my code. Thank you for that.

I understand the concept behind the blinkwithoutdelay. Are you saying its better to write it that way then with the delays??

I have seen some listener codes, but I may be getting confused by it and getting my code to do the things they way i want.

Ill keep tinkering.

camaroin2010:
I understand the concept behind the blinkwithoutdelay. Are you saying its better to write it that way then with the delays??

Yes, absolutely. BTW, the millis() function returns an unsigned long, so variables like time and debounce should be of the same data type.

I have seen some listener codes, but I may be getting confused by it and getting my code to do the things they way i want.
Ill keep tinkering.

Not sure what a "listener code" might be.
Good luck, ask more questions if you get stuck.

PS: There should be a call to pinMode() for the second relay.

State machines might sound daunting but are, in principle, quite simple.

Your project has 3 states
0 - both relays off and waiting
1 - relay 1 on for a period
2 - both relays on for a period

Using switch/case seems to make state machines easier to understand, at least for me. Set a variable, let's call it state, to 0 and use the state variable in switch/case.

start of loop()
switch using state
case 0:
  if it is time for relay 1 to turn on
   turn on relay 1
   set state to 1
   note the time using millis()

case 1:
  if it is time for relay 2 to turn on
   turn on relay 2
   set state to 2
   note the time using millis()

case 2
  if it is time to turn the relays off
    turn the relays off
    set state to 0
end of switch

if  the 'abort' button is pressed
turn off both relays
set state to 0

end of loop()

All the timing must be done with millis() so that the program does not stop and wait and the 'abort' button can be tested each time through loop()