Đầu tiên bạn vào Arduino IDE, cài thư viện FastLED
Chọn compile cho Arduino Mega nhé. Fast Led chỉ hổ trợ các chipset sau:
Adafruit's DotStars - AKA the APA102
Adafruit's Neopixel - aka the WS2812B (also WS2811/WS2812/WS2813, also supported in lo-speed mode) - a 3 wire addressable led chipset
TM1809/4 - 3 wire chipset, cheaply available on aliexpress.com
TM1803 - 3 wire chipset, sold by radio shack
UCS1903 - another 3 wire led chipset, cheap
GW6205 - another 3 wire led chipset
LPD8806 - SPI based chpiset, very high speed
WS2801 - SPI based chipset, cheap and widely available
SM16716 - SPI based chipset
APA102 - SPI based chipset
P9813 - aka Cool Neon's Total Control Lighting
DMX - send rgb data out over DMX using arduino DMX libraries
SmartMatrix panels - needs the SmartMatrix library - https://github.com/pixelmatix/SmartMatrix
LPD6803 - SPI based chpiset, chip CMODE pin must be set to 1 (inside oscillator mode)
//1 arduino for 3 animations
//this setting for 150leds : 50 leds for rainbow (FLOWER,BIRD,LOGO)
// 50 leds for colors palette (TREE)
// 50 leds for Random Strip Colors (LOGO CIRCLE)
//more leds can be changed from sketch
//example for 210leds : for( int i = 0; i < 70; i++) { //position led (rainbow)
// for( int i = 70; i < 140; i++) { //position led (colors palette)
// for(int k=140; k<210; k++) { //position led (RandomStripColors)
#include "FastLED.h";
#define DATA_PIN 3
//#define CLK_PIN 4
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 200
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 1
#define BRIGHTNESS 1
extern CRGBPalette16 myRedWhiteBluePalette;
extern CRGBPalette16 myRedWhiteBluePalette1;
extern CRGBPalette16 myRedWhiteBluePalette2;
TBlendType currentBlending;
CRGBPalette16 currentPalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
extern const TProgmemPalette16 myRedWhiteBluePalette1_p PROGMEM;
extern const TProgmemPalette16 myRedWhiteBluePalette2_p PROGMEM;
uint8_t gHue = 0; // rotating "base color" used by both patterns
CRGBPalette16 thisPalette;
CRGBPalette16 thatPalette;
#define qsubd(x, b) ((x>b)?b:0) // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
#define qsuba(x, b) ((x>b)?x-b:0)
unsigned long previousMillis;
uint8_t thishue;
uint8_t thathue;
uint8_t thisrot;
uint8_t thatrot;
uint8_t allsat;
uint8_t thisdir;
uint8_t thatdir;
uint8_t alldir;
int8_t thisspeed;
int8_t thatspeed;
uint8_t allfreq;
int thisphase;
int thatphase;
uint8_t thiscutoff;
uint8_t thatcutoff;
int thisdelay;
uint8_t fadeval;
void setup() {
delay(3000); // 3 second delay for recovery
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
thisPalette = RainbowColors_p;
thatPalette = RainbowColors_p;
resetvars();
}
void loop()
{
int Start1 = 0;
int Length1 = (NUM_LEDS);
int Start2 = 0;
int Length2 = (NUM_LEDS);
int Start3 = 0;
int Length3 = (NUM_LEDS);
drawRainbow( Start1, Length1);
drawColorsPalette( Start2, Length2);
drawRandomStripColors( Start3, Length3);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void drawRainbow(int startpixel, int pixelcount) {
for( int i = 0; i < 50; i++) { //position led (rainbow)
fill_rainbow( leds+ startpixel, pixelcount, gHue, 3);
EVERY_N_MILLISECONDS( 30 ) { gHue--; } //Rainbow speed
}
}
void drawColorsPalette(int startpixel, int pixelcount)
{
ChangePalettePeriodically();
static uint8_t startIndex = 0;
startIndex = startIndex + 1; //PaletteColors motion speed
FillLEDsFromPaletteColors( startIndex);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 50; i < 100; i++) { //position led (colors palette)
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
void ChangePalettePeriodically()
{
uint8_t secondHand = (millis() / 9000) % 90;
static uint8_t lastSecond = 99;
if( lastSecond != secondHand) {
lastSecond = secondHand;
if( secondHand == 0) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; } //Green
if( secondHand == 10) { currentPalette = myRedWhiteBluePalette1_p; currentBlending = LINEARBLEND; } //Green, yellow
if( secondHand == 15) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; } //Green
if( secondHand == 20) { currentPalette = myRedWhiteBluePalette1_p; currentBlending = LINEARBLEND; } //Green, yellow
if( secondHand == 25) { currentPalette = ForestColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 30) { currentPalette = ForestColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 35) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 40) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 45) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 50) { currentPalette = LavaColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 55) { currentPalette = LavaColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 60) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 65) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 70) { currentPalette = myRedWhiteBluePalette2_p; currentBlending = LINEARBLEND; } //red,yellow,green
if( secondHand == 75) { currentPalette = OceanColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 80) { currentPalette = OceanColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 85) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
if( secondHand == 90) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
}
}
// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
for( int i = 0; i < 16; i++) {
currentPalette[i] = CHSV( random8(), 255, random8());
}
}
// This function sets up a palette of black and white stripes,
// using code. Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
// 'black out' all 16 palette entries...
fill_solid( currentPalette, 16, CRGB::Black);
// and set every fourth one to white.
currentPalette[0] = CRGB::White;
currentPalette[4] = CRGB::White;
currentPalette[8] = CRGB::White;
currentPalette[12] = CRGB::White;
}
// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
CRGB purple = CHSV( HUE_PURPLE, 255, 255);
CRGB green = CHSV( HUE_GREEN, 255, 255);
CRGB black = CRGB::Black;
currentPalette = CRGBPalette16(
green, green, black, black,
purple, purple, black, black,
green, green, black, black,
purple, purple, black, black );
}
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
CRGB::White,
CRGB::White, // 'white' is too bright compared to red and blue
CRGB::White,
CRGB::Green,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::Green,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::Green,
CRGB::Green
};
const TProgmemPalette16 myRedWhiteBluePalette1_p PROGMEM =
{
CRGB::White,
CRGB::White, // 'white' is too bright compared to red and blue
CRGB::White,
CRGB::Yellow,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::Green,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::Green,
CRGB::Yellow
};
const TProgmemPalette16 myRedWhiteBluePalette2_p PROGMEM =
{
CRGB::White,
CRGB::White, // 'white' is too bright compared to red and blue
CRGB::Green,
CRGB::Red,
CRGB::White,
CRGB::White,
CRGB::Red,
CRGB::Green,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::White,
CRGB::Green,
CRGB::Red
};
void drawRandomStripColors(int startpixel, int pixelcount) {
EVERY_N_MILLISECONDS(0) {
ChangeMe();
}
thisdir ? thisphase += beatsin8(thisspeed, 4, 20) : thisphase -= beatsin8(thisspeed, 4, 20);
thatdir ? thatphase += beatsin8(thisspeed, 4, 20) : thatphase -= beatsin8(thatspeed, 4, 20);
thishue += thisrot; // Hue rotation is fun for thiswave.
thathue += thatrot; // It's also fun for thatwave.
for (int k=100; k<150; k++) { //position led (RandomStripColors)
int thisbright = qsuba(cubicwave8((k*allfreq)+thisphase), thiscutoff); // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
int thatbright = qsuba(cubicwave8((k*allfreq)+128+thatphase), thatcutoff); // This wave is 180 degrees out of phase (with the value of 128).
leds[k] = ColorFromPalette(thisPalette, thishue, thisbright, currentBlending);
leds[k] += ColorFromPalette(thatPalette, thathue, thatbright, currentBlending);
}
nscale8(leds + startpixel, pixelcount,fadeval);
}
void ChangeMe() {
uint8_t brightness = 255;
uint8_t secondHand = (millis() / 3000) % 60; // speed Change Palette =3000
static uint8_t lastSecond = 99; // Static variable, means it's only defined once. This is our 'debounce' variable.
if( lastSecond != secondHand) {
lastSecond = secondHand;
switch (secondHand) {
case 0: thisrot = 1; thatrot = 1; thisPalette=RainbowColors_p; thatPalette=PartyColors_p; break;
case 5: thisrot = 0; thatdir = 1; thatspeed = -4; thisPalette=ForestColors_p; thatPalette=OceanColors_p; break;
case 10: thatrot = 0; thisPalette=PartyColors_p; thatPalette=RainbowColors_p; break;
case 15: allfreq = 16; thisdir = 1; thathue = 128; break;
case 20: thiscutoff = 96; thatcutoff = 240; break;
case 25: thiscutoff = 96; thatdir = 0; thatcutoff = 96; thisrot = 1; break;
case 30: thisspeed= -4; thisdir = 0; thatspeed= -4; break;
case 35: thiscutoff = 128; thatcutoff = 128; break;
case 40: thisspeed = 3; break;
case 45: thisspeed = 3; thatspeed = -3; break;
case 50: thisspeed = 2; thatcutoff = 96; thiscutoff = 224; thatspeed = 3; break;
case 55: resetvars(); break;
case 60: break;
}
}
}
void resetvars() { // Reset the variable back to the beginning.
thishue = 0; // You can change the starting hue value for the first wave.
thathue = 140; // You can change the starting hue for other wave.
thisrot = 1; // You can change how quickly the hue rotates for this wave. Currently 0.
thatrot = 1; // You can change how quickly the hue rotates for the other wave. Currently 0.
allsat = 255; // I like 'em fully saturated with colour.
thisdir = 0; // Change the direction of the first wave.
thatdir = 0; // Change the direction of the other wave.
alldir = 0; // You can change direction.
thisspeed = 4; // You can change the speed, and use negative values.
thatspeed = 4; // You can change the speed, and use negative values.
allfreq = 32; // You can change the frequency, thus overall width of bars.
thisphase = 0; // Phase change value gets calculated.
thatphase = 0; // Phase change value gets calculated.
thiscutoff = 192; // You can change the cutoff value to display this wave. Lower value = longer wave.
thatcutoff = 192; // You can change the cutoff value to display that wave. Lower value = longer wave.
thisdelay = 10; // You can change the delay. Also you can change the allspeed variable above.
fadeval = 192; // How quickly we fade.
}
Github của FastLed https://github.com/FastLED/FastLED