Play with colors
The RGB LED has three individually controllable LEDs colored in red, green and blue. The brightness of each color can be controlled independently, so that a vast number of different colors can be mixed.
The brightness of each LED can be specified in numbers between 0 (off) and 255 (very bright). When all three LEDs are fully turned on, it turns out to be white.
At first, the LED should be programmed to light up in white and one of the three colors should slowly become darker.
Three variables are generated for the three color values. They need to be set to full brightness, meaning 255. Then the LED is activated with these three brightness values. It lights up white. The white value in the program block does not have to be set.
Then the red light is reduced to zero in steps of one and the new color values are passed to the LED. In order to see any effect at all, you have to wait after every step.
At the end, the LED lights up in cyan, since only green and blue are switched on.
Task: Change the program so that the blue or green LED changes its brightness. Which colours are produced at the end of the loop?
If you want to perform several consecutive color changes, the program becomes very long and therefore confusing. For this reason, program parts that are used several times are outsourced and this program part is then called a function. A function has a unique, freely definable name and is called up by using it.
A function which causes the red LED to be turned off slowly is supposed to be written. The name of the function is red_off. And that is also the name it is called up with.
This function contains the entire block for shutting down the brightness of red and is called up in the main program by its name.
Task: Write a second function with the name red_on, which caused the red LED to light up again. In the main program, the red LED is now being switched off and on in an alternating rhythm.
To keep track of large programs, finished blocks can be folded together. They are then still completely there, but only take up one line on the screen.
Task: Fold the two functions together.
Now a complete play of colours is to be programmed. It is supposed to be possible to switch every color on and off again.
Task: Write four more functions: green_on, green_off, blue_on, blue_off. Make sure to use the copy function (right mouse click!)
The main part of the program looks like this: