The circuit described herein is designed to control a traffic light system using an Arduino UNO microcontroller and a joystick module. The system allows a user to manipulate the traffic light by moving the joystick in different directions. Each direction corresponds to a different state of the traffic light:
The circuit is powered by the 5V output from the Arduino UNO, which also provides ground connections to the other components. Analog inputs from the joystick module are read to determine the position of the joystick, and digital outputs from the Arduino control the individual lights of the traffic light module.
/*
* 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;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int joystickXPin = A0;
const int joystickYPin = A1;
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(joystickXPin, INPUT);
pinMode(joystickYPin, INPUT);
}
void loop() {
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
if (yValue < 300) { // Joystick moved up
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
} else if (xValue < 300) { // Joystick moved left
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
} else if (xValue > 700) { // Joystick moved right
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
} else if (yValue > 700) { // Joystick moved down
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
} else { // Joystick in neutral position
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
delay(100); // Small delay to avoid flickering
}
This code is saved as sketch.ino
and should be uploaded to the Arduino UNO microcontroller to operate the traffic light system as intended.