3_6 RGB Color Mixer

Mix RGB LED Colors with Buttons

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.


/* Set up 3 buttons to correspond to each color from the RGB LED Pressing any single button shows one color. Pressing multiple buttons mixes colors! */ void setup() { pinMode(4,INPUT_PULLUP); //button pinMode(5,INPUT_PULLUP); //button pinMode(6,INPUT_PULLUP); //button pinMode(9,OUTPUT); //one leg of the RGB LED pinMode(10,OUTPUT); //one leg of the RGB LED pinMode(11,OUTPUT); //one leg of the RGB LED } void loop() { if(digitalRead(4)==LOW){ //if button 4 is pressed... digitalWrite(9,HIGH); //turn on the LED color 9 } else{ //otherwise, if button 4 is not pressed... digitalWrite(9,LOW); //turn off LED color 9 } if(digitalRead(5)==LOW){ //if button 5 is pressed... digitalWrite(10,HIGH); //LED color 10 is on } else{ //otherwise.... digitalWrite(10,LOW); //LED color 10 is off } if(digitalRead(6)==LOW){ //if button 6 is pressed... digitalWrite(11,HIGH); //LED color 11 is on } else{ //otherwise.... digitalWrite(11,LOW); //LED color 11 is off } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Video

Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!

Challenges

Can you complete the challenges? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!