Running patterns X amount of times

Hi guys, I am currently working on a project. I would like to run one pattern for 5 times (Cylon effect) and run the other pattern one time (rainbow effect) and have them go in a loop. The code works perfectly when I have only one pattern but I am confused on how to get them work together. Please help me. I really tried but I can't make it work.

#include <FastLED.h>
#define DATA_PIN 3

#define NUM_LEDS 32
#define NUM_PATTERN1 10
#define NUM_PATTERN2 10
CRGB leds[NUM_LEDS];



void setup() { 
Serial.begin(9600);
Serial.println("resetting");
LEDS.addLeds<WS2812,DATA_PIN,GRB>(leds,NUM_LEDS);
LEDS.setBrightness(200);
}
void pattern1() {
uint8_t hue = 0;
for(int i = 0; i < NUM_LEDS; i++) {
uint8_t mappedHue = map(i, 0, NUM_LEDS-1, 1, 250);
leds[i] = CHSV(mappedHue, 255, 255);
FastLED.show();
fadeall();
delay(70);
            }
}
void pattern2() {
          for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
    leds[i] = CHSV(hue, 255, 255);
  }

  EVERY_N_MILLISECONDS(125){
    hue--;
  }
            
      
        }
void loop() {
 for(int i=0; i<NUM_PATTERN1; i++) {
    pattern1();
  }

  for(int i=0; i<NUM_PATTERN2; i++) {
    pattern2();
 }

}

Something like that, not tested.

Thank you for your reply. It's telling me that fadeall () and 'hue' have not been declared in this scope.

I posted an idea and or concept that you could sue to get your code doing the thing you want. I wrote that the code is untested and figured you'd be able to troubleshoot it. If you want help troubleshooting your efforts then post your efforts and describe the issue you ran into.

I guess that you based your code on something existing that had a fadeall() function defined. So find that back and copy the function into your sketch.

It doesn't if you remove pattern1 and the one pattern that you have is pattern2.

Look at pattern1(); the first line in that function declares the hue variable. That variable is only known inside the function. You don't have that line in pattern2() and hence you get the error.

your definition of the variable hue is in the wrong place. It is used at function pattern2 and not inf function pattern1. The function fadeall should be defined. My guess is you copied some code but forgot to copy this portion. Try the code below:

#include <FastLED.h>
#define DATA_PIN 3

#define NUM_LEDS 32
#define NUM_PATTERN1 10
#define NUM_PATTERN2 10
CRGB leds[NUM_LEDS];



void setup() {
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(200);
}
void pattern1() {
  //GERRY MOD
  //  uint8_t hue = 0;
  for (int i = 0; i < NUM_LEDS; i++) {
    uint8_t mappedHue = map(i, 0, NUM_LEDS - 1, 1, 250);
    leds[i] = CHSV(mappedHue, 255, 255);
    FastLED.show();
    fadeall();
    delay(70);
  }
}
void pattern2() {
  //GERRY MOD
  uint8_t hue = 0;

  for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
    leds[i] = CHSV(hue, 255, 255);
  }

  EVERY_N_MILLISECONDS(125) {
    hue--;
  }


}
void loop() {
  for (int i = 0; i < NUM_PATTERN1; i++) {
    pattern1();
  }

  for (int i = 0; i < NUM_PATTERN2; i++) {
    pattern2();
  }

}

//GERRY MOD
void  fadeall() {
  for (int i = 0; i < NUM_LEDS; i++)
    leds[i].maximizeBrightness(250) ;
  FastLED.show();
  delay(100);
}

Thank you for your help. So i did some modifications to the code as per your help. Now my issue is that the second pattern (Rainbow effect) is on for only approximately 100ms. How can I make it stay on this effect for about 5 seconds? My new code is as follows

#include <FastLED.h>
#define DATA_PIN 3

#define NUM_LEDS 32
#define NUM_PATTERN1 4
#define NUM_PATTERN2 1
CRGB leds[NUM_LEDS];

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(120); } }

void setup() {
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(200);
}

