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
This is the corresponding Arduino/Wiring .ino program:
/* Serial2RGB_A
Toggles RGB LED junctions based on characters received via serial connection. Echos characters sent.
Hardware Required:
Arduino (or similar board as written)
Created by Dr. C. S. Tritt
Revision History
16-07-07 v. 1.0 Initial development (on TI MSP432). 17-03-13 v. 1.1 Ported to Arduino boards. 17-03-14 v. 1.2 Added echo of read characters.
- /
Use defines for pin IDs and other constants
- define RED_PIN 9 RED_LED
- define GRN_PIN 10 GREEN_LED
- define BLU_PIN 11 BLUE_LED
- define BAUD 9600 Other settings default to 8-N-1.
char inChar; boolean redState = LOW; boolean grnState = LOW; boolean bluState = LOW;
void setup() { Initialize the pins as an output. Assumes adjacent pins. for (int led = RED_PIN; led <= BLU_PIN; led++) { pinMode(led, OUTPUT); } Initialize serial communication. Serial.begin(BAUD); Set baud rate. }
void loop() { Read serial if available. if (Serial.available() > 0) { get incoming byte: inChar = (char) Serial.read(); Read and convert a value. Serial.print(inChar); Echo what was read. Use it to toggle outputs (LED states) if (inChar == 'r') { redState = !redState; digitalWrite(RED_PIN, redState); } else if (inChar == 'g') { grnState = !grnState; digitalWrite(GRN_PIN, grnState); } else if (inChar == 'b') { bluState = !bluState; digitalWrite(BLU_PIN, bluState); } } }
main.cpp@3:cbacf69dbed5, 2017-03-18 (annotated)
- Committer:
- CSTritt
- Date:
- Sat Mar 18 10:02:26 2017 +0000
- Revision:
- 3:cbacf69dbed5
- Parent:
- 2:f91fc3b8d8f7
Cleaned up comments. Switched from 1 to true constant in while loop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:8d318218bac1 | 1 | #include "mbed.h" |
CSTritt | 1:fdb8ecdf954f | 2 | /* |
CSTritt | 3:cbacf69dbed5 | 3 | Serial2RGB main by C. S. Tritt, Last revised 3/18/17 (v. 1.1) |
CSTritt | 2:f91fc3b8d8f7 | 4 | |
CSTritt | 3:cbacf69dbed5 | 5 | Toggles RGB LED junctions in response to serial input. Echos input. |
CSTritt | 2:f91fc3b8d8f7 | 6 | |
CSTritt | 2:f91fc3b8d8f7 | 7 | Suggested wiring... |
CSTritt | 2:f91fc3b8d8f7 | 8 | |
CSTritt | 2:f91fc3b8d8f7 | 9 | Common Anode LED (active low) |
CSTritt | 2:f91fc3b8d8f7 | 10 | |
CSTritt | 2:f91fc3b8d8f7 | 11 | /-- 1 kΩ -- D9 (red) |
CSTritt | 2:f91fc3b8d8f7 | 12 | + 3.3 to 5.0 V ----LED<--- 680 Ω -- D10 (green) |
CSTritt | 2:f91fc3b8d8f7 | 13 | \-- 680 Ω -- D11 (blue) |
CSTritt | 2:f91fc3b8d8f7 | 14 | |
CSTritt | 2:f91fc3b8d8f7 | 15 | Common Cathode LED (active high) |
CSTritt | 2:f91fc3b8d8f7 | 16 | |
CSTritt | 2:f91fc3b8d8f7 | 17 | /-- 1 kΩ -- D9 (red) |
CSTritt | 2:f91fc3b8d8f7 | 18 | GND ----LED<--- 680 Ω -- D10 (green) |
CSTritt | 2:f91fc3b8d8f7 | 19 | \-- 680 Ω -- D11 (blue) |
CSTritt | 2:f91fc3b8d8f7 | 20 | |
CSTritt | 1:fdb8ecdf954f | 21 | */ |
CSTritt | 3:cbacf69dbed5 | 22 | DigitalOut RedLED(D9); // Physically same as Arduino Digital pin 9 on Nucleos. |
CSTritt | 1:fdb8ecdf954f | 23 | DigitalOut GrnLED(D10); // Physically same as Arduino Digital pin 10. |
CSTritt | 1:fdb8ecdf954f | 24 | DigitalOut BluLED(D11); // Physically same as Arduino Digital pin 11. |
CSTritt | 1:fdb8ecdf954f | 25 | |
CSTritt | 1:fdb8ecdf954f | 26 | Serial pc(USBTX, USBRX); // Default settings are 9600 Baud, 8-N-1. |
mab5449 | 0:8d318218bac1 | 27 | |
mab5449 | 0:8d318218bac1 | 28 | int main() { |
CSTritt | 1:fdb8ecdf954f | 29 | |
CSTritt | 1:fdb8ecdf954f | 30 | RedLED = 0; |
CSTritt | 1:fdb8ecdf954f | 31 | GrnLED = 0; |
CSTritt | 1:fdb8ecdf954f | 32 | BluLED = 0; |
CSTritt | 1:fdb8ecdf954f | 33 | char letter; |
CSTritt | 1:fdb8ecdf954f | 34 | |
CSTritt | 3:cbacf69dbed5 | 35 | while(true) { |
CSTritt | 1:fdb8ecdf954f | 36 | if (pc.readable()) { |
CSTritt | 1:fdb8ecdf954f | 37 | letter = pc.getc(); |
CSTritt | 1:fdb8ecdf954f | 38 | pc.putc(letter); |
CSTritt | 1:fdb8ecdf954f | 39 | if (letter == 'r') { |
CSTritt | 1:fdb8ecdf954f | 40 | RedLED = !RedLED; |
CSTritt | 1:fdb8ecdf954f | 41 | } |
CSTritt | 1:fdb8ecdf954f | 42 | else if (letter == 'g') { |
CSTritt | 1:fdb8ecdf954f | 43 | GrnLED = !GrnLED; |
CSTritt | 1:fdb8ecdf954f | 44 | } |
CSTritt | 1:fdb8ecdf954f | 45 | else if (letter == 'b') { |
CSTritt | 1:fdb8ecdf954f | 46 | BluLED = !BluLED; |
CSTritt | 1:fdb8ecdf954f | 47 | } |
mab5449 | 0:8d318218bac1 | 48 | } |
mab5449 | 0:8d318218bac1 | 49 | } |
CSTritt | 3:cbacf69dbed5 | 50 | } |