WS2812B LED Strip with FastLed library problem

Hey guys,

So I have a few push buttons set up each one of them responsible for a different light effect. The idea is that when you push the button the effect will start, however if you push another button, it should erase the effect from the first button (even tho it hasn’t traversed through the whole LED strip) and just immediately start the next effect. (or if you press the same button twice in the span of a few seconds, it should again break wherever it has reached with the first press and start from the beginning). As of right now, the effects are working just fine with the buttons, the only problem is that the system always wait for the effect to go over all LEDs in the strip and only after that’s done, it triggers the effect again. Do you have an idea of how this can be solved?

I’m sorry, the code is a bit of a mess

:confused:

int ledPin = 13;
int inputPin1 = 7;   // choose input pin 7 for the push button
int inputPin2 = 8;
int inputPin3 = 9;
int inputPin4 = 11;
int timePassed = 0;
int counter1 = 0;
int next [100];
int k = 0;


#include <FastLED.h>

#define LED_PIN     5
#define COLOR_ORDER GRB
#define CHIPSET     WS2811
#define NUM_LEDS    80

#define BRIGHTNESS  200
#define FRAMES_PER_SECOND 60

bool gReverseDirection = false;

CRGBArray<NUM_LEDS> leds;


void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inputPin1, INPUT); // declare push button as input
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
  pinMode(inputPin4, INPUT);

  delay(3000); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
}

void loop()
{


  timePassed = millis();

  // run simulation frame

  FastLED.show(); // display this frame
  FastLED.delay(1000 / FRAMES_PER_SECOND);

  int pushed = digitalRead(inputPin1);  // read input value
  int pushed2 = digitalRead(inputPin2);
  int pushed3 = digitalRead(inputPin3);
  int pushed4 = digitalRead(inputPin4);

  if (pushed == HIGH && pushed2 == LOW) { // check if the input is HIGH


    counter1++;
    Serial.println(timePassed);

    if (timePassed < 3000 && counter1 > 1 ) {
      blueSnake();

    }
    else {
      redSnake();
    }
  }

  if (pushed2 == HIGH && pushed == LOW) { // check if the input is HIGH
    blueSnake();
    next[k] = 2;
    k++;
    timePassed = 0;
  }
  if (pushed2 == HIGH && pushed == HIGH) { // check if the input is HIGH
    Fire2012();
  }
  if (pushed3 == HIGH) {
    greenSnake();
  }
  if (pushed4 == HIGH) {
    greenSnake();
  }
  else
    digitalWrite(ledPin, HIGH);  // turn LED ON
}

#define COOLING  55
#define SPARKING 120

void Fire2012()
{
  // Array of temperature readings at each simulation cell
  static byte heat[NUM_LEDS];

  // Step 1.  Cool down every cell a little
  for ( int i = 0; i < NUM_LEDS; i++) {
    heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  }

  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for ( int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  }

  // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  if ( random8() < SPARKING ) {
    int y = random8(7);
    heat[y] = qadd8( heat[y], random8(160, 255) );
  }

  // Step 4.  Map from heat cells to LED colors
  for ( int j = 0; j < NUM_LEDS; j++) {
    CRGB color = HeatColor( heat[j]);
    int pixelnumber;
    if ( gReverseDirection ) {
      pixelnumber = (NUM_LEDS - 1) - j;
    } else {
      pixelnumber = j;
    }
    leds[pixelnumber] = color;
  }
}



void Fire3000()
{
  // Array of temperature readings at each simulation cell
  static byte heat[NUM_LEDS];

  // Step 1.  Cool down every cell a little
  for ( int i = 0; i < NUM_LEDS; i++) {
    heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  }

  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for ( int k = NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) * 2;
  }

  // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  if ( random8() < SPARKING ) {
    int y = random8(20);
    heat[y] = qadd8( heat[y], random8(160, 255) );
  }

  // Step 4.  Map from heat cells to LED colors
  for ( int j = 0; j < NUM_LEDS; j++) {
    CRGB color = HeatColor( HUE_PURPLE * heat[j]);
    int pixelnumber;
    if ( gReverseDirection ) {
      pixelnumber = (NUM_LEDS - 1) - j;
    } else {
      pixelnumber = j;
    }
    leds[pixelnumber] = color;
  }
}

void blueSnake() {
  for (int i = 0; i < NUM_LEDS; i++) {
    // fade everything out
    leds.fadeToBlackBy(40);

    // let's set an led value
    leds[i + 2] = CHSV(255, 255, 255);
    leds[i + 1] = CHSV(255, HUE_PURPLE, 255);
    leds[i] = CHSV(HUE_BLUE, 255, 255);

    if (i == NUM_LEDS - 1) {
      leds[i] = CHSV(0, 0, 0);
    }
    // now, let's first 20 leds to the top 20 leds,
    //leds(NUM_LEDS/2,NUM_LEDS-1) = leds(NUM_LEDS/2 - 1 ,0);
    FastLED.delay(5);

  }
}

void redSnake() {
  for (int i = 0; i < NUM_LEDS; i++) {
    // fade everything out
    leds.fadeToBlackBy(10);

    // let's set an led value
    leds[i + 4] = CHSV(HUE_YELLOW, 255, 255);
    leds[i + 3] = CHSV(HUE_PURPLE, 255, 255);
    leds[i + 2] = CHSV(HUE_PURPLE, 255, 255);
    leds[i + 1] = CHSV(HUE_PURPLE, 255, 255);
    leds[i] = CHSV(HUE_RED, 255, 255);

    if (i == NUM_LEDS - 1) {
      leds[i] = CHSV(0, 0, 0);
    }

    // now, let's first 20 leds to the top 20 leds,
    //leds(NUM_LEDS/2,NUM_LEDS-1) = leds(NUM_LEDS/2 - 1 ,0);
    FastLED.delay(2);
  }
}

void greenSnake() {
  for (int i = 0; i < NUM_LEDS; i++) {
    // fade everything out
    leds.fadeToBlackBy(20);

    // let's set an led value
    leds[i + 4] = CHSV(HUE_YELLOW, 255, 255);
    leds[i + 3] = CHSV(HUE_BLUE, 100, 255);
    leds[i + 2] = CHSV(HUE_BLUE, 255, 255);
    leds[i + 1] = CHSV(HUE_GREEN, 255, 255);
    leds[i] = CHSV(HUE_GREEN, 255, 255);

    if (i == NUM_LEDS - 1) {
      leds[i] = CHSV(0, 0, 0);
    }

    // now, let's first 20 leds to the top 20 leds,
    //leds(NUM_LEDS/2,NUM_LEDS-1) = leds(NUM_LEDS/2 - 1 ,0);
    FastLED.delay(1);
  }
}

void randomColors() {
  for (int i = 0; i < NUM_LEDS; i++) {
    // fade everything out
    leds.fadeToBlackBy(20);

    // let's set an led value
    leds[i] = CHSV(random(100, 255), random(100, 255), random(100, 255));

    // now, let's first 20 leds to the top 20 leds,
    //leds(NUM_LEDS/2,NUM_LEDS-1) = leds(NUM_LEDS/2 - 1 ,0);
    FastLED.delay(5);
  }
}

Do you have an idea of how this can be solved?

Yes this crops up several times a week.

You need to remove all delays and rewrite your function as a state machine. Many examples of a state machine for addressable strips have been posted, mainly by me.

Look at the blink without delay example in the IDE. I type that so often that my predictive text on my iPad came up with that whole sentence all by itself.

Ohh wow sorry about that, I should've dived deeper into the archives. But thanks a lot, I'll do that! :slight_smile: