01 RGB Control
01 RGB Control
This lesson aims to read the status of the onboard buttons on the Arduino
expansion board, achieving the control of the RGB LED color.
1. Program Flowchart
The three RGB colors of the onboard RGB LED can be separately set to light
up. This enables the RGB LED to achieve a colorful display effect.
3. Program Download
Note:
The Bluetooth module must be removed before downloading the program, otherwise
Please turn the switch of the battery box to “OFF” when connecting the Type-B
download cable in case the download cable touches the power pin of the expansion board
1)Locate and open “rgb_test” program file in the same dictionary as this
1
section.
3)Click the “Select Board”, and the software will automatically detect current
Arduino serial interface. Then click to connect.
4)Click to download the program to the Arduino, and then wait for the
download to complete.
4. Program Outcome
After turning on the robot, the RGB LED on the Arduino expansion board will
be red. When the KEY1 button on the expansion board is pressed, the RGB
2
LED will turn red; when the button is released, it will switch back to red.
5. Program Analysis
2) Configure the button pin as an input IO, with a default low voltage level
value after powering on. Initialize the RGB LED on the expansion board via the
“FastLED” library. Connect it to the “ledPin”. Call the “Rgb_Show(255, 255,
255)” function to set the RGB color to white.
3
5.4 Main Function
In the “loop()” function, use the “analogRead()” function to read the value of
the button pin. Determine the button pin value to control the color of the RGB
LED. If the read button pin value is 0 (low voltage level, indicating the button is
pressed), the color of the RGB LED is set to white; If the read pin value is 1
(high voltage level, indicating the button is released), the color of the RGB LED
is set to red.
4
6. Function Extension
How to modify the color of RGB LED from red to green? Please refer to the
following instructions:
1) Locate the function “Rgb_show()” for controlling the RGB color in the main
program. The three parameters of the function correspond to red, green, and
blue respectively.
2) Modify the value of red to 0 and the value of green to 255. This allows you
to change the RGB color to green.
For more detailed information about the RGB color table, please access:
https://www.bchrt.com/tools/rgbcolor/.
7. FAQ
Q:The code fails to upload. What should I do?
A:Please check if you have connected the Bluetooth module to the robot. If so,
please remove the Bluetooth module before downloading.