Cubelets API Documentation 2.0
C API
Loading...
Searching...
No Matches
mood_ring.c File Reference

Detailed Description

#include "cubelet.h"
void setup()
{
}
void loop()
{
think();
act();
}
void think()
{
if (!block_value_override)
{
block_value = weighted_average();
}
}
void act()
{
uint8_t r, g, b, brightness;
static uint8_t last_hue = 0;
static uint8_t blink_led = 0; // Tracks the LED that will blink brightly.
// Hue calculation based on block_value
uint8_t hue = (uint8_t)(block_value * 2); // Faster hue shift with higher block_value
// Iterate through the LEDs in pairs to create symmetry
for (uint8_t i = 0; i < 4; i++)
{
hueToRGB((hue + i * 64) % 255, 10, &r, &g, &b); // Generate RGB from hue, changing slightly per pair
// Calculate brightness, increasing with block_value
brightness = (uint8_t)((block_value / 2) + (rand() % (block_value / 2 + 1)));
// Scale RGB by brightness
r = (r * brightness) / 255;
g = (g * brightness) / 255;
b = (b * brightness) / 255;
// Set LEDs in mirrored pairs for symmetry
LedSet(i, r, g, b); // LED i
LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
}
// Blinking bright LED effect (varies with block_value)
if (rand() % (256 - block_value) < 5) // More frequent blink with higher block_value
{
blink_led = rand() % 8; // Choose a random LED to blink
LedSetHue(blink_led, last_hue, 255); // Set to bright hue with maximum brightness
}
// Slightly change the hue for the next iteration
last_hue = (last_hue + 1) % 255;
// Update LED ring and introduce a delay that decreases with block_value
wait(150 - (block_value / 2)); // Faster updates as block_value increases
LedRingUpdate();
}
uint8_t weighted_average(void)
Calculates a block value based on the on the weighted average of neighbors block values.
void setup()
Function ran just a single time. Used for setting up variables or timers.
Definition bargraph.c:3
void act()
Using to perform an action based on a previously calculated block value.
Definition bargraph.c:19
void think()
Used in Action and Think Cubelets to calculate their block value.
Definition bargraph.c:14
void loop()
The loop() function gets called repeatedly while a Cubelet is powered on.
Definition bargraph.c:8
void wait(uint16_t delay)
Function to delay execution for a specified amount of time.