button to trigger a relay sequence, press button again new sequence plays

Hello,

I’m looking for some help with my code I have for this project: My goal is to have a push button activate a relay board (5v activation, 250v 10A relay). When the button is pressed relay 1 should go on for a split second, and then the program will wait for the button to be pressed again and then relay 2 will go on for a second. Next time the button is pressed it should go back to relay 1.

I currently have a code that works but it has glitches, sometimes it switches between relay 1 and relay 2 as it should but sometimes it will do the relays out of order, for example it could be going:
relay 1,2,1,2,1,1,1,2,2,1. It just seems to be random.

Not sure what is wrong but if anyone could lend a hand I’d appreciate it.

mp3_trigg_test.ino (996 Bytes)

Could you post a schematic of your button connections? I am curious about what your button de-bounce circuit looks like.

downsanimatronics:
for example it could be going:
relay 1,2,1,2,1,1,1,2,2,1. It just seems to be random.

The switch is not debounced. See IDE -> file/examples/digital/debounce & state change detection. You're also not using INPUT_PULLUP, so the switch input floats - which is usually undesirable.

Thank you. I switched to the INPUT_PULLUP but still getting the same issue. The wiring is correct. I can’t figure out how to apply the debounce program to my code though. Do you have any advice?

Copy/paste the debounce section of the example into your sketch - don't forget the variable declarations and setup() code. Remove the LED control stuff. Substitute your switch name for 'buttonState'. Compile and cleanup errors 'til compile is clean.