This circuit is designed to control a traffic light system using an Arduino UNO microcontroller and a touch sensor. The system operates in a cycle where the touch sensor's activation changes the state of the traffic light from red to yellow to green and then off. The circuit uses the Arduino UNO's digital pins to interface with the touch sensor and to control the individual LEDs of the traffic light.
/*
* This Arduino sketch controls a traffic light system using a touch sensor.
* When the touch sensor 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 touchPin = 7; // Pin connected to the touch sensor
const int redPin = 2; // Pin connected to the red LED
const int yellowPin = 3; // Pin connected to the yellow LED
const int greenPin = 4; // Pin connected to the green LED
int touchState = 0; // Current state of the touch sensor
int lastTouchState = 0; // Previous state of the touch sensor
int ledState = 0; // State of the LEDs (0: off, 1: red, 2: yellow, 3: green)
void setup() {
pinMode(touchPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}
void loop() {
touchState = digitalRead(touchPin);
if (touchState == HIGH && lastTouchState == LOW) {
ledState = (ledState + 1) % 4;
updateLEDs();
delay(200); // Debounce delay
}
lastTouchState = touchState;
}
void updateLEDs() {
digitalWrite(redPin, ledState == 1 ? HIGH : LOW);
digitalWrite(yellowPin, ledState == 2 ? HIGH : LOW);
digitalWrite(greenPin, ledState == 3 ? HIGH : LOW);
}
This code is written for the Arduino UNO and is responsible for reading the state of the touch sensor and updating the traffic light LEDs accordingly. The setup()
function initializes the pins and sets the initial state of the LEDs to off. The loop()
function continuously checks the state of the touch sensor and updates the LEDs based on the current state. The updateLEDs()
function sets the appropriate LED to on based on the ledState
variable, which cycles through the values 0 to 3, representing the off, red, yellow, and green states, respectively.