0

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?

4
  • 1
    Please provide your entire code, which doesn't work. Please tell us what it does, and what you expect/want it to do. Dec 15, 2015 at 22:19
  • the same led fading to blue. Please elaborate.
    – Gerben
    Dec 16, 2015 at 15:04
  • added some new info
    – Niles
    Dec 16, 2015 at 18:46
  • The maximum value is 255 for each RGB value, you are stopping it at 100.
    – ott--
    Dec 16, 2015 at 23:21

1 Answer 1

1

I would do that based on the clock. This lets you run each one independently.

On the off-chance that your project runs that long, the clock rolls over (resets) every 50 days.

I'm going to assume the 55 in your code should read 255.

I would do something like this (this is a code snippet, but should get you started):

const int phase_milliseconds=2500; // each phase lasts 2.5 seconds.
const phase_progress_length=256;   // each phase goes from 0 to 255
const phases=4;                    // numbered from 0 to 3, will repeat forever.

// This is the time since your Arduino turned on
unsigned long time=millis();
// how far we are through the current phase?
int progress= time%phase_milliseconds)*phase_progress_length/phase_milliseconds;

// which phase are we in?
int phase=(time/phase_milliseconds)%phases;

if (phase==0) { // first step
         leds[0] = CRGB(progress,0,0);
         leds[1] = CRGB(0,progress,0);
} else if (phase==1) { // second step
         leds[0] = CRGB(255-progress,0,0);
         leds[1] = CRGB(0,255-progress,0);
} else if (phase==2) { // third step step
         leds[0] = CRGB(0,progress,0);
         leds[1] = CRGB(progress,0,0);
} else if (phase==3) { // fourth step
         leds[0] = CRGB(0,255-progress,0);
         leds[1] = CRGB(255-progress,0,0);
}

Since this is time based, it doesn't matter much how often it runs, you don't need a delay (you can still put one in if you like). You can extend this to more steps.

Also note that I rattled this off, off the top of my head, so there may be errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.