Combining multiple neopixel example sketches into 1 sketch

Hi All
Thanks for reading. I'm enjoying playing with my neopixel LED strip at a basic level, however I'm having trouble combining some of the great examples into one sketch. New to coding. Apologies.
I.e I'd like to do a colorwipe and some sparkle (one after the other) in the same loop. Or maybe combine 3 example effects into one sketch so that I can run each for a few minutes consecutively in a loop.
Ive attached code below where the sparkle effect doesnt work. I can successfully do multiple colorwipes within the one sketch .
Im sure there are some fundamental coding lessons I need to learn here. At the moment I am just cut/pasting the examples.
Much appreciated.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 90
// 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(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
  colorWipe(0xff,0xff,0xff, 12.9); //1.38 second wipe
  setAll(0,0,0); // set all to off for delay between wipes
  colorWipe(0xff,0xff,0xff, 129); //13.8 second wipe 
  delay(13800);  // adding 13.8 seconds all on
  setAll(0,0,0); // set all to off for delay between wipes
  Sparkle(0xff, 0xff, 0xff, 0); //perform sparkle function for 1 minute
  delay (3600);
  }
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel,0,0,0);
}
void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}
// *** REPLACE TO HERE ***

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();

}
  Sparkle(0xff, 0xff, 0xff, 0); //perform sparkle function for 1 minute

When you call the Sparkle() function with a delay parameter of 0, as above, what do you expect it to do and what is the relevance of the comment ?

You need a state machine for this, using a state variable to select the animation that is currently running.

That is the "fundamental coding lesson" that applies here.

1 Like

Thanks. I was wondering about that delay value, but the code was obtained from a working example and it works on its own - as shown below.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 60
// 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(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
  Sparkle(0xff, 0xff, 0xff, 0);
}

void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
  setPixel(Pixel,0,0,0);
}
// *** REPLACE TO HERE ***

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

That depends on your definition of "works". If you are going to use a delay of 0 then why have the delay ?

When you call the Sparkle function all it does it to change the state of one LED so I doubt whether you notice it. In the example you continually call Sparkle so continually see random LEDs change

In your main code try wrapping the call to Sparkle() in a 100 step for loop() so that there are 100 LED state changes

1 Like

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