-
Notifications
You must be signed in to change notification settings - Fork 0
/
Understanding_the_RGB_LED.cpp
76 lines (61 loc) · 1.43 KB
/
Understanding_the_RGB_LED.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Understanding the RGB LED
int redPin=8;
int greenPin=9;
int bluePin=10;
String myColor;
String msg="Which color would you like?";
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
//ask
Serial.println(msg);
//wait
while (Serial.available() == 0){ // to just keep it hanging out
}
//read
myColor = Serial.readString(); // to populate the empty variable we declared above
if (myColor=="red"){
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
if (myColor == "green"){
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
}
if (myColor == "blue"){
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
if (myColor == "off"){
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
if (myColor == "aqua"){
digitalWrite(redPin, LOW);
analogWrite(greenPin, 255); //use a number between 0 and 255
analogWrite(bluePin, 125);
}
if (myColor == "yellow"){
analogWrite(redPin, 255);
analogWrite(greenPin, 100);
analogWrite(bluePin, 0);
}
if (myColor == "cyan"){
analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 255);
}
if (myColor == "magenta"){
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 100);
}
}