Hi,
I am looking for some help on how to make a single button when pushed cycle through different subroutines.
For example a bike light or Christmas tree lights.
How do you press a button and have it interrupt a sequence to start the next without checking the button has been pressed at the end of each sequence?
I am a relative novice and looking for projects to run with a class of 15-17 year olds
Thanks for your advice on this.
Your sequence has to be aware of break condition if you want to break in the middle of the sequence
Yes that sounds right.
How do you go about this?
Perry Bebbington has some very good tutorials on button actions and how to use them right at the start of this forum. Both starter and advanced actions are covered. You may wish to read them. It's really good stuff.
Ron
Welcome to the fourm!
Please attach a circuit diagram, if you have one (otherwise ask us!)
@anon44338819
Hi tinkerer9 here is what I was planning to control. One press would flash red in a sequence a second could do a second sequence, third press for flashy white lights etc. I'm looking to cycle through sequences with a press but not have to wait for a full loop to complete before checking the next button press.
Thanks Ron, I'll have a look at your suggestion
I've plonked a sketch here at Tinkercad, which should get you started.
There are 3 leds with resistors, and push button wired from a pin to ground, using input pullup. (3 leds because I coded this before you posted the diagram with 2.....)
It uses a couple of important concepts:
- There's a blink without delay on the builtin led to prove that there's no blocking, and that technique is also used to blink the main leds in each of 2 states
- There are 2 states managed with switch...case. In one state the 3 leds blink together; in the other, the outer 2 leds wig-wag and the middle one is off
- Changing between states is done by checking for the button to become newly pressed.