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

Example code for the Mood Ring Cubelet. More...

Detailed Description

Pachinko

Explains the code behind the Pachinko personality:

void setup()
{
}
void loop()
{
think();
act();
}
void think()
{
if (!block_value_override)
{
block_value = weighted_average();
}
}
* static uint8_t blink_led = 0;
*
* uint8_t hue = (uint8_t)(block_value * 2);
*
* // Iterate through LEDs in mirrored pairs
* for (uint8_t i = 0; i < 4; i++)
* {
* hueToRGB((hue + i * 64) % 255, 10, &r, &g, &b); // Generate RGB based on hue
*
* brightness = (uint8_t)((block_value / 2) + (rand() % (block_value / 2 + 1))); // Randomized brightness
*
* // Scale RGB by brightness
* r = (r * brightness) / 255;
* g = (g * brightness) / 255;
* b = (b * brightness) / 255;
*
* // Set LEDs in mirrored pairs
* LedSet(i, r, g, b); // LED i
* LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
* }
*
* // Blinking effect for a random LED
* if (rand() % (256 - block_value) < 5)
* {
* blink_led = rand() % 8;
* LedSetHue(blink_led, last_hue, 255); // Set random LED to bright hue
* }
*
* last_hue = (last_hue + 1) % 255; // Increment hue for smooth transitions
*
* wait(150 - (block_value / 2)); // Adjust speed based on block_value
* LedRingUpdate();
* }
* @endcode
*/
void act()
{
uint8_t r, g, b, brightness;
static uint8_t last_hue = 0;
static uint8_t blink_led = 0;
uint8_t hue = (uint8_t)(block_value * 2);
// Iterate through LEDs in mirrored pairs
for (uint8_t i = 0; i < 4; i++)
{
hueToRGB((hue + i * 64) % 255, 10, &r, &g, &b); // Generate RGB based on hue
brightness = (uint8_t)((block_value / 2) + (rand() % (block_value / 2 + 1))); // Randomized brightness
// Scale RGB by brightness
r = (r * brightness) / 255;
g = (g * brightness) / 255;
b = (b * brightness) / 255;
// Set LEDs in mirrored pairs
LedSet(i, r, g, b); // LED i
LedSet(7 - i, r, g, b); // Symmetrical LED (7 - i)
}
// Blinking effect for a random LED
if (rand() % (256 - block_value) < 5)
{
blink_led = rand() % 8;
LedSetHue(blink_led, last_hue, 255); // Set random LED to bright hue
}
last_hue = (last_hue + 1) % 255; // Increment hue for smooth transitions
wait(150 - (block_value / 2)); // Adjust speed based on block_value
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.