In the previous blog, I showed how to get all the dependencies required to be able to use Mbed with the Arduino IDE (if you haven’t read that yet, click here). In this blog I will be showing how to get into programming with a few examples. Let’s get started!
Simple Blink Example
First, let’s rewrite the the Blink example (which could be called the “Hello World” of Arduino programming) using Mbed functions instead of the regular Arduino functions. For reference, here is the regular Blink example-
Start by opening a new sketch and including the Mbed header file. Then, type in the following code before the setup, which creates an object of the DigitalOut class to control the state of pin LED1. I have named it led but you can use any valid identifier name. LED1 refers to the pin name of the built in led of your Arduino (pin 13 in this case). A list of all the pin numbers and their corresponding Mbed pin names is given at the end of this blog for reference.
Next, enter the following code in the loop, which sets the state of led to 1 (or HIGH), waits for 1 second, sets it to 0 (or LOW), then waiting for another second, with everything repeating indefinitely. Setup can be left empty.
The completed code should now look like this-
Finally, compile and upload the code. You should start to see the built in LED of your Arduino blink. You can even try playing around with the wait values and see the differences in the output, to really make sure it is working.
In this example, we will spawn two threads which can perform multiple tasks simultaneously, giving the effect of multitasking.
Once again, start by including the Mbed header file and the Mbed namespace, but this time also include the rtos namespace, under which the thread class and all its functions are present. Now, create two threads as shown-
I have named the threads t1 and t2, however you can use any valid identifier name. These threads will require code to execute, so create two functions to pass to them. I have named the functions func1 and func2. These are the thread functions which our two threads will execute respectively. Remember that these functions must not require any parameter or return any value.
In the above example, the func1 and func2 print “A” and “B” and wait for 2 and 3 seconds respectively. To make them work indefinitely, while(1) loops have been used.
Finally, in the setup, begin Serial communication and start the threads by passing them the functions as parameters. The main loop function can be kept empty as the two threads will execute some code.
Here is how the completed code should look-
Now, compile and upload the program and open the Serial monitor. You should see the letters A and B being printed onto the Serial monitor asynchronously.
And that’s it, you’re all done!
If you found this blog useful, I would love to hear your opinions/suggestions in the Comments below.
Here are the pins mappings from Arduino to Mbed-
|Arduino Pins||Corresponding Mbed name|
|D0/TX||P1_3 (not recommended for use)|
|D1/RX||P1_10 (not recommended for use)|
|D13/Built in LED||P0_13|