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.
Diff: main.cpp
- Revision:
- 5:c849e3c4a5f0
- Parent:
- 4:43ce9b92ae68
- Child:
- 6:dcc4031025a6
--- a/main.cpp Tue Sep 12 12:58:40 2017 +0000
+++ b/main.cpp Tue Sep 12 13:21:53 2017 +0000
@@ -1,50 +1,54 @@
#include "mbed.h"
/*
- Serial2RGB main by C. S. Tritt, Last revised 3/18/17 (v. 1.1)
-
- Toggles RGB LED junctions in response to serial input. Echos input.
+ Serial2RGB main by C. S. Tritt, Last revised 9/12/17 (v. 1.2)
+
+ Toggles RGB LED junctions in response to serial input. Note input is case
+ sensive and known "commands" are r (toggle red), g (toggle green) and b
+ toggle blue). Input (including CR/LF is echoed.
Suggested wiring...
-
+
Common Anode LED (active low)
-
- /-- 680 kΩ -- D9 (red)
- + 3.3 to 5.0 V ----LED<--- 680 Ω -- D10 (green)
- \-- 1 kΩ -- D11 (blue)
-
+
+ /-- 680 kΩ -- D13 (red)
+ + 3.3 to 5.0 V ----LED<--- 680 Ω -- D14 (green)
+ \-- 1 kΩ -- D15 (blue)
+
Common Cathode LED (active high)
-
- /-- 680 Ω -- D9 (red)
- GND ----LED<--- 680 Ω -- D10 (green)
- \-- 1 kΩ -- D11 (blue)
-
+
+ /-- 680 Ω -- D13 (red)
+ GND ----LED<--- 680 Ω -- D14 (green)
+ \-- 1 kΩ -- D15 (blue)
+
*/
+
+// Construct resources.
+
DigitalOut RedLED(D13); // Arduino Digital pin 13 on Nucleos.
DigitalOut GrnLED(D14); // Arduino Digital pin 14.
DigitalOut BluLED(D15); // Arduino Digital pin 15.
Serial pc(USBTX, USBRX); // Default settings are 9600 Baud, 8-N-1.
-int main() {
-
- RedLED = 0;
+int main()
+{
+
+ RedLED = 0; // Set pins to known state (junction on if active low).
GrnLED = 0;
BluLED = 0;
- char letter;
-
- while(true) {
- if (pc.readable()) {
- letter = pc.getc();
- pc.putc(letter);
- if (letter == 'r') {
- RedLED = !RedLED;
+ char letter; // Declare variable to hold recieved characters.
+
+ while(true) { // Main (infinite) loop.
+ if (pc.readable()) { // Is there a character waiting? If so,
+ letter = pc.getc(); // Get it.
+ pc.putc(letter); // Echo it.
+ if (letter == 'r') { // Respond to known letters, ignore others.
+ RedLED = !RedLED; // Toggle red.
+ } else if (letter == 'g') {
+ GrnLED = !GrnLED; // Toggle green.
+ } else if (letter == 'b') {
+ BluLED = !BluLED; // Toggle blue.
}
- else if (letter == 'g') {
- GrnLED = !GrnLED;
- }
- else if (letter == 'b') {
- BluLED = !BluLED;
- }
}
}
}
\ No newline at end of file