#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, 255, 0), // Yellow
};
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 = 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 = (Colori[0].led_colore); // State to control which color to display
void setup() {
Serial.begin(9600); // Start serial communication
pixels_1.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)) {
// Green
pixels_1.fill(colors[2]); // Set first pixel to green
pixels_1.show(); // Update the LEDs
pixels_2.fill(colors[1]); // Set first pixel to green
pixels_2.show(); // Update the LEDs
// 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
}
}
}