#include <Adafruit_NeoPixel.h>

#define DIN_PIN 6
#define NUM_PIXELS 3

Adafruit_NeoPixel pixels(NUM_PIXELS, DIN_PIN);

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

unsigned long startMillis;         // Store start time
unsigned long millisCorrente;      // Store current time
const unsigned long periodo_verde = 5000;  // 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 = 0;  // State to control which color to display

void setup() {
  Serial.begin(9600);  // Start serial communication
  pixels.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 == 0) {  // Green
    pixels.setPixelColor(0, colors[2]);  // Set first pixel to green
    pixels.show();  // Update the LEDs
    
    // Switch state after the green period
    if (millisCorrente - startMillis >= periodo_verde) {
      Serial.println("Switching to Yellow");
      pixels.clear();  // Clear LED strip
      pixels.show();   // Update the LEDs
      stato_led = 1;   // Change state to yellow
      startMillis = millis();  // Reset startMillis for the next timing cycle
    }
  }
  else if (stato_led == 1) {  // Green + Yellow (transition)
       pixels.setPixelColor(1, colors[1]);  // Set second pixel to yellow
    pixels.show();  // Update the LEDs
    
    // Switch back to green after the yellow period
    if (millisCorrente - startMillis >= periodo_giallo) {
      Serial.println("Switching to Red");
      pixels.clear();  // Clear LED strip
      pixels.show();   // Update the LEDs
      stato_led = 2;   // Change state to green
      startMillis = millis();  // Reset startMillis for the next cycle
    }
  }
  else if (stato_led == 2) {  // Green + Yellow (transition)
       pixels.setPixelColor(2, colors[0]);  // Set third pixel to red
    pixels.show();  // Update the LEDs
    
    // Switch back to green after the yellow period
    if (millisCorrente - startMillis >= periodo_rosso) {
      Serial.println("Switching back to Green");
      pixels.clear();  // Clear LED strip
      pixels.show();   // Update the LEDs
      stato_led = 0;   // Change state to green
      startMillis = millis();  // Reset startMillis for the next cycle
    }
  }
}