Toggling on/off of flashing LED loop

Hoping someone can point my brain in the right direction from a high level perspective. I'm teaching my son the basics and I think I may have cornered myself in my attempt to expand on his desire to learn by building upon his questions. We started off with the standard 'blinky' LED, then he asked about a second LED and alternating flashing. No problem there and I was happy to follow along to keep him interested. Then the next question was how can I add a button to turn the flashing LEDs completely on or off. I thought no problem but quickly ran into the issue that when the LEDs flash the flash sequence has to run in a loop continuously even while scanning for button changes of state but I must escape the flashing loop when they are suppose to be off. I am beginning to think that I may have breached the basics and jump a bit to far ahead. But I think he can understand if I can wrap my head around it and explain it to him.

My question is from a high level perspective (outline) how would I go about accomplishing scanning for button state change while at the same time keeping the flashing loop running while doing so and turning off the flashing loop when its suppose to be off?

I looked at last state variable, but the last state of the button will always be a HIGH to trigger a change of on or off.

Maybe interrupts for the button? Is there any such thing as threading for the flashing loop?

Thanks
:slight_smile:

Have you read the discussion by Robin2?
http://forum.arduino.cc/index.php?topic=223286.0

Always show us your sketch so we can see your progress.
Use the </> icon in the posting menu to attach your code.

I can't provide any specifics since no code was provided. However...

The first thing to do is figure out how to make LEDs blink alternately WITHOUT using delay(...). Use millis(...) instead.

millis(...) lets you compare the current time with the time that something should happen, and lets you do other things. delay(...) prevents you from doing anything else (like scanning buttons) while it is in effect.

Look for the "Blink Without Delay" example, also known as BWOD.

Also see Demonstration code for several things at the same time - Project Guidance - Arduino Forum