This program demonstrates serial communications and digital output. I have a similar program for the Arduino Uno. The purpose of this program is to compare mbed and Arduino code.

Fork of Serial_ex_2 by mbed_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*
00003     Serial2RGB main by C. S. Tritt, Last revised 3/18/17 (v. 1.1)
00004     
00005     Toggles RGB LED junctions in response to serial input. Echos input.
00006     
00007     Suggested wiring...
00008     
00009     Common Anode LED (active low)
00010     
00011                            /-- 1 kΩ -- D9 (red)
00012     + 3.3 to 5.0 V ----LED<--- 680 Ω -- D10 (green)
00013                            \-- 680 Ω -- D11 (blue)
00014     
00015     Common Cathode LED (active high)
00016     
00017                 /-- 1 kΩ -- D9 (red)
00018     GND ----LED<--- 680 Ω -- D10 (green)
00019                 \-- 680 Ω -- D11 (blue)
00020    
00021 */
00022 DigitalOut RedLED(D9); // Physically same as Arduino Digital pin 9 on Nucleos.
00023 DigitalOut GrnLED(D10); // Physically same as Arduino Digital pin 10.
00024 DigitalOut BluLED(D11); // Physically same as Arduino Digital pin 11.
00025 
00026 Serial pc(USBTX, USBRX); // Default settings are 9600 Baud, 8-N-1.
00027 
00028 int main() {
00029     
00030     RedLED = 0;
00031     GrnLED = 0;
00032     BluLED = 0;
00033     char letter;
00034     
00035     while(true) {
00036         if (pc.readable()) {
00037             letter = pc.getc();
00038             pc.putc(letter);
00039             if (letter == 'r') {
00040                 RedLED = !RedLED;
00041             }
00042             else if (letter == 'g') {
00043                 GrnLED = !GrnLED;
00044             }       
00045             else if (letter == 'b') {
00046                 BluLED = !BluLED;
00047             }       
00048         }
00049     }
00050 }