How can i make it random?

Hello everyone :slight_smile: I have simple neopixel code which is fading my led on and off in the same time. But I would like to have it RANDOM. Anyone who can help me with it ? :))
THNX

#include <Adafruit_NeoPixel.h>

#define PIN 13

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(21, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(.01);  // Lower brightness and save eyeballs!
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
//Written by: Jason Yandell

int TOTAL_LEDS = 21;
float MaximumBrightness = 255;
float SpeedFactor = 0.008; // I don't actually know what would look good
float StepDelay = 5; // ms for a step delay on the lights

// Make the lights breathe
for (int i = 0; i < 65535; i++) {
// Intensity will go from 10 - MaximumBrightness in a "breathing" manner
float intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));
strip.setBrightness(intensity);
// Now set every LED to that color
for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
strip.setPixelColor(ledNumber, 174, 0, 255); //purple
}

strip.show();
//Wait a bit before continuing to breathe
delay(StepDelay);

}
}

Not sure what exactly you want to be RANDOM. But what happens if you change delay(StepDelay) to delay(random(2, 15) ? Or any other pair of numbers. Try a few different ones.

Steve

That will always evaluate to true for any AVR processor (16 bit ints). Since i is an int, the largest value it can hold is 32767 which is always less than 65535.

If you are using a 32 bit processor where ints are 32 bits, it will work as espected.

What board are you using?

Don’t take short cuts:

for (int i = 0; i < 65535; i++) {

for (unsigned int i = 0; i < 65535; i++) {

I mean now my 21 LEDs are breathing in the same time. But I want each individual LED to flash at a different time.

Problem is that this is a global function that applies to all the LEDs in your strip, so there is no simple fix to make your code do what you want.

You need an array of brightness speeds one entry for each of your LEDs. Then you need another to keep track of your current brightness for each LED.
Then you need to set the pixel colour for each LED taking into account the basic colour and how bright the colour currently is.

I suspect your coding skills are not yet up to doing this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.