Rainbow uses HSV color space. In this color system, colors are not mixed in red, green, and blue tones. Instead, choose a base color that provides a spectrum of colors. Two additional parameters are used to affect color saturation/intensity and brightness.
Our algorithm works to select the first LED color determined by a number between 0 and 255. The next LED is assigned a color slightly to the right of the color spectrum, and so on. In the next pass, shift the color number of the first LED by 1, and so on to create a rolling rainbow effect.
Click Run Code on the toolbar to start the program immediately. You can also load the script onto the cartridge (context menu “Load onto cartridge”). This will start the script as soon as you insert it into Oxocard.
The script contains two constants for brightness (brightness) and velocity (ANIMATION_DELAY), which can be changed using the slider. Click the button to end the animation.
Code explanation:
Lines 1 and 2 define two parameters that affect the brightness and speed of the animation. This can be done directly in the code or via the constant editor with a slider on the right.
The LED library is initialized on lines 3-6. Here we define the switching pins, number of LEDs, and controller type.
The actual program logic is on lines 12-19. Iterate through the LEDs in an infinite loop. for loop. On each pass, a new base color is calculated from the HSV color space according to the following formula: j+(i*3).
You can see that there are two variables here. variable I Contains the current LED number. This changes the color value between the two LEDs by 3 each time. as j Increments by 1 every cycle. Hue The values change and the whole is perceived as a flowing spectrum of colors.
The color is then output to the current LED. set digital led.
Line 18 sends new color data to the LED and waits a few milliseconds before the loop starts again.
Blocks 20-25 check if the user pressed the button. In this case, all LEDs will turn off and the program will exit.