tcas — Arduino Mega simulation

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by paarth-gupta0903.

Components

Sketch code

// ---------------- Traffic Light Pins ----------------
const int red1 = 2, yellow1 = 3, green1 = 4;
const int red2 = 5, yellow2 = 6, green2 = 7;
const int red3 = 8, yellow3 = 9, green3 = 10;
const int red4 = 11, yellow4 = 12, green4 = 13;

// ---------------- IR Sensor Pins (DIGITAL) ----------------
const int ir1 = A2, ir2 = A3, ir3 = A4, ir4 = A5;

// IR logic: MOST IR sensors = LOW when object detected
// If yours is opposite, tell me and I’ll invert this.
#define DETECT LOW     

// Normal cycle timing
const unsigned long normalCycleTime = 30000;
const unsigned long normalGreenTime = (normalCycleTime / 4) - 4000;
const unsigned long yellowTime = 4000;

// Lane structure
struct Lane {
  int redPin;
  int yellowPin;
  int greenPin;
  int irPin;
  bool hasTraffic;
};

Lane lanes[4] = {
  {red1, yellow1, green1, ir1, false},
  {red2, yellow2, green2, ir2, false},
  {red3, yellow3, green3, ir3, false},
  {red4, yellow4, green4, ir4, false}
};

enum TrafficState { PRIORITY, NORMAL_CYCLE };
TrafficState currentState = NORMAL_CYCLE;

int currentLane = -1;
unsigned long normalCycleStart = 0;

// --------------------------------------------------------
void setup() {
  Serial.begin(9600);

  for (int i = 0; i < 4; i++) {
    pinMode(lanes[i].redPin, OUTPUT);
    pinMode(lanes[i].yellowPin, OUTPUT);
    pinMode(lanes[i].greenPin, OUTPUT);
    pinMode(lanes[i].irPin, INPUT);
  }

  setAllRed();
}

// --------------------------------------------------------
void loop() {
  updateTraffic();
  manageSignals();
}

// --------------------------------------------------------
void updateTraffic() {
  for (int i = 0; i < 4; i++) {
    int val = digitalRead(lanes[i].irPin);
    lanes[i].hasTraffic = (val == DETECT);

    Serial.print("Lane ");
    Serial.print(i + 1);
    Serial.print(" IR: ");
    Serial.print(val);
    Serial.print("  Traffic: ");
    Serial.println(lanes[i].hasTraffic);
  }
}

// --------------------------------------------------------
void manageSignals() {

  int priorityLane = findPriorityLane();

  if (priorityLane != -1) {
    currentState = PRIORITY;
    servePriorityLane(priorityLane);
    return;
  }

  // No traffic → normal cycle
  if (currentState != NORMAL_CYCLE) {
    currentState = NORMAL_CYCLE;
    normalCycleStart = millis();
    setAllRed();
  }

  runNormalCycle();
}

// --------------------------------------------------------
int findPriorityLane() {
  for (int i = 0; i < 4; i++) {
    if (lanes[i].hasTraffic)
      return i;
  }
  return -1;
}

// --------------------------------------------------------
void servePriorityLane(int lane) {

  // Already serving this lane
  if (currentLane == lane) {
    if (!lanes[lane].hasTraffic) {
      currentLane = -1;
      setAllRed();
    }
    return;
  }

  // Switching to new priority lane
  setAllRed();
  delay(500);  // safety red

  currentLane = lane;
  setLaneLights(lane, false, false, true);  // GREEN ON
}

// --------------------------------------------------------
void run

More projects by paarth-gupta0903