void pattern1() {
//GERRY MOD
//  uint8_t hue = 0;  
  for (int i = 0; i < NUM_LEDS; i++) {
    uint8_t mappedHue = map(i, 0, NUM_LEDS - 1, 1, 250);
    leds[i] = CHSV(mappedHue, 255, 255);
    FastLED.show();
    fadeall();
    delay(70);
  }
  delay(500);
}
void pattern2() {
//GERRY MOD
  uint8_t hue = 0;  
  
  for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
    leds[i] = CHSV(hue, 255, 255);
  }

  EVERY_N_MILLISECONDS(125) {
    hue--;
  }


}
void loop() {
  for (int i = 0; i < NUM_PATTERN1; i++) {
    pattern1();
  }

  for (int i = 0; i < NUM_PATTERN2; i++) {
    pattern2();
  }

}

You don't have a FastLED.show() in pattern2() so it will never update the strip with the values that you set in the for-loop.

the colour unfortunately is stucked on red and therefore I am not getting the rainbow effect.

#include <FastLED.h>
#define DATA_PIN 3
#define NUM_LEDS 32
#define NUM_PATTERN1 4
#define NUM_PATTERN2 1
CRGB leds[NUM_LEDS];

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(120); } }

void setup() {
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(200);
}

void pattern1() {

uint8_t hue = 0;  
  for (int i = 0; i < NUM_LEDS; i++) {
    uint8_t mappedHue = map(i, 0, NUM_LEDS - 1, 1, 250);
    leds[i] = CHSV(mappedHue, 255, 255);
    FastLED.show();
    fadeall();
    delay(70);
  }
  delay(500);
}
void pattern2() {
  uint8_t hue = 0;  
    for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
    leds[i] = CHSV(hue, 255, 255);
  }
  EVERY_N_MILLISECONDS(125) {
    hue--;
  }
FastLED.show();
delay (5000);
}
void loop() {
  for (int i = 0; i < NUM_PATTERN1; i++) {
    pattern1();
  }

  for (int i = 0; i < NUM_PATTERN2; i++) {
    pattern2();
  }
}
  1. removed delay(5000) as EVERY_N_MILLISECONDS should be in a loop so used a while loop
  2. changed hue--; to hue = hue - 6; so the difference int the color can be noticed
  3. placed your code inside EVERY_N_MILLISECONDS

try the code below

#include <FastLED.h>
#define DATA_PIN 3
#define NUM_LEDS 32
#define NUM_PATTERN1 4
#define NUM_PATTERN2 1
CRGB leds[NUM_LEDS];

void fadeall() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].nscale8(120);
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  LEDS.setBrightness(200);
}

void pattern1() {

  uint8_t hue = 0;
  for (int i = 0; i < NUM_LEDS; i++) {
    uint8_t mappedHue = map(i, 0, NUM_LEDS - 1, 1, 250);
    leds[i] = CHSV(mappedHue, 255, 255);
    FastLED.show();
    fadeall();
    delay(70);
  }
  delay(500);
}
void pattern2() {
  uint8_t hue = 0;
  /* GERRY MOD
  for (int i = 0; i < NUM_LEDS; i++) {
    //leds[i] = CHSV(hue, 255, 255);
    leds[i] = CHSV(hue, 255, 255);
  }
  EVERY_N_MILLISECONDS(125) {
    hue--;
  }
  FastLED.show();
  delay (5000);
  */
  //GERRY MOD
  unsigned long int prevTime = millis();
  while (millis() - prevTime < 5000) {
    EVERY_N_MILLISECONDS(125) {
      hue = hue - 6;   // 255 / 5000 * 125
      for (int i = 0; i < NUM_LEDS; i++) {
        //leds[i] = CHSV(hue, 255, 255);
        leds[i] = CHSV(hue, 255, 255);
      }
      FastLED.show();
    }
  }
  
}
void loop() {
  for (int i = 0; i < NUM_PATTERN1; i++) {
    pattern1();
  }

  for (int i = 0; i < NUM_PATTERN2; i++) {
    pattern2();
  }
}

Thank you so much

1 Like

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