So I'm building my first Arduino project, and I'm attempting to use addressable LEDs to run a number of different light patterns at the same time. Basically, a couple of buttons control which LEDs are on, and which pattern certain LEDs are doing. The different patterns are each individual parts of a larger project, so I guess the key distinction is that when searching for information, a lot of things I came across were simply how to make an LED strip do one thing, whereas some of these LEDs are going to be over here flickering like candles, others are going to be pulsing like glowing eyes, etc.
Basically, the fade is where I'm having the problem. I've been getting a number of strange outcomes with it. Sometimes it will just stay 100% solid on, sometimes it will do completely random colors, other times it will fade on and off by at, for lack of a better term, a much slower frame rate than it should be.
The issue is exacerbated by the amount of LEDs in use. The first fade() function only used two, but when I tried the larger ones later (frame_blood(), etc.) I noticed the issues above. I retroactively increased the LED count in fade() and it did similar things. But the more LEDs it needs, the slower things get. Like, you can see the LEDs "run" as they turn on and then do whatever wrong thing they are going to do, mentioned above.
The thing that gets me is that the code is simple, and the candle flickering parts, to me, seem more computationally demanding than "increment this fade value by one and use it for all of the LEDs in question." I eventually swapped over to a sinusoidal function to cut down on extraneous if statements and it made no difference.
So here's ALL my code, but the areas that are the problem are fade(), fade_sin(), frame_blood(), and frame_blood_sin(). Any suggestions would be wonderful. And I apologize that my code isn't super clean - it's the result of a ton of experimentation and I'm going to revamp it once I get the actual hardware solidified more.
// ---------- EYES ----------
unsigned long fade() {
unsigned long currentFadeMillis = millis();
if (currentFadeMillis - previousFadeMillis >= 35) {
previousFadeMillis = currentFadeMillis;
for (int i = CANDLE_LEDS; i <= CANDLE_LEDS + EYES_LEDS - 1; i++){
if (fadeInFlag == 0 && fadeValue < 150) {
leds[i] = CRGB(fadeValue, 0, 0);
fadeValue++;
}
else if (fadeInFlag == 0 && fadeValue == 150) {
fadeInFlag = 1;
}
else if (fadeInFlag == 1 && fadeValue > 0) {
leds[i] = CRGB(fadeValue, 0, 0);
fadeValue--;
}
else if (fadeInFlag == 1 && fadeValue == 0) {
fadeInFlag = 0;
}
else{
}
FastLED.show();
}
return previousFadeMillis;
}
}
unsigned long fade_sin() {
unsigned long currentFadeMillis = millis();
if (currentFadeMillis - previousFadeMillis >= 20) {
previousFadeMillis = currentFadeMillis;
timer = millis(); // updating time
int ledValue = OFFSET + AMPLITUDE*(cos(OMEGA*timer)+PHASE);
for (int i = 0; i <= CANDLE_LEDS + EYES_LEDS - 1; i++){
leds[i] = CHSV(0, 255, ledValue);
FastLED.show();}
return previousCandleMillis;
}
}
unsigned long frame_blood_sin() {
unsigned long currentFadeMillis = millis();
if (currentFadeMillis - previousFadeMillis >= 20) {
previousFadeMillis = currentFadeMillis;
timer = millis(); // updating time
int ledValue = OFFSET + AMPLITUDE*(cos(OMEGA*timer)+PHASE);
for (int i = 30; i < 64; i++) {
leds[i] = CHSV(0, 255, ledValue);
FastLED.show();}
return previousCandleMillis;
}
}
unsigned long frame_blood() {
unsigned long currentFadeMillis2 = millis();
if (currentFadeMillis2 - previousFadeMillis2 >= 20) {
previousFadeMillis2 = currentFadeMillis2;
if (fadeInFlag2 == 0 && fadeValue2 < 150) {
fadeValue++;
}
else if (fadeInFlag2 == 0 && fadeValue2 == 150) {
fadeInFlag2 = 1;
}
else if (fadeInFlag2 == 1 && fadeValue2 > 0) {
fadeValue--;
}
else if (fadeInFlag2 == 1 && fadeValue2 == 0) {
fadeInFlag2 = 0;
}
else{
}
// for (int i = CANDLE_LEDS + EYES_LEDS + BLOOD_LEDS; i < CANDLE_LEDS + EYES_LEDS + BLOOD_LEDS + FRAME_LEDS; i++) {
for (int i = 30; i < 64; i++) {
leds[i] = CHSV(50, 255, fadeValue2);
FastLED.show();
}
return previousFadeMillis2;
}
}
One other question: in my candle functions you can see me using random(#, #) in the CHSV command to change the brightness and the hue values. I'm wondering if there is a way I can introduce a bias? For instance, I want the candles to be primarily red, but I want hints of orange in there, but not with equal probability. Or I want a range of brightnesses, but every once in a while, I want it to spark up a little brighter. What is the easiest way to achieve something like this?
Thank you in advance for any help you can provide!