What I would like to do is, step 1:
led1 fades from black(off) to green (max-on 255)
led2 fades from black(off) to red (max-on 255)
this happens at the same time. If the max value is reached, then both leds fade out (to black, off) so step 2:
led1 fades from green to black
led2 fades from red to black
Then step 3, almost same as step 1 but opposite colours:
led1 fades from black(off) to red(max-on 255)
led2 fades from black(off) to green(max-on 255)
And step 4, both fade to black
led1 fades from green to black
led2 fades from red to black
This is the code that fades led 1/3/5 from red to black and back to red and led 2/4/6 from green to black, and back.
#include <FastLED.h>
#define NUM_LEDS 6
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop()
{
int x = 1;
for (int i = 1; i > -1; i = i + x){
leds[0] = CRGB(i,0,0);
leds[1] = CRGB(0,i,0);
leds[2] = CRGB(i,0,0);
leds[3] = CRGB(0,i,0);
Serial.println(i);
FastLED.show();
if (i == 55) x = -1; // switch direction at peak
delay(100);
}
}
****Original question****
I am trying to fade a led (WS2812B) from black to green or red, and back to black. (that is working) But now I want the same led fading to blue.
How could I do that? I was trying a if..else but can't figure it out Or should I count the for loops?
This is the starting code I am using
maybe something like
if (i==0) { leds[0] = CRGB(i,0,0); leds[1] = CRGB(0,i,0); leds[2] = CRGB(i,0,0); leds[3] = CRGB(0,i,0); } else { leds[0] = CRGB(0,0,i); leds[1] = CRGB(0,0,i); leds[2] = CRGB(0,0,i); leds[3] = CRGB(0,0,i); } FastLED.show(); if (i == 100) x = -1; delay(100);
But that is not working. Any suggestions?
the same led fading to blue
. Please elaborate.