Charles Tritt / Mbed OS Serial2RGB_21_v5

main.cpp

Committer:
CSTritt
Date:
2017-09-12
Revision:
4:43ce9b92ae68
Parent:
3:cbacf69dbed5
Child:
5:c849e3c4a5f0

File content as of revision 4:43ce9b92ae68:

#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.
    
    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)
    
    Common Cathode LED (active high)
    
                /-- 680 Ω -- D9 (red)
    GND ----LED<--- 680 Ω -- D10 (green)
                \-- 1 kΩ -- D11 (blue)
   
*/
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;
    GrnLED = 0;
    BluLED = 0;
    char letter;
    
    while(true) {
        if (pc.readable()) {
            letter = pc.getc();
            pc.putc(letter);
            if (letter == 'r') {
                RedLED = !RedLED;
            }
            else if (letter == 'g') {
                GrnLED = !GrnLED;
            }       
            else if (letter == 'b') {
                BluLED = !BluLED;
            }       
        }
    }
}