Hi everyone.
I am trying to have a specific sequence of colors changing on my led strip one after another with each color having different duration time. Also I want to do this with millis cause I will integrate it in bigger code and I don't want it to be frozen because of delay. I have managed to do transition with brightness staying same all the time and it works fine (code below):
#include <FastLED.h>
#define NUM_LEDS 20
#define DATA_PIN 5
#define BRIGHTNESS 127
CRGB leds[NUM_LEDS];
const unsigned long period_1 = 1000;
const unsigned long period_2 = 2000;
const unsigned long period_3 = 3000;
unsigned long currentTime = 0;
unsigned long prevTime = 0;
int color = 0;
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(9600);
}
void loop() {
currentTime = millis();
switch (color) {
case 0:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(9, 151, 95);}
FastLED.show();
for(brightness = 0; brightness<=130; brightness+fadeAmount)
{
}
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 1:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(0, 255, 50);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 2:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(95, 158, 200);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 3:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(100, 157, 240);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 4:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(65, 166, 179);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 5:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(100, 149, 237);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 6:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(0, 71, 171);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 7:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(20, 52, 164);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 8:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(128, 0, 128);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 9:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(170, 51, 106);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 10:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(103, 49, 71);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 11:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(159, 43, 104);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 12:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(75, 0, 130);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 13:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(93, 63, 211);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 14:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(170, 51, 106);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 15:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(251, 150, 50);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 16:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(255, 255, 0);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 17:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(255, 234, 0);}
FastLED.show();
if(currentTime - prevTime >= period_3)
{
prevTime = currentTime;
color++;
}
break;
case 18:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(228, 208, 10);}
FastLED.show();
if(currentTime - prevTime >= period_2)
{
prevTime = currentTime;
color++;
}
break;
case 19:
for(int i=0;i<NUM_LEDS;i++){
leds[i].setRGB(223, 255, 0);}
FastLED.show();
if(currentTime - prevTime >= period_1)
{
prevTime = currentTime;
color++;
}
break;
case 20:
color=0;
break;
}
}
So now I want to have smoother transitions between colors with sequence as follows: color1 fades in from 0 to 130 brightness, stays at 130 brightness for some period of time (period 1,2 or 3), color1 fades out to 0 brightness, color2 fades in from 0 to 130 brightness, etc. I have managed to figure out how to fade in and out single color using delay (code below) but like I mentioned earlier I dont want to use delay cause it freezes code. I am having trouble putting in fade effect using millis. I would have same steps of fading (5) for same amount of times between them (30ms).
#include "FastLED.h"
#define NUM_LEDS 20
CRGB leds[NUM_LEDS];
#define PIN 5
int fadeAmount = 5; // Set the step of fade.
int brightness = 0;
void setup()
{
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUM_LEDS);
}
void loop()
{
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i].setRGB(0,255,250);
leds[i].fadeLightBy(brightness);
}
FastLED.show();
brightness = brightness + fadeAmount;
if(brightness == 0 || brightness == 130)
{
fadeAmount = -fadeAmount ;
}
delay(30); // This delay sets speed of the fade.
}
Could anyone help me out to figure this out?
Thanks in advance