How can I make LEDs blink randomly?
I found a piece of code, but it is not working for me. I probably need something other than setPixel()
, but I don't know what.
I am using the Fast.Led library with Arduino Nano and WS2812B LEDs
void loop() {
Twinkle(CRGB::Red, 10, 100, false);
}
void Twinkle(uint32_t color, uint16_t Count, uint16_t SpeedDelay, boolean OnlyOne)
{
FastLED.setBrightness(MAX_BRIGHTNESS);
fill_solid(leds, random(NUM_LEDS), CRGB::White);
FastLED.show();
for (int i=0; i < Count; i++) {
// setPixel(random(NUM_LEDS),red,green,blue); //!ORIGINAL LINE OF CODE!
setPixel(random(NUM_LEDS), color); //!MY LINE!
FastLED.show();
delay(SpeedDelay);
if (OnlyOne) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
}
}
delay(SpeedDelay);
return;
}
1 Answer 1
I can't tell exactly whats not working with your code,it would be really helpful if you elaborate more. But here is some code that I wrote and I think it will get the job done:
//declare our variables:
int myPins[] = {2, 4, 8, 3, 6}; //Enter the number of the pins being used
int RNum = 0;
void loop()
{
DigitalWrite(myPins[RNum],LOW);
RNum = Random(0,MAX) // MAX being the number of LEDs connected minus one
//the above generates a random number
DigitalWrite(myPins[RNum],High);
}
This will turn on one LED at a time.
setPixel()
? Does the original code work at all? Remove the statements within exclamation marks and use proper comments for your code. A link to your library would also be nice.it is not working for me
- what is it doing? Nothing blinks? Too many things blink? It isn't random? Aliens kidnap you and do scientific experiments on you? "Not working" doesn't tell us much.