Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@6:dcc4031025a6, 2021-09-17 (annotated)
- Committer:
- CSTritt
- Date:
- Fri Sep 17 20:47:58 2021 +0000
- Revision:
- 6:dcc4031025a6
- Parent:
- 5:c849e3c4a5f0
- Child:
- 7:a03687963ad4
Mbed v5 for 2021 week 2 lab.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:8d318218bac1 | 1 | #include "mbed.h" |
CSTritt | 6:dcc4031025a6 | 2 | |
CSTritt | 1:fdb8ecdf954f | 3 | /* |
CSTritt | 6:dcc4031025a6 | 4 | Serial2RGB main by C. S. Tritt, Last revised 9/15/21 (v. 1.2) |
CSTritt | 5:c849e3c4a5f0 | 5 | |
CSTritt | 5:c849e3c4a5f0 | 6 | Toggles RGB LED junctions in response to serial input. Note input is case |
CSTritt | 5:c849e3c4a5f0 | 7 | sensive and known "commands" are r (toggle red), g (toggle green) and b |
CSTritt | 5:c849e3c4a5f0 | 8 | toggle blue). Input (including CR/LF is echoed. |
CSTritt | 2:f91fc3b8d8f7 | 9 | |
CSTritt | 6:dcc4031025a6 | 10 | Mbed 5 or earlier (due to Serial, BufferedSerial changes). |
CSTritt | 6:dcc4031025a6 | 11 | |
CSTritt | 2:f91fc3b8d8f7 | 12 | Suggested wiring... |
CSTritt | 5:c849e3c4a5f0 | 13 | |
CSTritt | 2:f91fc3b8d8f7 | 14 | Common Anode LED (active low) |
CSTritt | 5:c849e3c4a5f0 | 15 | |
CSTritt | 5:c849e3c4a5f0 | 16 | /-- 680 kΩ -- D13 (red) |
CSTritt | 5:c849e3c4a5f0 | 17 | + 3.3 to 5.0 V ----LED<--- 680 Ω -- D14 (green) |
CSTritt | 5:c849e3c4a5f0 | 18 | \-- 1 kΩ -- D15 (blue) |
CSTritt | 5:c849e3c4a5f0 | 19 | |
CSTritt | 2:f91fc3b8d8f7 | 20 | Common Cathode LED (active high) |
CSTritt | 5:c849e3c4a5f0 | 21 | |
CSTritt | 5:c849e3c4a5f0 | 22 | /-- 680 Ω -- D13 (red) |
CSTritt | 5:c849e3c4a5f0 | 23 | GND ----LED<--- 680 Ω -- D14 (green) |
CSTritt | 5:c849e3c4a5f0 | 24 | \-- 1 kΩ -- D15 (blue) |
CSTritt | 5:c849e3c4a5f0 | 25 | |
CSTritt | 1:fdb8ecdf954f | 26 | */ |
CSTritt | 5:c849e3c4a5f0 | 27 | |
CSTritt | 5:c849e3c4a5f0 | 28 | // Construct resources. |
CSTritt | 5:c849e3c4a5f0 | 29 | |
CSTritt | 4:43ce9b92ae68 | 30 | DigitalOut RedLED(D13); // Arduino Digital pin 13 on Nucleos. |
CSTritt | 4:43ce9b92ae68 | 31 | DigitalOut GrnLED(D14); // Arduino Digital pin 14. |
CSTritt | 4:43ce9b92ae68 | 32 | DigitalOut BluLED(D15); // Arduino Digital pin 15. |
CSTritt | 1:fdb8ecdf954f | 33 | |
CSTritt | 1:fdb8ecdf954f | 34 | Serial pc(USBTX, USBRX); // Default settings are 9600 Baud, 8-N-1. |
mab5449 | 0:8d318218bac1 | 35 | |
CSTritt | 5:c849e3c4a5f0 | 36 | int main() |
CSTritt | 5:c849e3c4a5f0 | 37 | { |
CSTritt | 5:c849e3c4a5f0 | 38 | |
CSTritt | 5:c849e3c4a5f0 | 39 | RedLED = 0; // Set pins to known state (junction on if active low). |
CSTritt | 1:fdb8ecdf954f | 40 | GrnLED = 0; |
CSTritt | 1:fdb8ecdf954f | 41 | BluLED = 0; |
CSTritt | 5:c849e3c4a5f0 | 42 | char letter; // Declare variable to hold recieved characters. |
CSTritt | 5:c849e3c4a5f0 | 43 | |
CSTritt | 5:c849e3c4a5f0 | 44 | while(true) { // Main (infinite) loop. |
CSTritt | 5:c849e3c4a5f0 | 45 | if (pc.readable()) { // Is there a character waiting? If so, |
CSTritt | 5:c849e3c4a5f0 | 46 | letter = pc.getc(); // Get it. |
CSTritt | 5:c849e3c4a5f0 | 47 | pc.putc(letter); // Echo it. |
CSTritt | 5:c849e3c4a5f0 | 48 | if (letter == 'r') { // Respond to known letters, ignore others. |
CSTritt | 5:c849e3c4a5f0 | 49 | RedLED = !RedLED; // Toggle red. |
CSTritt | 5:c849e3c4a5f0 | 50 | } else if (letter == 'g') { |
CSTritt | 5:c849e3c4a5f0 | 51 | GrnLED = !GrnLED; // Toggle green. |
CSTritt | 5:c849e3c4a5f0 | 52 | } else if (letter == 'b') { |
CSTritt | 5:c849e3c4a5f0 | 53 | BluLED = !BluLED; // Toggle blue. |
CSTritt | 1:fdb8ecdf954f | 54 | } |
mab5449 | 0:8d318218bac1 | 55 | } |
mab5449 | 0:8d318218bac1 | 56 | } |
CSTritt | 3:cbacf69dbed5 | 57 | } |