In lesson 20 we showed you how to connect up an RGB LED, and how to program it to get the primary colors, based on input from the user. In this lesson we look at mixing colors and getting a wide array of colors from the LED by mixing the R, G, and B channels. As examples, we use RGB mixing to create Cyan, Magenta, and Yellow. These are just examples, and any of the in between colors can also be mixed.
If you want to follow along at home, you can order the Arduino Kit we are using HERE.
The video shows how to connect the circuit, and also takes you step by step through the code. If you need extra help, the code is included below.
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 | int redPin=8; int greenPin=9; int bluePin=10; String myColor; String msg="What Colour Do You Want?"; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(redPin,OUTPUT); pinMode(greenPin,OUTPUT); pinMode(bluePin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: Serial.println(msg); while (Serial.available()==0){ } myColor=Serial.readString(); 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=="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); } if (myColor=="aqua"){ digitalWrite(redPin,LOW); analogWrite(greenPin,255); analogWrite(bluePin,80); } } |