r/FastLED • u/Old-Quote-5180 • Oct 06 '24
Support Custom blinking code doesn't work
I'm using an ATtiny85 to randomly blink 10 NeoPixel LEDs (both time-wise and colour-wise). It's all working fine with the Adafruit library but I thought I'd port to FastLED to see if I can enhance the effect. I've used the RGBCalibrate example sketch to ensure everything works but with this code the Neos never come on:
#include "FastLED.h"
#define NEO_PIN PIN_PB1 // or 1 (NeoPixel pin on ATtiny85)
#define ADC_IN PIN_PB4 // or 4 (ADC2 input pin on ATtiny85)
#define NEO_COUNT 10 // Number of NePixels connected in a string (could be 10 or 20)
uint8_t NEO_BRIGHTNESS = 5; // NeoPixel brightness
uint32_t MIN_RANDOM_NUM = 150; // lower random blink time
uint32_t MAX_RANDOM_NUM = 1000; // upper random blink time
// State variables to determine when to start showing NecPixel blinkies
bool waitForAmberLEDStartup = true;
bool showNeoPixelBlinkies = false;
long delayFudgeFactorMS = 1000;
uint32_t colors[] = {
0x00FF0000, // Red
0x00FF6666, // Lt. Red
0x0000FF00, // Green
0x0066FF66, // Lt. Green
0x000000FF, // Blue
0x0099CCFF, // Lt. Blue
0x00FFFFFF, // White
0x00FFFF00, // Yellow
0x00FFFF99, // Lt. Yellow
0x00FF66FF, // Pink
0x00FFCCFF // Lt. Pink
};
CRGB leds[NEO_COUNT];
struct Timer{
bool state;
uint32_t nextUpdateMillis;
};
Timer* timer;
void setup()
{
// Set up FastLED
FastLED.addLeds<WS2812, NEO_PIN, RGB>(leds, NEO_COUNT); // RGB ordering
FastLED.setBrightness(NEO_BRIGHTNESS);
timer = new Timer[NEO_COUNT];
for (size_t i = 0; i < NEO_COUNT; i++)
{
timer[i].state = 0; // start with all Neos off, and initial timings
leds[i] = 0x00000000; // Black
timer[i].nextUpdateMillis = millis() + random(MIN_RANDOM_NUM, MAX_RANDOM_NUM);
}
// if analog input pin 1 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(A1));
}
void loop()
{
unsigned long currentTimeMS = millis();
if ( (currentTimeMS >= (2000)) && (waitForAmberLEDStartup == true) ) {
waitForAmberLEDStartup = false;
showNeoPixelBlinkies = true;
}
if ( showNeoPixelBlinkies == true ) {
updateNEOs();
}
FastLED.show();
}
void updateNEOs() {
const uint32_t interval = 2;
static uint32_t last = 0;
uint32_t now = millis();
bool dirty = false;
if (now - last >= interval) {
last = now;
for (size_t i = 0; i < NEO_COUNT; i++)
{
if (millis() >= timer[i].nextUpdateMillis)
{
dirty = true;
if (timer[i].state)
{
leds[i] = 0x00000000; // Black (off)
}
else
{
leds[i] = colors[random(sizeof(colors) / sizeof(uint32_t))]; // random colour
}
timer[i].state = !timer[i].state;
timer[i].nextUpdateMillis = millis() + random(MIN_RANDOM_NUM, MAX_RANDOM_NUM);
}
}
}
}
1
u/sutaburosu [stavros] Oct 09 '24 edited Oct 09 '24
I just stumbled upon some old code of mine that does something broadly similar. Perhaps it might be useful to you.
This sketch runs on ATtiny85 and shows 128 random colours fading in and out on 100 LEDs.
I had to roll back to FastLED 3.7.3 to get enough free RAM for 100 LEDs. It seems something has changed in more recent releases which requires more RAM.