const int redPin = 2; // Pin untuk lampu merah
const int yellowPin = 3; // Pin untuk lampu kuning
unsigned long previousMillisRed = 0;
unsigned long previousMillisYellow = 0;
const long intervalRed = 1000; // Interval berkedip untuk lampu merah (1 detik)
const long durationRed = 5000; // Durasi total berkedip (5 detik)
const long onDurationYellow = 5000; // Durasi hidup lampu kuning (5 detik)
const long offDurationYellow = 10000; // Durasi mati lampu kuning (10 detik)
bool redState = LOW;
bool yellowState = LOW;
unsigned long startMillisRed = 0;
unsigned long startMillisYellow = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Mengatur lampu merah
if (currentMillis - startMillisRed >= intervalRed) {
if (currentMillis - startMillisRed < durationRed) {
redState = !redState; // Toggle state lampu merah
digitalWrite(redPin, redState);
startMillisRed = currentMillis; // Reset timer untuk lampu merah
} else {
digitalWrite(redPin, LOW); // Matikan lampu merah setelah 5 detik
}
}
// Mengatur lampu kuning
if (currentMillis - previousMillisYellow >= (yellowState ? onDurationYellow : offDurationYellow)) {
yellowState = !yellowState; // Toggle state lampu kuning
digitalWrite(yellowPin, yellowState ? HIGH : LOW);
previousMillisYellow = currentMillis; // Reset timer untuk lampu kuning
}
}