Sparkle LED Strip

Sparkle LED Strip
Here is a nice short sketch for an LED Strip animation with a sparkle added to it. This is the code: However the animated GIF does not have the random delay that is at the end of the sketch.

#include <Adafruit_NeoPixel.h>
 
#define PIN 9
#define numberOfPixels 60
int myRandom = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numberOfPixels, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.setBrightness(5); // LOW brightness
  allBlue();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
  // put your main code here, to run repeatedly:
myRandom= random(numberOfPixels);
strip.setPixelColor(myRandom, strip.Color(255,255,255));
strip.show();
delay(5);
strip.setPixelColor(myRandom, strip.Color(0,0,50));
strip.show();
delay(random(25,75));
}
 
void allBlue() {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 50));
      strip.show();
  }
}


It is not very long at all. I like short code examples that give us something to look at.

It uses pin number 9 on an Arduino and Adafruit’s Adafruit_NeoPixel library.

#include <Adafruit_NeoPixel.h>
 
#define PIN 9
#define numberOfPixels 60

There is a random number and the brightness is very low. It is set to 5. You can increase the brightness as you wish. Brightness goes all the way up to 255. I find 255 way too bright. In fact, a brightness of 100 is still quite bright.

int myRandom = 0;
strip.setBrightness(5); // LOW brightness

In setup() the pixels are all set to a dark blue: strip.Color(0,0,50). This is called with allBlue();. While the code runs the default background is set to this dark blue. You can change it to any color you like.

void allBlue() {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 50));
      strip.show();
  }
}

myRandom is a random number representing each of the pixels on the strip of pixels. Since there are 60 pixels the random number is 0 to 59.

myRandom= random(numberOfPixels);

Once the myRandom number is chosen the pixel associated with that number is set to white for a tiny bit of time. You can see this time in the delay(5); command. After that, the pixel is set back to the blue color. Then there is a random time until the code repeats. Upon repeating the next random pixel is chosen to turn white for a short amount of time. And on it goes.

The random time that is chosen before the code repeats is set by the following line of code. It is setting a delay length of time from 25 through 74.

delay(random(25,75));

Leave a Reply

Your email address will not be published. Required fields are marked *