Build Your Own Holiday Star With LEDs — Glowing DIY Decorations for the Festive Season!


The rainbow uses the HSV color space. In this color coordinate system, the color is not mixed in the form of red, green, and blue tones. Instead, we select a base color that provides us with a color spectrum. We use two further parameters to influence the saturation/strength of the color and the brightness.

Basic color HSV color spectrum (value range: 0-255).

Our algorithm now works in such a way that we select a color for the first LED that is determined by a number between 0 and 255. The next LED is assigned a color slightly further to the right of the color spectrum, and so on. In the next pass, we shift the color number of the first LED by 1, and so on down the line, creating the effect of the rolling rainbow.

The script can be downloaded directly in the NanoPy IDE, under Examples / Publications / Make Magazine 07/2024.

Click on “Execute code” in the toolbar to start the program immediately. You can also load the script onto the cartridge (context menu “Load onto cartridge”), which will start it immediately when you insert it into the Oxocard.

The script contains two constants, for brightness (BRIGHTNESS) and speed (ANIMATION_DELAY), which can be changed using sliders. Clicking the button ends the animation.

With the integrated debugger, the program can also be viewed during execution.

Explanation of the code:

In lines 1-2, we define two parameters with which we can influence the brightness and speed of the animations. This can be done directly in the code, or alternatively via the constant editor with the sliders on the right.

The LED library is initialized in lines 3-6. Here we define the switching pin, the number of LEDs, and the controller type. 

The actual program logic can be found in lines 12-19. Within the infinite loop, we iterate through the LEDs with a for loop. With each pass, a new base color is calculated from the HSV color space according to the formula j+(i*3).

Here you can see that we have two variables. Variable i contains the number of the current LED. This causes the color value between two LEDs to change by 3 each time. As j is increased by 1 with each cycle, the HUE value shifts and the whole thing is perceived as a flowing color spectrum. 

The color is then output on the current LED with setDigitalLed.

In line 18 we send the new color data to the LEDs, then we wait a few milliseconds until the loop starts again.

Block 20-25 checks whether the user has pressed a button. If this is the case, all LEDs are switched off and the program is then ended.



Source link

Leave a comment

All fields marked with an asterisk (*) are required