#include <FastLED.h>
// MAX_MILLIWATTS can only be changed at compile-time. Use 0 to disable limit.
// Brightness can be changed at runtime via serial with 'b' and 'B'
#define MAX_MILLIWATTS 0
#define BRIGHTNESS 255
#define LED_PIN 2
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define NUM_LEDS 13 //((kMatrixWidth) * (kMatrixHeight))
int fadeAmount = 5; // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
int _brightness = 0;
CRGB leds[NUM_LEDS];
int x, a = 0;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
uint8_t hue = 0;
void color(int x, int a);
CRGBPalette16 myPalette(
CRGB::Black,
CRGB::Black,
CRGB::Red,
CRGB::Yellow,
CRGB::Green,
CRGB::Blue,
CRGB::Purple,
CRGB::Black,
0x100000,
0x200000,
0x400000,
0x800000,
CHSV( 30,255,255),
CHSV( 50,255,255),
CHSV( 70,255,255),
CHSV( 90,255,255)
);
uint8_t paletteIndex = 0;
DEFINE_GRADIENT_PALETTE ( heatmap_gp ) {
0, 0, 0, 0, // nero
128, 255, 0, 0, // rosso
200, 255, 255, 0, // giallo acceso
255, 255, 255, 255 // bianco
};
CRGBPalette16 myPal = heatmap_gp;
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setCorrection(UncorrectedColor);
FastLED.setTemperature(UncorrectedTemperature);
FastLED.setDither(DISABLE_DITHER);
// FastLED.setMaxRefreshRate(400);
if (MAX_MILLIWATTS > 0) FastLED.setMaxPowerInMilliWatts(MAX_MILLIWATTS);
resetStrip();
}
void loop() {
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue=gHue+2; } // slowly cycle the "base color" through the rainbow
//test 1 OK
// semplice();
//test 2 OK 13
//color(24, a); a++;
//test 3
//my_pal(); NON VA
//test 4 OK
//rainbow();
//test 5 OK 13
//dual();
//test 6 OK
//dual2();
//test 7 OK 13
//rotazione();
//test 8 OK 13
// FadeUp(25);
//test 9 OK 13
// OndeSfumate();
//test 10 ok 13
//theaterChaseRainbow(150);
//test 11 OK 13
//RunningLights(0x00,0x00,0xff, 80); // blue
//test 12 OK 13
rainbowCycle(50);
//test 13 da rivedere i parametri, pochi lampeggi
//SnowSparkle(0x00, 0x00, 0x00, 20, random(100,800));
//test 14 così così
//CylonBounce(0xff, 0x00, 0x00, 2, 10, 50);
//(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay)
//test 15 KO
//oneled();
//test 16 _test
//_blur1d();
//test 17 OK 13
//arcobaleno();
//test 18 OK 13
//_blu();
}
void oneled(){
for (int i = 1; i < NUM_LEDS; i++)
{ leds[i].CRGB::BlueViolet;
FastLED.show();
delay(1000);
leds[i].CRGB::Black;
}
}
void color(int x, int a){
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].setHue(i*x+(sin8(i+a)));
}
FastLED.show();
delay(25);
}
void semplice(){
{
leds[x]=CRGB::Red;
leds[(x+3)%12]=CRGB(51, 255, 0);
leds[(x+6)%12]=CRGB(8, 0, 255);
leds[(x+9)%12]=CRGB(255, 251, 0);
}
FastLED.show();
delay(250);
{
//leds[x]=CRGB::Black;
//leds[(x+3)%12]=CRGB::Black;
//leds[(x+6)%12]=CRGB::Black;
//leds[(x+9)%12]=CRGB::Black;
}
FastLED.clear();
x++;
x=1+x%12;
}
void my_pal(){
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(myPalette, i);
}
FastLED.show();
delay(250);
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
FastLED.show();
}
void dual()
{
for (int i = 0; i < 7; i++)
{
switch(i)
{
case 0: leds[1] = CRGB::White;
break;
case 1: leds[2] = CRGB::White;
leds[12] = CRGB::White;
break;
case 2: leds[2] = CRGB::White;
leds[3] = CRGB::White;
leds[12] = CRGB::White;
leds[11] = CRGB::White;
break;
case 3: leds[2] = CRGB::White;
leds[3] = CRGB::White;
leds[4] = CRGB::White;
leds[12] = CRGB::White;
leds[11] = CRGB::White;
leds[10] = CRGB::White;
break;
case 4: leds[2] = CRGB::White;
leds[3] = CRGB::White;
leds[4] = CRGB::White;
leds[5] = CRGB::White;
leds[12] = CRGB::White;
leds[11] = CRGB::White;
leds[10] = CRGB::White;
leds[9] = CRGB::White;
break;
case 5: leds[2] = CRGB::White;
leds[3] = CRGB::White;
leds[4] = CRGB::White;
leds[5] = CRGB::White;
leds[6] = CRGB::White;
leds[12] = CRGB::White;
leds[11] = CRGB::White;
leds[10] = CRGB::White;
leds[9] = CRGB::White;
leds[8] = CRGB::White;
break;
case 6: leds[2] = CRGB::White;
leds[3] = CRGB::White;
leds[4] = CRGB::White;
leds[5] = CRGB::White;
leds[6] = CRGB::White;
leds[7] = CRGB::White;
leds[12] = CRGB::White;
leds[11] = CRGB::White;
leds[10] = CRGB::White;
leds[9] = CRGB::White;
leds[7] = CRGB::White;
break;
}
FastLED.show();
delay(500);
}
delay(2000);
FastLED.clear();
}
void dual2()
//Versione leggera di dual()
{
//Discesa
for (int i = 0; i < 7; i++)
{
leds[i+1] = CRGB::White;
leds[((12-i)%12)+1] = CRGB::White;
FastLED.show();
delay(500);
}
delay(2000);
//Salita
for (int i = 6; i >=0; i--)
{
leds[i] = CRGB::Black;
leds[((12-i)%12)+1] = CRGB::Black;
FastLED.show();
delay(500);
}
delay(2000);
}
void rotazione()
{
for(x=0; x<5; x++)
for (int i = 1; i <7; i++) {
{
leds[i] = CRGB(51, 255, 0);
leds[i+6] = CRGB(51, 255, 0);
FastLED.show();
delay(500);
leds[i] = CRGB::Black;
leds[i+6] = CRGB::Black;
}
}
}
void FadeUp(int x) //Fading up and down using fadeLightBy
{
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i].setHue(i*x+(sin8(i+a)));
//leds[i].setRGB(50,255,250); // Set Color HERE!!!
leds[i].fadeLightBy(_brightness);
}
FastLED.show();
_brightness = _brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if(_brightness == 0 || _brightness == 255)
{
fadeAmount = -fadeAmount ;
a=a+5;
}
delay(9); // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
}
void OndeSfumate() //https://www.moreware.org/wp/blog/2021/05/28/led-ws2812-e-arduino-onde-e-sfumature-variopinte/
{
fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, myPal, 255, LINEARBLEND);
EVERY_N_MILLISECONDS(15) {
paletteIndex++;
}
FastLED.show();
}
// used by rainbowCycle and theaterChaseRainbow
byte * Wheel(byte WheelPos) {
static byte c[3];
if(WheelPos < 85) {
c[0]=WheelPos * 3;
c[1]=255 - WheelPos * 3;
c[2]=0;
} else if(WheelPos < 170) {
WheelPos -= 85;
c[0]=255 - WheelPos * 3;
c[1]=0;
c[2]=WheelPos * 3;
} else {
WheelPos -= 170;
c[0]=0;
c[1]=WheelPos * 3;
c[2]=255 - WheelPos * 3;
}
return c;
}
void theaterChaseRainbow(int SpeedDelay) {
byte *c;
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=1; i < NUM_LEDS; i=i+3) {
c = Wheel( (i+j) % 255);
setPixel(i+q, *c, *(c+1), *(c+2)); //turn every third pixel on
}
leds[0]=leds[1];
showStrip();
delay(SpeedDelay);
for (int i=1; i < NUM_LEDS; i=i+3) {
setPixel(i+q, 0,0,0); //turn every third pixel off
//leds[i+q].fadeLightBy(96);
}
}
}
}
void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
int Position=0;
for(int x=0; x<NUM_LEDS*2; x++)
{
Position++; // = 0; //Position + Rate;
for(int i=0; i<NUM_LEDS; i++) {
// sine wave, 3 offset waves make a rainbow!
//float level = sin(i+Position) * 127 + 128;
//setPixel(i,level,0,0);
//float level = sin(i+Position) * 127 + 128;
setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
((sin(i+Position) * 127 + 128)/255)*green,
((sin(i+Position) * 127 + 128)/255)*blue);
}
showStrip();
delay(WaveDelay);
}
}
void rainbowCycle(int SpeedDelay) {
byte *c;
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< NUM_LEDS; i++) {
c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
setPixel(i, *c, *(c+1), *(c+2));
}
showStrip();
delay(SpeedDelay);
}
}
void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
setAll(red,green,blue);
int Pixel = random(NUM_LEDS);
setPixel(Pixel,0xff,0xff,0xff);
showStrip();
delay(SparkleDelay);
setPixel(Pixel,red,green,blue);
showStrip();
delay(SpeedDelay);
}
void CylonBounce(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay){
for(int i = 0; i < NUM_LEDS-EyeSize-2; i++) {
setAll(0,0,0);
setPixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= EyeSize; j++) {
setPixel(i+j, red, green, blue);
}
setPixel(i+EyeSize+1, red/10, green/10, blue/10);
showStrip();
delay(SpeedDelay);
}
delay(ReturnDelay);
for(int i = NUM_LEDS-EyeSize-2; i > 0; i--) {
setAll(0,0,0);
setPixel(i, red/10, green/10, blue/10);
for(int j = 1; j <= EyeSize; j++) {
setPixel(i+j, red, green, blue);
}
setPixel(i+EyeSize+1, red/10, green/10, blue/10);
showStrip();
delay(SpeedDelay);
}
delay(ReturnDelay);
}
void _blur1d() { //https://www.moreware.org/wp/blog/2021/05/28/led-ws2812-e-arduino-onde-e-sfumature-variopinte/
EVERY_N_SECONDS(1) {
for (int i = 0; i < 4; i++) {
blur1d(leds, NUM_LEDS, 150);
}
}
EVERY_N_SECONDS(10) {
resetStrip();
}
FastLED.show();
}
void arcobaleno() { //https://www.moreware.org/wp/blog/2021/05/14/led-ws2812-e-arduino-altre-animazioni-sui-colori/
for (int i = 0; i < NUM_LEDS; i++) {
// Modificando il valore di MAX si modifica la saturazione e la luminosità del colore
// (vedi puntata precedente)
leds[i] = CHSV(hue + (i * 10), 255, 255);
}
// Nell'esempio, il valore della tinta viene cambiato ogni 40 millisecondi
// Consente di gestire la velocità nella transizione del colore
EVERY_N_MILLISECONDS(40) {
// la variabile hue è di tipo unsigned integer 8 bit,
// quindi quando supera il valore di 255 torna a zero
hue++;
}
FastLED.show();
}
void _blu() {
EVERY_N_MILLISECONDS(50) {
// Creiamo un pixel per il led in posizione 0
leds[0] = CHSV(160, random8(), random8(100, 255));
// copiamo il led appena creato all'estremità opposta,
// simulando un movimento sulla striscia
for (int i = NUM_LEDS - 1; i > 0; i--) {
leds[i] = leds[i - 1];
}
}
FastLED.show();
}
// ***************************************
// ** FastLed/NeoPixel Common Functions **
// ***************************************
// Apply LED color changes
void showStrip() {
/* #ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif */
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
// Set a LED color (not yet visible)
void setPixel(int Pixel, byte red, byte green, byte blue) {
/* #ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif */
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
void resetStrip() {
// Inizializziamo la striscia con righe rosse, verdi e blu
fill_solid(leds, NUM_LEDS, CRGB::Red);
fill_solid(leds, (NUM_LEDS / 3) * 2, CRGB::Green);
fill_solid(leds, (NUM_LEDS / 3), CRGB::Blue);
}