Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
thread for LEDs in NXP LPC1768
I am trying to make a sequence of flickerings in 4 LEDs by setting a 5 minutes flicker each LED, one by one of the LEDs with 3 minutes pauses of no light in between them, and then finish. I am trying to use the Thread function for this, I updated the library by deleting the old one, that solved me a problem.
but I think I am doing some wrong interpretation/writing in the sequence of the programming lines as it is blinking at first, but then is staying ON instead of OFF in the 3 minutes pause.
How can i turn off the LED?
Also, is ti possible to make 2 LEDs to turn on at the same time in the same thread?
Any suggestions? Or a different way to do this sequence of flickerins?
Thanks
Carlos
1 Answer
8 years, 10 months ago.
No need to use a thread or even any interrupts etc... If you don't need to also do something else at the same time you can do that very simply using waits.
#include "mbed.h" // times in seconds #define blinkTime 0.25 #define onTime (5*60) #define offTime (3*60) #define onCycles (onTime / (blinkTime*2)) BusOut leds(LED1,LED2,LED3,LED4); main () { int led = 0; leds = 0; while (true) { ledTimer.reset(); unsigned int cycles = onCycles; while (cycles > 0) { leds[led] = 1; wait(blinkTime); leds[led] = 0; wait(blinkTime); cycles-- } wait (offTime); led++; if (led==4) led =0; } }
You could create a more elegant solution using timers or interrupts which would leave the CPU idle most of the time and able to process other things but if you don't need that then why bother when the simple approach works fine.
That is great to understand, I will give it a try and just in case I will leave the post open. Thanks again Andy!!!! you helped me with my previous use on this mbed board, I appreciatte a lot your time!!
posted by 03 Feb 2016Hello Andy: By trying to make a short try out with 2 leds and shorter time, it shows me an error in the Timer part that I can not find the libraries in the website...is it that is not #defined in the top section that the Timer is called "ledTimer"? I tried by adding that too and keeps sending me the message that I need to add an space in the expression. Below I am including my modifications for the example.
- include "mbed.h"
times in seconds
- define blinkTime 0.25
- define onTime (10)
- define offTime (10)
- define onCycles (onTime / (blinkTime*2))
BusOut leds(LED1,LED2,LED3,LED4);
main () { int led1 = 0; leds = 0;
while (true) {
ledTimer.reset();
unsigned int cycles = onCycles;
while (cycles > 0) {
leds[led1] = 1;
wait(1);
leds[led1] = 0;
wait(1);
cycles;
}
wait (offTime);
led1++;
if (leds==4)
leds =0;
int led2 = 0; leds = 0;
while (cycles > 0) {
leds[led2] = 1;
wait(1);
leds[led2] = 0;
wait(1);
cycles;
}
wait (offTime);
led2++;
if (leds==4)
leds =0;
}
}
In my original code the line ledTimer.reset(); should be deleted. I changed my mind half way through entering that code and forgot to delete that line. It looks like I also missed a ; on the end of line 24. Other than those two changes I've tested it and it compiles and runs without any errors. If you are getting a missing library error make sure your project includes the mbed library.
I'm not quite sure what your modifications are trying to do, they will result in only the first LED flashing since led1 and led2 are both 0 and are only changed after you have finished using them. The code I gave will already flash all 4 LEDs in turn, the value of the variable led sets which LED to flash and increases each time around the loop. If you only want the first two leds change
if (led==4) led =0;
to
if (led==2) led =0;
posted by 04 Feb 2016