8 years, 2 months ago.

Flash led witout wait

I am trying to figure out how to write code where i flash an led without using wait to create a pause. I have done this on the arduino side using code that looks like the code in the link attached. Is this possible, obviously i want to do something else with it but i want to start easy.

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

3 Answers

8 years, 2 months ago.

Hi Tyler,

Thanks for your question, i'd be happy to help :)

What you basically are asking for is to allow other processes to run and not halt to flash an LED. Basically an RTOS (Real time operating system).

This is built right into mbed-os 5.0.

Have a look at the mbed-os-example-blinky.

https://developer.mbed.org/compiler/#import:https://developer.mbed.org/teams/mbed-os-examples/code/mbed-os-example-blinky

Here it uses Thread::wait(), instead of a delay other processes can be scheduled.

Have a read of the documentation on Wait() as well:

OS delay

When you call wait the CPU of your mbed will be busy spinning in a loop waiting for the required time to pass. Using the mbed RTOS you can make a call to Thread::wait instead. In this way the OS scheduler will put the current thread in waiting state and it will allow another thread to execute, or even better, if there are not other threads in ready state, it can put the whole microcontroller to sleep saving energy.

https://developer.mbed.org/handbook/Wait

Let me know if there's anything else i can do.

Regards,

Andrea, team mbed

Accepted Answer

Thank you for the info i think this will best fit what i am trying to accomplish measuring multiple sensor at a controlled rate. I want to refresh each sensor at a time interval and have them of set from each other.

So i just want to make sure i understand the thread function correct. When using thread::wait() it is in lamens terms setting an egg timer and continuing on till that timer go's off? When the timer goes off does it stop what it is doing and go back or does it complete its current function then goes back to the function with the wait timer that has expired and continue to process?

posted by Tyler Harris 12 Sep 2016

Pretty much :).

Read about RTOS for more insight :)

posted by Andrea Corrado 13 Sep 2016
8 years, 2 months ago.

If you don't want to use an RTOS then you can use the Ticker object. This will call a function at a fixed rate, it is using a timer interrupt in the background.

#include "mbed.h"  // include the library

DigitalOut led(LED1); // define the LED pin
Ticker myTick;            // create a ticker object

void onTick(void) {     // function to call every tick
  led = !led;                 //  toggle the LED
}


main () { 
  myTick.attach(&onTick,0.5);  // attach the onTick function to the ticker at a period of 0.5 seconds
  while(1) {                               // do whatever else you want to do.
    }
  myTick.detach();                    // stop the LED flashing.
}

8 years, 2 months ago.

And if you really want to slim it down you can use a hardware function called a PWM output to toggle the LED. This is a "set it and forget it" interface - in other words it does all the work without intervention. Then you can modify its flash rate and and enable/disable it via calls to the PWM api in your main code. No threads, tickers needed.

Thanks for the info but this wouldn't really work for what i am trying to accomplish. i was just using the led no wait as a example.

posted by Tyler Harris 12 Sep 2016