Dependencies:   mbed

Committer:
BertieHarte
Date:
Fri Mar 26 10:21:38 2021 +0000
Revision:
0:15e1afc3849d
make public

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BertieHarte 0:15e1afc3849d 1 #include "mbed.h"
BertieHarte 0:15e1afc3849d 2
BertieHarte 0:15e1afc3849d 3 #include "iostream" // this is required for "cin" (user input from keyboard)
BertieHarte 0:15e1afc3849d 4 DigitalOut R(p23); //Red pin of RGB LED
BertieHarte 0:15e1afc3849d 5
BertieHarte 0:15e1afc3849d 6 DigitalOut G(p24); //Green pin of RGB LED
BertieHarte 0:15e1afc3849d 7
BertieHarte 0:15e1afc3849d 8 DigitalOut B(p25); //Blue pin of RGB LED
BertieHarte 0:15e1afc3849d 9
BertieHarte 0:15e1afc3849d 10 char c; // initialise a variable of type char for the user intput.//*NOTE THE RGB LED on the application board is "0" for on, "1" for off * NOTE //
BertieHarte 0:15e1afc3849d 11
BertieHarte 0:15e1afc3849d 12 int main() {
BertieHarte 0:15e1afc3849d 13
BertieHarte 0:15e1afc3849d 14 R = G = B = 1; // initialize all LED to off
BertieHarte 0:15e1afc3849d 15 printf("Control the LED using keyboard. \n \r"); // print the messages below to terminal (\n = new line, \r = move to 1st pos on next line.)
BertieHarte 0:15e1afc3849d 16
BertieHarte 0:15e1afc3849d 17 printf("R for Red, G for Green, B for Blue,W for White. \n \r");
BertieHarte 0:15e1afc3849d 18
BertieHarte 0:15e1afc3849d 19 printf("O turn off all LEDs. \n \r");
BertieHarte 0:15e1afc3849d 20
BertieHarte 0:15e1afc3849d 21 while(1) {
BertieHarte 0:15e1afc3849d 22
BertieHarte 0:15e1afc3849d 23 cin >> c; // get user input and assign to the char variable "c"
BertieHarte 0:15e1afc3849d 24
BertieHarte 0:15e1afc3849d 25 if (c == 'r') // if input = r, for red...
BertieHarte 0:15e1afc3849d 26
BertieHarte 0:15e1afc3849d 27 {R = 0, G = 1, B = 1;} // turn red of RGB on, while turning Green & Blue off. ** SEE NOTE ABOVE **
BertieHarte 0:15e1afc3849d 28
BertieHarte 0:15e1afc3849d 29 else if // if not the above...
BertieHarte 0:15e1afc3849d 30
BertieHarte 0:15e1afc3849d 31 (c == 'g')
BertieHarte 0:15e1afc3849d 32
BertieHarte 0:15e1afc3849d 33 {R = 1, G = 0, B = 1;}
BertieHarte 0:15e1afc3849d 34
BertieHarte 0:15e1afc3849d 35 else if// if not the above...
BertieHarte 0:15e1afc3849d 36
BertieHarte 0:15e1afc3849d 37 (c == 'b')
BertieHarte 0:15e1afc3849d 38
BertieHarte 0:15e1afc3849d 39 {R = 1, G = 1, B = 0;}
BertieHarte 0:15e1afc3849d 40
BertieHarte 0:15e1afc3849d 41 else if // if not the above...
BertieHarte 0:15e1afc3849d 42
BertieHarte 0:15e1afc3849d 43 (c == 'w')
BertieHarte 0:15e1afc3849d 44
BertieHarte 0:15e1afc3849d 45 {R = 0, G = 0, B = 0;}
BertieHarte 0:15e1afc3849d 46
BertieHarte 0:15e1afc3849d 47 else if // if not the above...
BertieHarte 0:15e1afc3849d 48
BertieHarte 0:15e1afc3849d 49 (c == 'o')
BertieHarte 0:15e1afc3849d 50
BertieHarte 0:15e1afc3849d 51 {R = 1, G = 1, B = 1;}
BertieHarte 0:15e1afc3849d 52
BertieHarte 0:15e1afc3849d 53 }
BertieHarte 0:15e1afc3849d 54
BertieHarte 0:15e1afc3849d 55 }