![]() |
Cubelets API Documentation 2.0
C API
|
Creates a symmetrical LED pattern with blinking effects as seen in the Mood Ring Pachinko personality. More...
Functions | |
void | setup () |
Initialization function called once at the start. The setup() Function runs once when the program starts. Here, it’s empty because we don't have any initialization. | |
void | loop () |
Main loop function that runs repeatedly. The loop() Function runs repeatedly. It calls two functions: | |
void | think () |
Computes the block_value if there is no manual override. | |
void | act () |
Lights up the LED ring with a symmetrical pattern and blinking effects. | |
We want to create a symmetrical LED pattern where:
setup()
function initializes the system (empty in this case).loop()
function continuously calls think()
and act()
.think()
function computes the block_value
based on sensor input.act()
function lights up LEDs symmetrically and introduces dynamic effects.void setup | ( | ) |
void loop | ( | ) |
The loop() function gets called repeatedly while a Cubelet is powered on.
void think | ( | ) |
Used in Action and Think Cubelets to calculate their block value.
The think() function calculates block_value, a number that determines:
The weighted_average() function calculates this based on sensor inputs (not shown in the code). If there’s no manual override (block_value_override), it updates block_value.
void act | ( | ) |
Using to perform an action based on a previously calculated block value.
This is where the magic happens! Let’s break it into smaller sections.
Symmetry in LED Colors
The 8 LEDs are arranged in pairs: LED 0 pairs with LED 7. LED 1 pairs with LED 6, and so on.
The following loop runs for half the LEDs, setting their colors and mirroring them for the other half.
Color Calculation
The hueToRGB() function converts a hue (a number between 0 and 255) into red (r), green (g), and blue (b) values.
The hue shifts slightly for each pair:
The % 255 ensures the hue wraps around within the valid range.
Brightness Adjustment
a. Brightness depends on block_value and a random factor.This ensures that brightness increases with block_value but also has some variation for a cool effect.
b. The RGB values are scaled by brightness
Setting the LEDs
The LedSet() function lights up a specific LED with the calculated color. This creates a symmetrical pattern.
Blinking Effect
One random LED blinks brightly. The chance of blinking increases with block_value. Note: rand() % 8 selects a random LED to blink.
Dynamic Hue and Speed
The hue shifts slightly each loop.
The update speed also changes with block_value:
Turn the LEDs on
The last line of code is when we tell the Cubelet to turn the LEDs on to their newest colors. Without this call, the LEDs will never update.
And now, for the full code: