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

Arduino UNO Controlled Traffic Light with Joystick Interaction

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

Circuit Documentation

Summary of the Circuit

This circuit is designed to simulate a traffic light system controlled by an Arduino UNO microcontroller and a joystick module. The joystick's button is used to cycle through the traffic light states: red, yellow, green, and off. The system operates in a sequence where each button press advances the traffic light to the next state in the cycle.

Component List

Arduino UNO

Joystick Module

Traffic Light

Wiring Details

Arduino UNO

Joystick Module

Traffic Light

Documented Code

/*
 * This Arduino sketch controls a traffic light system using a joystick module
 * as a touch sensor. When the joystick button is pressed, the red LED turns on.
 * On the next press, the yellow LED turns on. On the next press, the green LED
 * turns on. On the next press, all LEDs turn off. This cycle repeats with each
 * subsequent press.
 */

const int buttonPin = 7; // Joystick button connected to digital pin 7
const int redLedPin = 2; // Red LED connected to digital pin 2
const int yellowLedPin = 3; // Yellow LED connected to digital pin 3
const int greenLedPin = 4; // Green LED connected to digital pin 4

int buttonState = 0; // Variable for reading the button status
int lastButtonState = 0; // Variable for storing the last button state
int ledState = 0; // Variable for tracking the LED state

void setup() {
  pinMode(buttonPin, INPUT); // Initialize the button pin as an input
  pinMode(redLedPin, OUTPUT); // Initialize the red LED pin as an output
  pinMode(yellowLedPin, OUTPUT); // Initialize the yellow LED pin as an output
  pinMode(greenLedPin, OUTPUT); // Initialize the green LED pin as an output
}

void loop() {
  buttonState = digitalRead(buttonPin); // Read the state of the button

  // Check if the button is pressed
  if (buttonState == HIGH && lastButtonState == LOW) {
    ledState = (ledState + 1) % 4; // Cycle through LED states
    updateLeds(); // Update the LEDs based on the current state
  }

  lastButtonState = buttonState; // Save the current button state
}

void updateLeds() {
  // Turn off all LEDs
  digitalWrite(redLedPin, LOW);
  digitalWrite(yellowLedPin, LOW);
  digitalWrite(greenLedPin, LOW);

  // Turn on the appropriate LED based on the current state
  if (ledState == 1) {
    digitalWrite(redLedPin, HIGH);
  } else if (ledState == 2) {
    digitalWrite(yellowLedPin, HIGH);
  } else if (ledState == 3) {
    digitalWrite(greenLedPin, HIGH);
  }
}

This code is responsible for the logic behind the traffic light system. It initializes the pins connected to the joystick button and the LEDs of the traffic light. In the main loop, it reads the state of the joystick button and cycles through the LED states accordingly. The updateLeds() function is used to turn on the appropriate LED based on the current state.