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;
}
Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Mar 18, 2016 at 22:54
2
  • 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. Commented Mar 18, 2016 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. Commented Mar 19, 2016 at 6:25

1 Answer 1

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.

answered Mar 19, 2016 at 16:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.