What's new

Make a two-way traffic light code for the Arduino Mega 2560 that will operate both with and without a switch at the same time. Additionally, if there

shindey

Eternal Poster
Joined
Apr 16, 2022
Posts
381
Reaction
60
Points
288
Make a two-way traffic light code for the Arduino Mega 2560 that will operate both with and without a switch at the same time. Additionally, if there is a switch, one will start off green and the other red.
 
Sure! Here's an example code for a two-way traffic light with an optional switch on the Arduino Mega 2560:

C++:
// Pin definitions
const int red1Pin = 2;
const int yellow1Pin = 3;
const int green1Pin = 4;
const int red2Pin = 5;
const int yellow2Pin = 6;
const int green2Pin = 7;
const int switchPin = 8;

// Traffic light timing settings
const unsigned long greenTime = 10000; // 10 seconds
const unsigned long yellowTime = 3000; // 3 seconds

// Variables
bool useSwitch = false;
bool switchState = false;
unsigned long currentTime = 0;
unsigned long previousTime = 0;
int currentState = 0; // 0: Green1, 1: Yellow1, 2: Red1, 3: Green2, 4: Yellow2, 5: Red2

void setup() {
  // Initialize pin modes
  pinMode(red1Pin, OUTPUT);
  pinMode(yellow1Pin, OUTPUT);
  pinMode(green1Pin, OUTPUT);
  pinMode(red2Pin, OUTPUT);
  pinMode(yellow2Pin, OUTPUT);
  pinMode(green2Pin, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);

  // Check if the switch is connected
  if (digitalRead(switchPin) == LOW) {
    useSwitch = true;
    currentState = 3; // Start with Green2
  }
}

void loop() {
  // Update switch state
  if (useSwitch) {
    switchState = digitalRead(switchPin);
  }

  // Traffic light state machine
  switch (currentState) {
    case 0: // Green1
      digitalWrite(green1Pin, HIGH);
      digitalWrite(red2Pin, HIGH);
      currentState = 1;
      previousTime = millis();
      break;

    case 1: // Yellow1
      currentTime = millis();
      if (currentTime - previousTime >= yellowTime) {
        digitalWrite(yellow1Pin, LOW);
        digitalWrite(red1Pin, HIGH);
        currentState = 2;
        previousTime = currentTime;
      } else {
        digitalWrite(yellow1Pin, HIGH);
      }
      break;

    case 2: // Red1
      currentTime = millis();
      if (currentTime - previousTime >= greenTime) {
        digitalWrite(green2Pin, HIGH);
        digitalWrite(red1Pin, LOW);
        currentState = 3;
        previousTime = currentTime;
      }
      break;

    case 3: // Green2
      digitalWrite(green2Pin, HIGH);
      digitalWrite(red1Pin, HIGH);
      currentState = 4;
      previousTime = millis();
      break;

    case 4: // Yellow2
      currentTime = millis();
      if (currentTime - previousTime >= yellowTime) {
        digitalWrite(yellow2Pin, LOW);
        digitalWrite(red2Pin, HIGH);
        currentState = 5;
        previousTime = currentTime;
      } else {
        digitalWrite(yellow2Pin, HIGH);
      }
      break;

    case 5: // Red2
      currentTime = millis();
      if (currentTime - previousTime >= greenTime) {
        digitalWrite(green1Pin, HIGH);
        digitalWrite(red2Pin, LOW);
        currentState = 0;
        previousTime = currentTime;
      }
      break;
  }

  // Check switch state to switch traffic lights
  if (useSwitch && switchState) {
    digitalWrite(green1Pin, HIGH);
    digitalWrite(green2Pin, LOW);
    digitalWrite(red1Pin, LOW);
    digitalWrite(red2Pin, HIGH);
    currentState = 0;
  }
}

In this code, the traffic light is controlled using a state machine. The setup() function initializes the pin modes and checks if the switch is connected by reading its state. If the switch is pressed (LOW state), the traffic light starts with Green2, otherwise, it starts with Green1.

The loop() function updates the switch state and controls the traffic light based on the current state. The switch statement is used to determine the behavior of each state. The traffic light timings are controlled using millis() to track the elapsed time.

If the switch is detected during operation, the traffic lights are immediately switched to the opposite state, overriding the current state and starting with Green1 and Red2.

Make sure to connect the LEDs and the switch to the correct pins on the Arduino Mega 2560 as defined in the code.
 
Back
Top