Multiple neopixel effects

Hello,
I have a small neopixel stick. On this stick I would like to display two effects in a row.
1.) a repeating running light with e.g. 10 repetitions.
2.) a stroboscope effect which should flash 50 times fast afterwards
After that the function should be finished.
I am not so good at programming.
Theoretically my project works. but the esp8266 crashes if my while loop has more than 5 repetitions.
Also I don't know if the delay is so good with the stroboscope effect.

At the moment the function is called in the loop, but in the future it should just be called once by another action.
Am I on the right track? can anyone give me some helpful tips?

#include <Adafruit_NeoPixel.h>

#define PIN_NEO_PIXEL 0  // The ESP8266 D3
#define NUM_PIXELS 8     // The number of LEDs (pixels) on NeoPixel LED strip

//Adafruit_NeoPixel tira(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel tira = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);

//byte mState                 = 0;
byte iteration = 0;

//timing stuff
unsigned long commonMillis;
unsigned long switchMillis;
bool flag = true;
int zaehler1 = 1;
int zaehler2 = 1;

void setup() {
  //NeoPixel.begin();  // initialize NeoPixel strip object (REQUIRED)
  tira.begin();  // initialize NeoPixel strip object (REQUIRED)
  tira.show();
  tira.setBrightness(50);
}

void loop() {


  yamato();
}


void yamato() {

  while (zaehler1 < 5)  //(millis() - commonMillis >= 100)    //flag == true &&
  {
    if (millis() - commonMillis >= 100) {
      //restart the TIMER
      commonMillis = millis();

      //Led color red
      tira.setPixelColor(iteration, 255, 0, 0);
      tira.setBrightness(50);
      tira.show();

      //next LED
      iteration++;
    }
    if (iteration > 7) {
      tira.clear();
      tira.show();
      iteration = 0;
      zaehler1 = zaehler1 + 1;
    }
  }

  tira.clear();
  tira.show();
  delay(100);


  if (zaehler2 < 50)  
  {
    for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {       // for each pixel
      tira.setPixelColor(pixel, tira.Color(255, 255, 255));  // Led color white
      tira.setBrightness(80);
    }

    tira.show();

    //next LED

    delay(20);
    tira.clear();
    tira.show();
    zaehler2 = zaehler2 + 1;
  } else {
    zaehler1 = 1;
    zaehler2 = 1;
  }

}

why is that commented out ?

sorry, I should have cleaned that up.

this line should make the job:
tira.begin();

ah I missed it sorry for the comment

your yamato() function can take a long time. May be the watchdog bites you?
try to add a yield(); or delay(0); inside the while

  while (zaehler1 < 5)  //(millis() - commonMillis >= 100)    //flag == true &&
  {
    yield(); // or delay(0);
    if (millis() - commonMillis >= 100) {
     ...

a state machine would probably provide a better structure for your code

1 Like

ok, that solved the crash problem. thank you very much.
Now, If I do not execute this in the loop funkction. I have to change the second part of the code somehow.
I tried to do a while too, but it does not flash anymore. It just lights up.

#include <Adafruit_NeoPixel.h>

#define PIN_NEO_PIXEL 0  // The ESP8266 D3
#define NUM_PIXELS 8     // The number of LEDs (pixels) on NeoPixel LED strip

Adafruit_NeoPixel tira = Adafruit_NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);

//byte mState                 = 0;
byte iteration = 0;

//timing stuff
unsigned long commonMillis;
unsigned long switchMillis;
bool flag = true;
int counter1 = 1;
int counter2 = 1;

void setup() {

  tira.begin();
  tira.show();
  tira.setBrightness(50);
    yamato();
}

void loop() {



}


void yamato() {

  while (counter1 < 10) 
  {
    yield(); // or delay(0);
    if (millis() - commonMillis >= 100) {
      //restart the TIMER
      commonMillis = millis();

      //Led color red
      tira.setPixelColor(iteration, 255, 0, 0);
      tira.setBrightness(50);
      tira.show();

      //next LED
      iteration++;
    }
    if (iteration > 7) {
      tira.clear();
      tira.show();
      iteration = 0;
      counter1 = counter1 + 1;
    }
  }

  tira.clear();
  tira.show();
  delay(100);


  while (counter2 < 50)  
  {
    for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {       // for each pixel
      tira.setPixelColor(pixel, tira.Color(255, 255, 255));  // Led color white
      tira.setBrightness(80);
    }

    tira.show();

    delay(20);
    tira.clear();
    tira.show();
    counter2 = counter2 + 1;
  } 
  
  if (counter2 == 50) {
    counter1 = 1;
    counter2 = 1;
  }

}

Are you doing okay with the Pix sequences?
Are you working on this "other action?"

no, the part
while (counter2 < 50)

does not work. I don't know yet how to make the flashing in this while section :frowning:

You are not giving the "pixels off" time long enough to be seen by your eyes. Put a 300 ms delay after .show()

1 Like

Investigate state machines

thank you very much. I had to put the delay after the second .show().
Thanks a lot!

Note that the Adafruit library guide says:

setBrightness() was intended to be called once, in setup(), to limit the current/brightness of the LEDs throughout the life of the sketch. It is not intended as an animation effect itself!

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