This circuit is designed to control a traffic light using an Arduino UNO and a joystick module. The traffic light has three LEDs (red, yellow, and green) that are controlled by the joystick's position. Moving the joystick up turns on the red LED, moving it left turns on the yellow LED, and moving it right turns on the green LED. Moving the joystick down turns on all LEDs. The joystick also has a switch that is read by the Arduino but not used in the current code.
/*
* This Arduino sketch controls a traffic light 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.
*/
// Pin definitions
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int vrxPin = A0;
const int vryPin = A1;
const int swPin = 7;
void setup() {
// Initialize LED pins as outputs
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
// Initialize joystick pins
pinMode(vrxPin, INPUT);
pinMode(vryPin, INPUT);
pinMode(swPin, INPUT);
}
void loop() {
int vrxValue = analogRead(vrxPin);
int vryValue = analogRead(vryPin);
// Joystick moved up
if (vryValue < 300) {
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
// Joystick moved down
else if (vryValue > 700) {
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, HIGH);
}
// Joystick moved left
else if (vrxValue < 300) {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
}
// Joystick moved right
else if (vrxValue > 700) {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
}
// Joystick in neutral position
else {
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
delay(100); // Small delay for debounce
}
This code is saved as sketch.ino
and is intended to be uploaded to the Arduino UNO microcontroller.