// ===== Pin =====
const int PIN_RED = 23;
const int PIN_YELLOW = 22;
const int PIN_GREEN = 21;
const int PIN_BUTTON = 18;
const int PIN_LDR = 34;
// ===== Durasi fase (ms) =====
const unsigned long RED_TIME = 5000;
const unsigned long YELLOW_TIME = 2000;
const unsigned long GREEN_TIME = 5000;
const unsigned long RED_EXT_TIME = 10000;
// ===== Debounce tombol =====
const unsigned long DEBOUNCE_MS = 50;
// ===== PWM =====
const int PWM_FREQ = 5000;
const int PWM_RES = 8;
const int DUTY_BRIGHT = 255;
const int DUTY_DARK = 50;
const int LED_PINS[3] = {PIN_RED, PIN_YELLOW, PIN_GREEN};
// ===== Sensor cahaya (ADC) =====
// Makin terang -> ADC makin kecil. Hysteresis agar tidak berkedip di batas.
const int ADC_DARK_ON = 2200;
const int ADC_BRIGHT_ON = 1800;
const unsigned long SENSOR_INTERVAL = 100;
const unsigned long PRINT_INTERVAL = 1000;
int adcValue = 0;
bool isDark = false;
int currentDuty = DUTY_BRIGHT;
unsigned long lastSensor = 0;
unsigned long lastPrint = 0;
// ===== State machine =====
enum LightState { STATE_RED, STATE_YELLOW, STATE_GREEN };
LightState state = STATE_RED;
unsigned long stateStart = 0;
unsigned long currentDuration = RED_TIME;
// ===== Status penyeberangan =====
bool crossingRequest = false;
bool redExtended = false;
// ===== Variabel ISR =====
volatile bool buttonPressed = false;
volatile unsigned long lastIsrTime = 0;
// ISR: hanya mencatat penekanan tombol (dengan debounce waktu)
void IRAM_ATTR onButtonPress() {
unsigned long now = millis();
if (now - lastIsrTime > DEBOUNCE_MS) {
buttonPressed = true;
lastIsrTime = now;
}
}
// ----- PWM (kompatibel core ESP32 versi 2.x dan 3.x) -----
void setupPwm() {
for (int i = 0; i < 3; i++) {
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcAttach(LED_PINS[i], PWM_FREQ, PWM_RES);
#else
ledcSetup(i, PWM_FREQ, PWM_RES);
ledcAttachPin(LED_PINS[i], i);
#endif
}
}
void pwmWrite(int idx, int duty) {
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
ledcWrite(LED_PINS[idx], duty);
#else
ledcWrite(idx, duty);
#endif
}
// Hanya LED fase aktif yang diberi duty, LED lain 0
void applyOutputs() {
pwmWrite(0, state == STATE_RED ? currentDuty : 0);
pwmWrite(1, state == STATE_YELLOW ? currentDuty : 0);
pwmWrite(2, state == STATE_GREEN ? currentDuty : 0);
}
const char* stateName() {
if (state == STATE_RED) return "MERAH";
if (state == STATE_YELLOW) return "KUNING";
return "HIJAU";
}
unsigned long getDuration(LightState s) {
if (s == STATE_RED) return RED_TIME;
if (s == STATE_YELLOW) return YELLOW_TIME;
return GREEN_TIME;
}
// Pindah ke fase baru
void enterState(LightState s) {
state = s;
stateStart = millis();
currentDuration = getDuration(s);
redExtended = false;
// Saat masuk merah: layani permintaan yang tersimpan
if (s == STATE_RED && crossingRequest) {
currentDuration = RED_EXT_TIME;
redExtended = true;
crossingRequest = false;
Serial.println("Permintaan dilayani: merah 10 detik");
}
applyOutputs();
Serial.print("Fase: ");
Serial.println(stateName());
}
// Olah penekanan tombol (dipanggil dari loop, bukan dari ISR)
void handleButton() {
if (state == STATE_RED) {
if (!redExtended) {
currentDuration = RED_EXT_TIME;
redExtended = true;
Serial.println("Tombol: merah diperpanjang jadi 10 detik");
} else {
Serial.println("Tombol: diabaikan (merah sudah diperpanjang)");
}
} else {
if (!crossingRequest) {
crossingRequest = true;
Serial.println("Tombol: permintaan disimpan untuk merah berikutnya");
} else {
Serial.println("Tombol: permintaan sudah tersimpan");
}
}
}
// Baca LDR, klasifikasi terang/gelap, atur duty PWM
void updateSensor() {
adcValue = analogRead(PIN_LDR);
if (!isDark && adcValue > ADC_DARK_ON) isDark = true;
else if (isDark && adcValue < ADC_BRIGHT_ON) isDark = false;
int newDuty = isDark ? DUTY_DARK : DUTY_BRIGHT;
if (newDuty != currentDuty) {
currentDuty = newDuty;
applyOutputs();
Serial.printf("PWM berubah: duty=%d (%d%%) -> %s\n",
currentDuty, currentDuty * 100 / 255, isDark ? "GELAP" : "TERANG");
}
}
// Cetak status berkala
void printStatus() {
unsigned long elapsed = millis() - stateStart;
long remain = (long)currentDuration - (long)elapsed;
if (remain < 0) remain = 0;
const char* req = crossingRequest ? "TERSIMPAN"
: (redExtended ? "DILAYANI (merah 10 dtk)" : "tidak ada");
Serial.printf("[%s] sisa=%.1fs | ADC=%d (%s) | PWM=%d (%d%%) | Permintaan: %s\n",
stateName(), remain / 1000.0, adcValue,
isDark ? "GELAP" : "TERANG",
currentDuty, currentDuty * 100 / 255, req);
}
void setup() {
Serial.begin(115200);
pinMode(PIN_BUTTON, INPUT_PULLUP);
setupPwm();
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), onButtonPress, FALLING);
updateSensor();
enterState(STATE_RED);
}
void loop() {
unsigned long now = millis();
// 1. Tombol (kejadian dicatat ISR, diolah di sini)
if (buttonPressed) {
noInterrupts();
buttonPressed = false;
interrupts();
handleButton();
}
// 2. Sensor cahaya berkala -> PWM
if (now - lastSensor >= SENSOR_INTERVAL) {
lastSensor = now;
updateSensor();
}
// 3. Pergantian fase (non-blocking)
if (now - stateStart >= currentDuration) {
if (state == STATE_RED) enterState(STATE_YELLOW);
else if (state == STATE_YELLOW) enterState(STATE_GREEN);
else enterState(STATE_RED);
}
// 4. Monitoring Serial berkala
if (now - lastPrint >= PRINT_INTERVAL) {
lastPrint = now;
printStatus();
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4