For this week’s assignment, I drew inspiration from traffic lights. I aimed to replicate their functionality using a potentiometer for analog input and a button for digital input. The potentiometer serves to control the transition of the LED lights, mimicking the sequence of a traffic light as it turns from red to yellow and then to green. Additionally, pressing the button initiates a special state where all three lights blink simultaneously.
Video Demonstration
link to the video: https://drive.google.com/file/d/1gREpijAMYFY12Yk4Ann_fiJzZrkjL-z1/view?usp=sharing
Code
The code uses the concepts of analog reading, digital input, LED control, and mapping to create a dynamic and interactive traffic light simulation.
const int analogSensorPin = A0;
const int digitalSensorPin = 2;
const int yellowLEDPin = 9;
const int greenLEDPin = 11;
//variables to store sensor readings
int trafficLightState = 0; // 0: red, 1: yellow, 2: green
pinMode(analogSensorPin, INPUT);
pinMode(digitalSensorPin, INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
analogSensorValue = analogRead(analogSensorPin);
digitalSensorState = digitalRead(digitalSensorPin);
//toggle traffic light state on button press
if (digitalSensorState == HIGH) {
// Blink all lights together
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
delay(500); // Blink duration
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
delay(200); // Debouncing
//map potentiometer value to traffic light state
trafficLightState = map(analogSensorValue, 0, 1023, 0, 2);
//control traffic light LEDs based on state
if (trafficLightState == 0) {
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 1) {
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 2) {
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
const int analogSensorPin = A0;
const int digitalSensorPin = 2;
const int redLEDPin = 6;
const int yellowLEDPin = 9;
const int greenLEDPin = 11;
//variables to store sensor readings
int analogSensorValue;
int digitalSensorState;
int trafficLightState = 0; // 0: red, 1: yellow, 2: green
void setup() {
pinMode(analogSensorPin, INPUT);
pinMode(digitalSensorPin, INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
}
void loop() {
analogSensorValue = analogRead(analogSensorPin);
digitalSensorState = digitalRead(digitalSensorPin);
//toggle traffic light state on button press
if (digitalSensorState == HIGH) {
// Blink all lights together
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
delay(500); // Blink duration
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
delay(200); // Debouncing
} else {
//map potentiometer value to traffic light state
trafficLightState = map(analogSensorValue, 0, 1023, 0, 2);
//control traffic light LEDs based on state
if (trafficLightState == 0) {
//red light
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 1) {
//yellow light
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 2) {
// green light
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
}
}
delay(50);
}
const int analogSensorPin = A0;
const int digitalSensorPin = 2;
const int redLEDPin = 6;
const int yellowLEDPin = 9;
const int greenLEDPin = 11;
//variables to store sensor readings
int analogSensorValue;
int digitalSensorState;
int trafficLightState = 0; // 0: red, 1: yellow, 2: green
void setup() {
pinMode(analogSensorPin, INPUT);
pinMode(digitalSensorPin, INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
}
void loop() {
analogSensorValue = analogRead(analogSensorPin);
digitalSensorState = digitalRead(digitalSensorPin);
//toggle traffic light state on button press
if (digitalSensorState == HIGH) {
// Blink all lights together
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
delay(500); // Blink duration
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
delay(200); // Debouncing
} else {
//map potentiometer value to traffic light state
trafficLightState = map(analogSensorValue, 0, 1023, 0, 2);
//control traffic light LEDs based on state
if (trafficLightState == 0) {
//red light
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 1) {
//yellow light
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
} else if (trafficLightState == 2) {
// green light
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
}
}
delay(50);
}
I enjoyed working on this assignment since there were numerous ways to modify the LEDs’ behavior. The only challenging aspect was recording the video, as the potentiometer is a bit tough to turn, and it ends up rotating the entire breadboard with it :/