Go Down

Topic: Change color of fade effect on led strip (Read 216 times) previous topic - next topic

SchnoppDog

Hello there,

I was hoping that you could eventually help me. So I got this code for my adafruit dotstar led strip (containing 144 leds/m):
Code: [Select]

#include <Adafruit_DotStar.h>
#include <SPI.h>

#define NUMPIXELS 30
int head = NUMPIXELS;
int tail = 0;

Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BRG);

void setup() {
  strip.begin();
  strip.show();
}

void loop() {
  fade();
}

void fade(){
  for(int i = 5; i < 256; i++){
    for(int j = tail; j <= head; j++){
      strip.setPixelColor(j, i, 0, 0);
    }
    strip.show();
    delay(10);
  }

    for(int i = 255; i >= 5; i--){
    for(int j = tail; j <= head; j++){
      strip.setPixelColor(j, i, 0, 0);
    }
    strip.show();
    delay(10);
  }
  delay(2000);
}


I want to combine it later with another written code. But first I need some help on changing the color of the fade.
I want to hand over the "fade()" function a color like green. And this color should now be faded. When I decide to change the color to red the red color should be faded.

Later it also shall fade with different colors like purple or yellow.

I don't know how to do this. I kinda thought about handing over values for r,g and b, but then I don't know how to tell the fade-function which color I want to use to fade right now.

Do you think you can help me?
Thank you in advance!

Grumpy_Mike

Quote
I want to hand over the "fade()" function a color like green.
So you pass the fade function the colour as three colour components

Code: [Select]
void fade(byte endRed, byte endGreen, byte endBlue){

Then instead of changing just the red component like your code does now you need to change all three components. Do do this you have to know where you are going to start from. Your code so far seems to start with just a very dim red ( red = 5 ) and then gradually increase the colour's brightness.

You need to know not only what colour to start from but where you are going. To do that you have to calculate what the increment from the start colour to the end colour is going to be and how many steps you want to fade it over. For example
Code: [Select]
float redIncrement = abs(startRed - endRed) / numberOfSteps;
Repeat for the other colours. You also need a float variable currentRed, currentGreen and currentBlue.
Then on each step of the fade do:-
Code: [Select]
currentRed += redIncrement;
strip.setPixelColor(j, (byte)currentRed, (byte)currentGreen, (byte)currentBlue);



Go Up