Looping LED's - need advice on Delay command vs alternatives.

Hi Guys, I need some advice before i start messing around only to find i get frustrated and have to start over or spend time chasing a dead end.

basically i want to write a sketch that lights up 8 led's individually at a set interval, say 1 sec for arguments sake, however i want to be able to change this delay by pressing a button.

e.g. Arduino is switched on and it starts to blink each led in turn at 1 second intervals, whist its doing this i press the button which would increase it to 2 second intervals, 3 second etc.

ive been reading up on the blink example and the blink no delay example, but im not sure exactly which one would be fine to use.

Can i use the delay function to provide the 1/2/3.. second interval or am i taking a chance that the arduino might miss the button press whilst its in the delay function?

would this be better accomplished using an interrupt? im not 100% on the use of interupts yet but i get the feeling this would be the best method?

any advice would be appreciated on the best way to accomplish this project, at the moment i do not have any code to post (still getting to grips with the syntax) but im not looking for someone to write it for me either im looking forward to the challenge id just like to be set off in the right direction before i make a pile of mistakes.

thanks for any suggestions everyone.

While in delay() the Arduino won't be checking your button. The "Blink without delay" example is an excellent example of how to make it appear as if the Arduino is both waiting for input and doing other things. You will have a far more responsive program if you eliminate delay().

John Wasser's recommendation on "Blink without Delay" will work for you. An alternative would be, as you suggest, use an interrupt function.

They're not very hard to implement at all -- take a look at something like this:


What you could do is connect a switch to pin 2 and have an interrupt function for it that adjusts a global variable "blinkTime", then in your loop() you can just call delay(blinktime).

Good luck.

I am of the purley personal opinion that most "interrupt" code attempted by people here is totally uneccessary. Interrupts are not a beginners coding exercise. I have done various Arduino projects for a year now, and I still have not found it necessary to use an interrupt. Most of my loop() code is written with a bit care, avoiding blocking code (and certainly never a delay()! ) and usually runs at 10 or 50 Khz (ie takes 100 or 20 microseconds to loop once). See this. No interrupts, yet this handles serial input, monitors the two switches including software debounce (and some other stuff not used in this proof-of-concept - my first(!) Arduino project)

Declare an unsigned long timervariables for each LED (if you want them to turn on/off at seperate intervals). Repeat the blinkingwithoutdelay code for each of LED, each with its own variables that notes the millis() at start and if the time has elapsed to toggle state.

I wrote another explanation, it says the same as the short tutorial example, but using a differnet explanation. See if that helps you.