1

Learning how to program with Arduino, but the learning curve is out of my league.

I have this

    #include <FastLED.h>
    #define NUM_LEDS 4
    #define DATA_PIN 3

        CRGB leds[NUM_LEDS];

           void setup() { 
       FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
   }

       void loop() {
      // Turn the first led red for 1 second
      leds[0] = CRGB::Green; 
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Green; 
      leds[3] = CRGB::Red;
      FastLED.show( 99 );
      delay(1000);

      // Set the first led back to black for 1 second
      leds[0] = CRGB::Red; 
      leds[1] = CRGB::Green;
      leds[2] = CRGB::Red; 
      leds[3] = CRGB::Green; 
      FastLED.show( 99 );
      delay(1000);
    }

So, I want them to fade from green to red. led 1 is green, and led 2 is red I should probably program this in a different way, but I don't know how. Any ideas are welcome!

1

At the moment you have become used to working with pre-defined named colours, like CRGB::Red and CRGB::Green. Those are just symbolic names for combinations of numeric values for red, green and blue.

So by using numeric values and varying them slightly over time you can create fade effects. I am not personally familiar with that library or class, so I cannot tell you how to do it, but reading the documentation should show you how you can use variables to specify each of the colour components as a single colour.

As for how to fade from red to green - since they are two completely separate colours (they use separate colour channels in the LEDs) there are two main ways of doing it:

  1. Start with red on full and green off. Slowly increase green to get yellow. Then slowly decrease red to get green.
  2. Start with red on full and green off. Slowly increase green while slowly decrease red. At 50% they will cross over.
|improve this answer|||||
1
//start with full red
leds[0].red = 255;

//slowly add green
for( int i=0; i<256; i++ )
{
  leds[0].green = i;
  delay(10);
}

// slowly subtract red
for( int i=255; i>=0; i-- )
{
  leds[0].red = i;
  delay(10);
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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