7_12 Color Tuner

Color Tuner

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.


/*Use the turn knob and the red, green, and blue buttons to make a custom color on the multicolor LED. Turn the knob to set the intensity level, then press the button to show that color and that intensity on the RGB LED. red button = red light, blue button = blue light, green button = green light*/ int red = 0; //Create three variables named red, green, and blue that are integers (whole numbers) and have a starting value of 0 int green = 0; //These variables will store the different values of the intensity variable in the loop function int blue = 0; void setup(){ pinMode(9,OUTPUT); //LED pinMode(10,OUTPUT); //LED pinMode(11,OUTPUT); //LED pinMode(A0,INPUT); //turn knob pinMode(4,INPUT_PULLUP); //button pinMode(5,INPUT_PULLUP); //button pinMode(6,INPUT_PULLUP); //button } void loop(){ int intensity = analogRead(A0)/4; //Create a variable called 'intensity' that is an integer (whole number) //The intensity variable value takes the reading from the turn knob and divides it by 4, so it has a range of 0 to 255 //analogWrite works on LEDs 9,10,and 11 and has a range of 0-255 if(digitalRead(4)==LOW){ //If the button 4 reading is LOW (the red button is pressed) red = intensity; //Set the value of red to the intensity value //Turning the knob while holding the red button will change the amount of red in the LED } if(digitalRead(5)==LOW){ //If the button 5 reading is LOW (the green button is pressed) green = intensity; //Set the value of green to the intensity value //Turning the knob while holding the green button will change the amount of green in the LED } if(digitalRead(6)==LOW){ //If the button 6 reading is LOW (the blue button is pressed) blue = intensity; //Set the value of blue to the intensity value //Turning the knob while holding the blue button will change the amount of blue in the LED } analogWrite(9, red); //set the LED's red intensity equal to the value of the 'red' variable analogWrite(10, green); //set the LED's green intensity equal to the value of the 'green' variable analogWrite(11, blue); //set the LED's blue intensity equal to the value of the 'blue' variable } // (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 challenge? 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!