1

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
    What exactly is 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. – TisteAndii Mar 18 '16 at 23:15
  • 1
    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. – Nick Gammon Mar 19 '16 at 6:25
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.