#include <Adafruit_NeoPixel.h>

#define DIN_PIN_1 4
#define NUM_PIXELS_1 4
#define DIN_PIN_2 5
#define NUM_PIXELS_2 4
#define DIN_PIN_3 6
#define NUM_PIXELS_3 4

Adafruit_NeoPixel pixels_1(NUM_PIXELS_1, DIN_PIN_1);
Adafruit_NeoPixel pixels_2(NUM_PIXELS_2, DIN_PIN_2);
Adafruit_NeoPixel pixels_3(NUM_PIXELS_3, DIN_PIN_3);

int32_t colors[] = {
  pixels_1.Color(255, 0, 0),    // Red
  pixels_1.Color(255, 255, 0),  // Yellow
  pixels_1.Color(0, 255, 0),    // Green
  pixels_1.Color(0,0,0), //Black
  pixels_2.Color(255, 0, 0),    // Red
  pixels_2.Color(255, 255, 0),  // Yellow
  pixels_2.Color(0, 255, 0),    // Green
  pixels_2.Color(0,0,0), //Black
};


struct colore { // definizione della struttura dell'array colore
    char messaggio[50];
    int led_colore;
};

struct colore Colori[4] = { // Dichiarazione e inizializzazione diretta dell'array di colore
        {"Semaforo Verde", 0},
        {"Semaforo Giallo", 1},
        {"Semaforo Rosso", 2},
        {"Semaforo spento", 3}
    };



unsigned long startMillis;         // Store start time
unsigned long millisCorrente;      // Store current time
const unsigned long periodo_verde = 500;  // Green color period (5 second)
const unsigned long periodo_giallo = 1000; // Yellow period (1 second)
const unsigned long periodo_rosso = 5000; // Red period (5 second)
int stato_led = (Colori[0].led_colore);  // State to control which color to display

void setup() {
  Serial.begin(9600);  // Start serial communication
  pixels_1.begin();
  pixels_2.begin();      // Initialize NeoPixel strip
  startMillis = millis();  // Store the start time
  
}

void loop() {
  millisCorrente = millis();  // Get current time

  // Handle the logic for the different LED states
  if (stato_led == (Colori[0].led_colore)) {
   

   for (int i=0; i<NUM_PIXELS_1;i++){
    pixels_1.setPixelColor(i, colors[2]);  // Set first pixel to green
    pixels_1.show();  // Update the LEDs
    delay(100);
    pixels_1.fill(colors[0]);
    delay(200);
    pixels_1.clear();  // Clear LED strip
    pixels_1.show();   // Update the LEDs
    }
    //pixels_1.clear();  // Clear LED strip
    //pixels_1.show();   // Update the LEDs
    delay(100);
    for (int i=0; i<NUM_PIXELS_2;i++){
    pixels_2.setPixelColor(i, colors[6]);  // Set first pixel to green
    pixels_2.show();  // Update the LEDs
    delay(100);
    pixels_1.fill(colors[5]);
    delay(200);
    }

    pixels_2.clear();  // Clear LED strip
    pixels_2.show();   // Update the LEDs
    delay(100);
      
    // Switch state after the green period
    if (millisCorrente - startMillis >= periodo_verde) {
    Serial.println(Colori[1].messaggio); 
      pixels_1.clear();  // Clear LED strip
      pixels_1.show();   // Update the LEDs
      stato_led = (Colori[1].led_colore);   // Change state to yellow
      startMillis = millis();  // Reset startMillis for the next timing cycle
    }
  }
  else if (stato_led == (Colori[1].led_colore)) {  // Green + Yellow (transition)
       pixels_1.fill(colors[1]);  // Set second pixel to yellow
    pixels_1.show();  // Update the LEDs
  
    // Switch back to green after the yellow period
    if (millisCorrente - startMillis >= periodo_giallo) {
      Serial.println((Colori[2].messaggio));
      pixels_1.clear();  // Clear LED strip
      pixels_1.show();   // Update the LEDs
      stato_led = 2;   // Change state to green
      startMillis = millis();  // Reset startMillis for the next cycle
    }
  }
  else if (stato_led == (Colori[2].led_colore)) {  // Green + Yellow (transition)
    pixels_1.setPixelColor(2, colors[0]);  // Set third pixel to red
    pixels_1.show();  // Update the LEDs
   
    // Switch back to green after the yellow period
    if (millisCorrente - startMillis >= periodo_rosso) {
      Serial.println((Colori[0].messaggio));
      pixels_1.clear();  // Clear LED strip
      pixels_1.show();   // Update the LEDs
      stato_led = (Colori[0].led_colore);   // Change state to green
      startMillis = millis();  // Reset startMillis for the next cycle
    }
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
rgb1:VDD
rgb1:DOUT
rgb1:VSS
rgb1:DIN
rgb2:VDD
rgb2:DOUT
rgb2:VSS
rgb2:DIN
rgb3:VDD
rgb3:DOUT
rgb3:VSS
rgb3:DIN
rgb4:VDD
rgb4:DOUT
rgb4:VSS
rgb4:DIN
rgb5:VDD
rgb5:DOUT
rgb5:VSS
rgb5:DIN
rgb6:VDD
rgb6:DOUT
rgb6:VSS
rgb6:DIN
rgb7:VDD
rgb7:DOUT
rgb7:VSS
rgb7:DIN
rgb8:VDD
rgb8:DOUT
rgb8:VSS
rgb8:DIN
rgb9:VDD
rgb9:DOUT
rgb9:VSS
rgb9:DIN
rgb10:VDD
rgb10:DOUT
rgb10:VSS
rgb10:DIN
rgb11:VDD
rgb11:DOUT
rgb11:VSS
rgb11:DIN
rgb12:VDD
rgb12:DOUT
rgb12:VSS
rgb12:DIN