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.
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();
}
}