Blink an LED - embedded version of HelloWorld

Dependencies:   mbed

Fork of HelloWorld by Simon Ford

What Does It Do?

This code blinks the LED - the embedded version of the classic beginners' program Hello World.

/media/uploads/cputalk/led-flash2.gif

The source code can be viewed at the bottom of this page by clicking on main.cpp.

However, the easiest way to use the code is to click on Import into Compiler from the Repository Toolbox on the right, as shown in the screen capture below:
/media/uploads/cputalk/repository-toolbox-3.gif
This loads a copy of the code into your browser-based, online compiler. Click on Compile and in a few seconds your will have compiled your own Hello World!

By the way, edit and experiment as much as you want, because you cannot mess anything up - you can come back here any time and import again for a fresh start.

This code is identical to Simon Ford's /teams/mbed/code/mbed_blinky/ and ST's /teams/ST/code/Nucleo_blink_led/ except that this source contains extra comment lines.

What hardware does it work with?

Any of the official mbed platforms: https://developer.mbed.org/platforms/

Library Information

The mbed library used by this code is currently /users/mbed_official/code/mbed/rev/093f2bd7b9eb, which is revision 140 of the official mbed 2 library. Revision 140 was released on April 12, 2017. You can verify this by viewing the contents of the mbed.bld file at the bottom of this page.

The mbed library is automatically imported when you import this code into your online editor - in general you don't need to do anything special.

mbed Library Source Code

One of the coollest features of mbed is that the mbed library source code is freely available.

You can find the library source code here https://developer.mbed.org/users/mbed_official/code/mbed-dev/. This is not an easy read - if you are a beginner, you will probably find this intimidating. But, here it is, dig in!

If Your Compile Fails

If your compile fails, it's most likely that you are using brand new hardware that was not yet included when revision 140 of the mbed library was released. In that case, update to the latest library. (see below)

Updating to the Latest mbed Library

In your online compiler, click on "mbed" in your Program Workspace, as in the picture below:

/media/uploads/cputalk/mbed-lib.gif

Then, on the right side of your screen, under Library Build Details, click on Update, as seen in the picture below:

/media/uploads/cputalk/lib-build-details.gif

Notice how the Status line in the Library Build Details window (above) informs you new version available.

I will attempt to regularly update the code to use the latest mbed library version. If I fall behind, use the two simple steps described above to update it yourself!

Committer:
cputalk
Date:
Thu Mar 23 16:49:59 2017 +0000
Revision:
5:3d32131a575f
Parent:
3:868c1fb7b5b0
Child:
6:6c170453bfde
Added more comments.; Newest mbed library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cputalk 2:486a4fe519e1 1 // Elementary code to switch an LED on and off
cputalk 5:3d32131a575f 2 // If this code fails to compile, you probably have new hardware. Click on the mbed library ("mbed") and then click on "Update" to update to the latest version.
cputalk 5:3d32131a575f 3 // This code is identical to mbed_blinky, but the mbed library version for mbed_blinky is updated more regularly. However, it has less comments (see below)
cputalk 2:486a4fe519e1 4
cputalk 5:3d32131a575f 5 #include "mbed.h" // This is how you import most of the library functions on mbed
simon 0:fb6bbc10ffa0 6
cputalk 5:3d32131a575f 7 DigitalOut myled(LED1); // Set up LED1 pin as a digital output
cputalk 2:486a4fe519e1 8
cputalk 2:486a4fe519e1 9 int main() // Start of main program
simon 0:fb6bbc10ffa0 10
cputalk 2:486a4fe519e1 11 {
cputalk 2:486a4fe519e1 12
cputalk 3:868c1fb7b5b0 13 while(1) // Start an endless loop
cputalk 2:486a4fe519e1 14
cputalk 5:3d32131a575f 15 { // top of endless loop
cputalk 2:486a4fe519e1 16
cputalk 2:486a4fe519e1 17 myled = 1; // Set pin high, turns LED off
cputalk 2:486a4fe519e1 18 wait(0.2); // Wait 0.2 seconds
cputalk 2:486a4fe519e1 19
cputalk 2:486a4fe519e1 20 myled = 0; // Set pin low, turns LED on
cputalk 2:486a4fe519e1 21 wait(0.2); // Wait 0.2 seconds
cputalk 2:486a4fe519e1 22
cputalk 3:868c1fb7b5b0 23 } // bottom of endless loop
cputalk 2:486a4fe519e1 24
cputalk 2:486a4fe519e1 25 } // end of main program