First Mbed Application
Building an Mbed Application
This will guide you through creating an Mbed application. We first will cover Mbed OS to read/write to sensors and peripherals on the Mbed Connect Cloud board.
Prerequisites
- An Mbed account - sign up here
- Install the https:developer.mbed.org/handbook/Windows-serial-configuration (for Windows PCs)
Required Hardware
- https:os.mbed.com/platforms/mbed-Connect-Cloud/
- Ethernet cable or active WiFi network
Setup
- Connect the board to your computer via the *OpenSDA* port.
- The board mounts as a mass-storage device (like a USB drive). Verify that you can see it (the drive name will be DAPLINK).
Making an mbed application
Import the example program.
Import programseoul-iot-hackathon-demo
Hackathon Repo
Printing on the LCD screen
We will add some code to print a message to the LCD screen of the shield.
Open 1_app_shield\main.h
Declare an object to instantiate the driver for the LCD screen. Under 'GLOBAL VARIABLES HERE' add the following code:
C12832 lcd(D11, D13, D6, D7, D10);
Add a function that prints a message to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void lcd_print(const char* message) { lcd.cls(); lcd.locate(0,3); lcd.printf(message); }
Invoke the previous function to print "Hello World!" to the LCD screen. Under 'MAIN CODE HERE' add the following code:
lcd_print("Hello World!");
Compile and run
- Select a target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- "Hello World!" should show on the LCD screen
Reading the potentiometer
We will add some code to read the value of the potentiometer every 100 ms and, if the measured value is not equal to the last one recorded, print the value to the LCD screen on the shield.
Open 1_app_shield\main.h
Instansiate an AnalogIn
object to read the potentiometer and an EventQueue
to schedule reading the potentiometer. Under 'GLOBAL VARIABLES HERE' add the following code:
AnalogIn pot1(A0); EventQueue queue;
Add a function that reads the potentiometer and prints the value to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_potentiometer() { static float potentiometer_val = 0; if ((float)pot1 != potentiometer_val) { potentiometer_val = (float)pot1; char val[13]; sprintf(val, "%.2f", potentiometer_val); lcd_print(val); } }
Use the event queue to schedule reading the potentiometer every 100 ms. Under 'MAIN CODE HERE' add the following code:
queue.call_every(100, read_potentiometer); while(1){ wait_ms(100); queue.dispatch(0); }
Compile and run
- Select a target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The value of the leftmost potentiometer should be shown on the LCD screen. - Twist the potentiometer back and forth to see the value update
Blinking the LED
We will add some code to start blinking the LED when we push the button (SW0 on Odin and SW2 on K64F) down. If we release the button, it will stop blinking.
Open 1_app_shield\main.h
Instansiate a DigitalOut
object to write to the LED and a DigitalIn
object to read the button input. Under 'GLOBAL VARIABLES HERE' add the following code:
DigitalOut led(D9, 1); DigitalIn button(BUTTON, PullUp);
Add two functions. blink_led
that will switch the state of the LED. And set_blink_led
that will read the joystick. If the joystick is pressed left, set_blink_led
will add an event to the queue that will invoke blink_led
every 500 ms. If the joystick is unpressed, set_blink_led
will remove blink_led
from the queue. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void blink_led() { led = !led; } void set_blink_led() { static int blink_id = NULL; // Read the button int blink = !button.read(); // If the button is pressed and the light is not currently blinking if (blink == 1 && blink_id == NULL) { // Add blinking the LED to event queue blink_id = queue.call_every(500, blink_led); } else if (blink == 0) { // Cancel the blinking event queue.cancel(blink_id); blink_id = NULL; led = 1; } }
Add set_blink_led
to the queue, so we can read the state of the button every 100 ms. Under 'MAIN CODE HERE' add the following code:
queue.call_every(100, set_blink_led);
NOTE: Make sure to add this line above the while
loop from the previous step.
Compile and run
- Select a target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The LED will blink if you push the button down.
- SW2 on K64F, SW0 on Odin
Publishing changes
- Right click the project folder
- Select "Publish"
- Write a commit message in the "Revision Commit" menu
- Press OK
- Select "Fork..." in the "Publish Repository" menu
- Write a Description
- Press OK
- Make a note of the URL for your fork. We will use it to locally clone this repository.
Please log in to post comments.