Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Traffic Light System with Joystick Interaction

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Traffic Light System with Joystick Interaction

Traffic Light Control System Documentation

Summary

The circuit described in this documentation is designed to control a traffic light system using an Arduino UNO microcontroller and a joystick module. The traffic light system consists of three LEDs (red, yellow, and green) that are controlled based on the movement of the joystick. The joystick's vertical and horizontal movements are used to switch between the traffic light states, while a button press on the joystick can activate all lights simultaneously.

Component List

Arduino UNO

Traffic Light

Joystick Module

Wiring Details

Arduino UNO

Traffic Light

Joystick Module

Documented Code

/*
 * This Arduino sketch controls a traffic light system using a joystick module.
 * When the joystick is moved up, the red LED turns on.
 * When the joystick is moved left, the yellow LED turns on.
 * When the joystick is moved right, the green LED turns on.
 * When the joystick is moved down, all LEDs turn on.
 */

const int redLEDPin = 2;    // Red LED connected to D2
const int yellowLEDPin = 3; // Yellow LED connected to D3
const int greenLEDPin = 4;  // Green LED connected to D4
const int vrxPin = A0;      // Joystick VRX connected to A0
const int vryPin = A1;      // Joystick VRY connected to A1
const int swPin = 7;        // Joystick SW connected to D7

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(swPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int vrxValue = analogRead(vrxPin);
  int vryValue = analogRead(vryPin);
  int swValue = digitalRead(swPin);

  // Center values for the joystick
  const int centerX = 512;
  const int centerY = 512;
  const int threshold = 100;

  // Reset all LEDs
  digitalWrite(redLEDPin, LOW);
  digitalWrite(yellowLEDPin, LOW);
  digitalWrite(greenLEDPin, LOW);

  if (vryValue < centerY - threshold) {
    // Joystick moved up
    digitalWrite(redLEDPin, HIGH);
  } else if (vrxValue < centerX - threshold) {
    // Joystick moved left
    digitalWrite(yellowLEDPin, HIGH);
  } else if (vrxValue > centerX + threshold) {
    // Joystick moved right
    digitalWrite(greenLEDPin, HIGH);
  } else if (vryValue > centerY + threshold) {
    // Joystick moved down
    digitalWrite(redLEDPin, HIGH);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(greenLEDPin, HIGH);
  }

  delay(100); // Small delay to avoid flickering
}

This code is responsible for reading the joystick's position and button state and controlling the traffic light LEDs accordingly. The setup() function initializes the pins connected to the LEDs and the joystick switch. The loop() function continuously reads the joystick's analog values to determine its position and sets the state of the LEDs based on the joystick's movement.