i don't know what's wrong with my code and why its not continuing the next sequence
// Define LED pin numbers
const int redPin1 = 8; // Red LED for direction 1
const int yellowPin1 = 9; // Yellow LED for direction 1
const int greenPin1 = 2; // Green LED for direction 1
const int arrowPin1 = 3; // Green arrow LED for direction 1
const int redPin2 = 4; // Red LED for direction 2
const int yellowPin2 = 5; // Yellow LED for direction 2
const int greenPin2 = 6; // Green LED for direction 2
const int arrowPin2 = 7; // Green arrow LED for direction 2
unsigned long previousMillis = 0;
const long interval = 1000; // Interval between state changes in milliseconds
int currentState = 0; // 0 for Section 1, 1 for Section 2
unsigned long stateStartMillis; // Variable to store the start time of each state
unsigned long stateDuration; // Variable to store the duration of each state
void setup() {
// Initialize LED pins as OUTPUT
pinMode(redPin1, OUTPUT);
pinMode(yellowPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(arrowPin1, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(yellowPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(arrowPin2, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
switch (currentState) {
case 0: // Section 1
digitalWrite(arrowPin1, HIGH);
stateStartMillis = currentMillis; // Record the start time of the state
stateDuration = 2000; // Green arrow for 2 seconds
if (currentMillis - stateStartMillis <= stateDuration) {
// Green arrow, red in section 2
digitalWrite(greenPin1, LOW);
digitalWrite(redPin2, HIGH);
} else {
digitalWrite(arrowPin1, LOW);
digitalWrite(redPin2, LOW);
stateStartMillis = currentMillis; // Move to the next state
currentState = 1; // Move to Section 2
}
break;
case 1: // Section 2
digitalWrite(arrowPin2, HIGH);
stateStartMillis = currentMillis; // Record the start time of the state
stateDuration = 2000; // Green arrow for 2 seconds
if (currentMillis - stateStartMillis <= stateDuration) {
// Red in section 1, green arrow
digitalWrite(redPin1, HIGH);
digitalWrite(greenPin2, LOW);
} else {
digitalWrite(arrowPin2, LOW);
digitalWrite(redPin1, LOW);
stateStartMillis = currentMillis; // Move to the next state
currentState = 0; // Move to Section 1
}
break;
}
}
}