I am working on a led strip project and was looking for ways to smooth transition from color to another. Problem is my current method is not so good.
#include <Adafruit_NeoPixel.h>
#define LED_PIN 25
#define LED_COUNT 60
#define BRIGHTNESS 50
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
bool isFading = false;
unsigned long startTime;
uint32_t fadeDuration = 1000; //fade for 3seconds
uint32_t startingColor = 0x00ffff00; // W = 0x00 R=0xFF G=0xFF B=0x00 yellow
uint32_t targetColor = 0x008f00ff; // W = 0x00 R=0x8F G=0x00 B=0xff violet
void setup() {
pinMode(25, OUTPUT);
digitalWrite(25, LOW);
Serial.begin(9600);
while (!Serial);
Serial.println("Starting");
strip.begin();
strip.show();
strip.setBrightness(BRIGHTNESS);
for (int i = 0; i < strip.numPixels(); i++)
strip.setPixelColor(i, startingColor);
strip.show();
delay(5000); // wait 5 seconds before fading
isFading = true;
startTime = millis();
}
void loop() {
if (isFading) { //Fade from yellow to violet in 3 seconds
for (int i = 0; i < strip.numPixels(); i++)
strip.setPixelColor(i, getFadeColor(startingColor, targetColor, startTime, startTime + fadeDuration));
strip.show();
}
if ( millis() >= startTime + fadeDuration){; // i intentionally did not include an overflow check to keep it simple
Serial.println("Fade ended");
isFading = false;
}
}
uint32_t getFadeColor(uint32_t startColor, uint32_t endColor, unsigned long timeStart, unsigned long timeEnd) {
uint8_t startWhite = (startColor >> 24) & 0xff;
uint8_t startRed = (startColor >> 16) & 0xff;
uint8_t startGreen = (startColor >> 8 ) & 0xff;
uint8_t startBlue = startColor & 0xff;
uint8_t endWhite = (endColor >> 24) & 0xff;
uint8_t endRed = (endColor >> 16) & 0xff;
uint8_t endGreen = (endColor >> 8) & 0xff;
uint8_t endBlue = endColor & 0xff;
unsigned long timeNow = millis();
uint8_t mapTime = map(timeNow, timeStart, timeEnd, 0, 255);
uint8_t white = map(mapTime, 0, 255, startWhite, endWhite);
uint8_t red = map(mapTime, 0, 255, startRed, endRed);
uint8_t green = map(mapTime, 0, 255, startGreen, endGreen);
uint8_t blue = map(mapTime, 0, 255, startBlue, endBlue);
Serial.print("Time: \t");
Serial.print(mapTime);
Serial.print('\t');
Serial.print("Color now: \t");
Serial.println(strip.Color( red, green, blue, white),HEX);
return strip.Color( red, green, blue, white);
}
The problem im having with this code is visually it would seem that it goes from
Yellow -> White -> violet
and it does not look like a proper transition. I have tried with other colors, and it always seems to pass throught the white color
Are there color theories i can apply for the transition? if you can show me an example sketches on how to do the transition that would also be great.