Pages: [1]   Go Down
Author Topic: Looping LED's - need advice on Delay command vs alternatives.  (Read 353 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

http://www.dave-auld.net/index.php?option=com_content&view=article&id=107:arduino-interrupts&catid=53:arduino-input-output-basics&Itemid=107

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.
Logged

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1202
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: