Traffic intersection code broken ;/

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;
    }
  }
}

If stateStartMillis is set to currentMillis two lines before, then there's no much chance of this if statement not being true so your code never advances. You need to set the state start time only once at the start of the state. Or at the end of the last state is a great spot.

2 Likes

Yes, the millis timer usage is wrong..
Thinking it would be better to have wait states..

Here's an example to check out..
Press and Hold down a button to get a certain sequence..
UnoLedsBlink

good luck.. ~q

you've tried to code a timer using startStateMillis within a block already conditioned on a timer using previousMillis.

need unique states for each traffic light state which seem most likely

  • green arrow
  • green
  • yellow
  • red

each case just needs to update the LEDs that are changed, the interval and the state. since this is a sequencer, the state can simply be incremented except the last where it is set to zero.

for example

    if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;

        switch (currentState) {
        case 0: // Section 1
            digitalWrite(yellowPin2, LOW);
            digitalWrite(redPin2,    HIGH);

            digitalWrite(arrowPin1,  HIGH);
            currentState++;
            break;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.