In this lesson you will learn how to work with RGB LED’s. RGB LED’s can be used to create not only the primary colors, but also all mixtures of the primary colors. This video shows you how to connect up a Common Cathode RGB LED. This project is a little more complex than the ones we have done in the past, so I include the code down below. While the code is here for your reference, you should not just copy and paste it. In order to really learn, you need to type it in for yourself, and then find and debug your mistakes. If you just copy and paste from me, you will never learn how to troubleshoot.
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 | 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=="aqua"){ digitalWrite(redPin,LOW); analogWrite(greenPin,255); analogWrite(bluePin,80); } } |
An official Arduino Uno R3 is available HERE. In this new series of lessons, I will be using the sensor and other components found in this KIT.