loop and LEDs

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi all!

I just wana know if it is possible to run 2 loops at the same time?:D

Also, assume you want 2 LEDs to flash at the same time BUT with different frequencies...
example: LED1 every 1 second and LED2 every 2 second.

Here's the algorithm i came up with:

loop

LED1 on
LED2 on
delay 0.5 second
LED1 off
delay 0.5 second
LED2 off
LED1 on
delay 0.5 second
LED1 off
delay 0.5 second
goto loop


Does it make any sense? this is just an algorithm...no need to write proper codes....

Your replies will be welcome,
 
Last edited:

praondevou

Joined Jul 9, 2011
2,942
loop

LED1 on
LED2 on
delay 0.5 second
LED1 off
delay 0.5 second
LED2 off
LED1 on
delay 0.5 second
LED1 off
delay 0.5 second
goto loop

What language are u using? You can do it like this, however unless you use interrupts to get out off the 0.5 second delays, the MC won't do anything else during this time, just waiting, right? Makes only sense if the MC is supposed to do ONLY this.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Oh to your other question, YES the microcontroller will only do this BUT that will be controlled by one pushbutton.
 

ErnieM

Joined Apr 24, 2011
8,377
Your simple simple algorithm (hey, you can call that pseudo code ya know;)) would work.

There is no real need to start doing interrupts or using the timers unless you require very accurate times. Depending on how fast you want the button to work (meaning if you want the button the LEDs light as soon as you hit the switch, and as soon as you release the switch the LEDs go dark) you can just check that switch inside any delay routine you write.

Break it down in steps:
first get LEDs to light,
then get them to light in the delay pattern,
then add the button to turn it all on and off.

Makes for less craziness when you debug your code in simple stages.
 
Top