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 state of the traffic light changes upon each touch sensor activation. The cycle progresses from red to yellow to green and then turns off all lights before repeating. The Arduino UNO reads the state of the touch sensor and controls the traffic light LEDs accordingly.
/*
* 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 touch sensor
const int redPin = 2; // Pin connected to red LED
const int yellowPin = 3; // Pin connected to yellow LED
const int greenPin = 4; // Pin connected to 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: all off, 1: red, 2: yellow, 3: green)
void setup() {
pinMode(touchPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
// Initialize all LEDs to off
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
}
void loop() {
touchState = digitalRead(touchPin);
// Check if the touch sensor state has changed
if (touchState != lastTouchState) {
if (touchState == HIGH) {
// Cycle through LED states
ledState = (ledState + 1) % 4;
updateLEDs();
}
// Debounce delay
delay(50);
}
lastTouchState = touchState;
}
void updateLEDs() {
// Turn off all LEDs
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
// Turn on the appropriate LED based on the current state
switch (ledState) {
case 1:
digitalWrite(redPin, HIGH);
break;
case 2:
digitalWrite(yellowPin, HIGH);
break;
case 3:
digitalWrite(greenPin, HIGH);
break;
}
}
The code is written for the Arduino UNO microcontroller. It initializes the pins connected to the touch sensor and the traffic light LEDs. The loop
function continuously reads the state of the touch sensor and updates the state of the LEDs accordingly. The updateLEDs
function is responsible for turning on the appropriate LED based on the current state of the system.