/*
CALENDARIO DELL'AVVENTO CON LED NEOPIXEL
ULTIMA MODIFICA 22/09/2024
SERTECH 2024
*/
#include <FastLED.h>
#include <OneButton.h>
// Dimensione array
#define numeriCalendario 24
#define dataPin 2
#define numLeds 24
#define maxPower 500
#define ledType WS2812B
#define colorLed GRB
#define brightness 70
CRGB leds[numLeds];
#define ledColor 8
//array iniziale
int posizioneLed[numeriCalendario];
int coloreLed[ledColor];
uint32_t colori[ledColor] = {0xFF0000, 0xFF4500, 0xFFFF00, 0x32CD32, 0x00FFFF, 0x0000FF, 0x800080, 0xDA70D6};
/*
Colore 1: RED
Colore 2: OrangeRed
Colore 3: Yellow
Colore 4: LimeGreen
Colore 5: Cyan
Colore 6: Blue
Colore 7: Purple
Colore 8: Orchid
*/
int ledInc = 0;
int coloreInc = 0;
int index = 0;
int indexColore = 0;
#define selButton 3
OneButton btn1(selButton, true);
void setup() {
Serial.begin(9600);
// Popola array
popolaArray(posizioneLed, numeriCalendario);
popolaArrayColore(coloreLed, ledColor);
// Mescola array
shuffle(posizioneLed, numeriCalendario);
shuffleColor(coloreLed, ledColor);
// Stampa array mescolati
printa(posizioneLed, numeriCalendario);
printaColor(coloreLed, ledColor);
FastLED.addLeds<ledType, dataPin, GRB >(leds, numLeds);
FastLED.setCorrection(TypicalLEDStrip);
FastLED.setMaxPowerInVoltsAndMilliamps(5, maxPower);
FastLED.setBrightness(brightness);
btn1.attachClick(click1);
pinMode(4, OUTPUT);
delay(1000);
}
void loop() {
btn1.tick();
digitalWrite(4, HIGH);
}
//********************************* PULSANTE INCREMENTO LED CALENDARIO *********************************//
void click1(){
if(index > 24){index = 0;}
digitalWrite(4, LOW);
ledInc = posizioneLed[index];
coloreInc = coloreLed[indexColore];
leds[ledInc-1] = colori[coloreInc-1];
FastLED.show();
if (index < 23){
index++;
} else {index = 0;}
if(indexColore < 7){
indexColore++;
}else{ indexColore = 0;}
delay(200);
}
//********************************* POPOLO ARRAY POSIZIONI LED E COLORI *********************************//
void popolaArray(int *n, const int len){
for (int i = 0; i < len; i++) {
n[i] = i+1;
}
}
void popolaArrayColore(int *n, const int len){
for (int i = 0; i < len; i++){
n[i] = i+1;
}
}
//********************************* STAMPO GLI ARRAY MESCOLATI *********************************//
void printa(int *n, const int len){
for (int i = 0; i < len; i++) {
Serial.print(n[i]);
Serial.print("\t");
}
Serial.println("");
}
void printaColor(int *n, const int len){
for (int i = 0; i < len; i++) {
Serial.print("Colori");
Serial.print(n[i]);
Serial.print("\t");
}
Serial.println("");
}
//********************************* MESCOLO GLI ARRAY LED E COLORI *********************************//
void shuffle(int *n, const int len){
randomSeed(analogRead(A0));
for (int i = 0; i < len*3; i++){
//indice a caso tra 0 e len-2
int j = random(len-1);
//scambio gli elementi
int t = n[j];
n[j] = n[j+1];
n[j+1] = t;
}
}
void shuffleColor(int *n, const int len){
randomSeed(analogRead(A0));
for (int u = 0; u < len*2; u++){
//indice a caso tra 0 e len-2
int y = random(len-1);
//scambio gli elementi
int f = n[y];
n[y] = n[y+1];
n[y+1] = f;
}
}