Hello,
I'm a beginner at coding for the Arduino, but I have finished a few projects. I want to be able to have two colors on opposite sides of the LED strip where they start to blend together towards the middle of the strip. I'm using an Arduino mini pro and a WS2812B LED strip. The code that I'm using is code that I found online to fade in and out an LED, but I'm trying to redesign the fade in and out function into blending the colors along the LED strip. I thought to divide the number of LEDs by 256 and multiply that by the individual RGB codes and then multiply it by the LED address. Currently, the LEDs are just flashing in between the two colors instead of showing both of them on opposite sides of the strip and blending them together in the middle. Also, when I get rid of the second color it just flashes the first color and doesn't look like it's fading the color out to one side.
#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 20
#define BRIGHTNESS 64
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop () {
ColorFade1(0x13, 0xff, 0xff);
ColorFade2(0xff, 0x00, 0x00);
}
void ColorFade1(byte red, byte green, byte blue) {
float r,g,b;
for(int i = 1; i < NUM_LEDS; i++) {
r = (NUM_LEDS/256.0)*red*i;
g = (NUM_LEDS/256.0)*green*i;
b = (NUM_LEDS/256.0)*blue*i;
setALL(r,g,b);
FastLED.show();
}
}
void ColorFade2(byte red, byte green, byte blue) {
float r,g,b;
for(int i = 1; i < NUM_LEDS; i++) {
r = (NUM_LEDS/256.0)*red*i;
g = (NUM_LEDS/256.0)*green*i;
b = (NUM_LEDS/256.0)*blue*i;
setALL(r,g,b);
FastLED.show();
}
}
void showStrip() {
FastLED.show();
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifndef ADAFRUIT_NEOPIXEL_H
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setALL(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
}
showStrip();
}
Please let me know if the code I'm using is the right way for what I want it to do or if there is an easier statement to use instead of float. Thank you