#include <Arduino.h>
// === Pinos dos LEDs do Semáforo de Veículos ===
const int VEI_GREEN_PIN = D2;
const int VEI_YELLOW_PIN = D3;
const int VEI_RED_PIN = D4;
// === Pinos dos LEDs do Semáforo de Pedestres ===
const int PED_RED_PIN = D5;
const int PED_GREEN_PIN = D6;
// === Outros Componentes ===
const int BUTTON_PIN = D7;
const int BUZZER_PIN = D8;
// === Temporização (ms) ===
const unsigned long T_VEI_YELLOW = 2000;
const unsigned long T_PED_GREEN = 4000;
const unsigned long T_PED_BLINK = 3000;
const unsigned long T_VEI_GREEN = 5000;
const unsigned long BLINK_INTERVAL = 400;
// === Prototipação das funções ===
void setEstadoPadrao();
void executarCiclo();
// ============================================================
void setEstadoPadrao() {
digitalWrite(VEI_GREEN_PIN, HIGH);
digitalWrite(VEI_YELLOW_PIN, LOW);
digitalWrite(VEI_RED_PIN, LOW);
digitalWrite(PED_RED_PIN, HIGH);
digitalWrite(PED_GREEN_PIN, LOW);
noTone(BUZZER_PIN);
}
void executarCiclo() {
// === FASE 1: Veículos amarelo, pedestres vermelho ===
Serial.println("Fase 1: Veiculos AMARELO | Pedestres VERMELHO");
digitalWrite(VEI_GREEN_PIN, LOW);
digitalWrite(VEI_YELLOW_PIN, HIGH);
digitalWrite(VEI_RED_PIN, LOW);
digitalWrite(PED_RED_PIN, HIGH);
digitalWrite(PED_GREEN_PIN, LOW);
delay(T_VEI_YELLOW);
// === FASE 2: Veículos vermelho, pedestres verde fixo ===
Serial.println("Fase 2: Veiculos VERMELHO | Pedestres VERDE");
digitalWrite(VEI_YELLOW_PIN, LOW);
digitalWrite(VEI_RED_PIN, HIGH);
digitalWrite(PED_RED_PIN, LOW);
digitalWrite(PED_GREEN_PIN, HIGH);
delay(T_PED_GREEN);
// === FASE 3: Verde piscando + buzzer ===
Serial.println("Fase 3: Pedestres VERDE PISCANDO + Buzzer");
unsigned long inicioFase3 = millis();
unsigned long lastBlink = millis();
bool ledState = HIGH;
while (millis() - inicioFase3 < T_PED_BLINK) {
if (millis() - lastBlink >= BLINK_INTERVAL) {
ledState = !ledState;
digitalWrite(PED_GREEN_PIN, ledState);
lastBlink = millis();
}
tone(BUZZER_PIN, 1000);
}
// === FASE 4: Pedestres vermelho, buzzer para ===
Serial.println("Fase 4: Pedestres VERMELHO | Buzzer OFF");
digitalWrite(PED_GREEN_PIN, LOW);
digitalWrite(PED_RED_PIN, HIGH);
noTone(BUZZER_PIN);
delay(500);
// === FASE 5: Veículos verde ===
Serial.println("Fase 5: Veiculos VERDE | Pedestres VERMELHO");
digitalWrite(VEI_RED_PIN, LOW);
digitalWrite(VEI_GREEN_PIN, HIGH);
delay(T_VEI_GREEN);
}
void setup() {
pinMode(VEI_GREEN_PIN, OUTPUT);
pinMode(VEI_YELLOW_PIN, OUTPUT);
pinMode(VEI_RED_PIN, OUTPUT);
pinMode(PED_RED_PIN, OUTPUT);
pinMode(PED_GREEN_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
delay(500);
setEstadoPadrao();
Serial.println("Estado padrao. Aguardando botao...");
}
void loop() {
static bool lastButtonState = HIGH;
bool currentButton = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && currentButton == LOW) {
delay(50);
currentButton = digitalRead(BUTTON_PIN);
if (currentButton == LOW) {
Serial.println("Botao pressionado! Iniciando ciclo...");
executarCiclo();
setEstadoPadrao();
Serial.println("Ciclo concluido. Aguardando botao...");
}
}
lastButtonState = currentButton;
}