LED Strip colour fade giving strange results.

Hi everyone,

I am currently trying to fade between custom colours using led pixels, driver ws2182. I have successfully managed to fade between straight RGB values with no problem as you can see in the code below. In fact the result is very effective. However as soon as I try and enter a custom RGB value, for example 255,50,0 I start getting strange results. The led begins cycling through the individual colours one at a time rather than fading.

Can anyone offer any advice as to why this is happening?

I am new to Arduino so I appreciate your patience!
Thanks in advance :slight_smile:

#include <Adafruit_NeoPixel.h>
#define PIN 5
#define NUM_LEDS 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

int time = 5;

void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

 
void loop() { 
  FadeInLED0(255, 0, 0, 255, 0, 0); //1 sunrise 6am in 
  FadeInLED0(0,255,0, 255, 0, 0); // 7am
  FadeInLED0(0,0,255, 0, 255, 0); // 7am
  FadeInLED0(255,0,0, 0, 0, 255); // 7am

 
}

void FadeInLED0(byte red, byte green, byte blue, byte Rold, byte Gold, byte Bold)
{
  float r, g, b;
  int m=1;
  int Rdiff=(Rold-red);
  int Gdiff=(Gold-green);
  int Bdiff=(Bold-blue);  
  
  int RStep = Rdiff/256;
  int GStep = Gdiff/256;
  int BStep = Bdiff/256;
  
  for(int k = 0; k < 256; k=k+1) 
  { 
     r=Rold+Rdiff*(k);
     g=Gold+Gdiff*(k);
     b=Bold+Bdiff*(k);
    
    strip.show();
    strip.setPixelColor(0,r,g,b);
    delay (time);
    Serial.println (g);
  }}

Don't you want to use the xStep variables to change between values? :wink:

But you're doing int math. So for example:
is diff = 100 then
100/256 gives you 0,39 which is plain 0 in integer math.
You can use map to do

color = map(k, 0, 255, oldVal, newVal);

Also, drop the float. You're doing nice integer math (which is gooood).

it worked :D! Thanks so much, you have saved my brain from exploding!

re: "Don't you want to use the xStep variables to change between values? ;)"

As my xstep values wasn't working and xdiff was I rolled with it for as long as I could. Which wasn't very long considering the maths was all wrong! haha!

Thanks again!!