Assignment 6 – Traffic Light

Concept

For my project I decided to use toy cars to light up a traffic light. My idea is that when the red car is on the piece of aluminum foil the red light lights up, when the grey light is on the aluminum the yellow light lights up, and when the black car is on the aluminum foil the green light lights up. This is a way to use code to control a traffic light using an unusual switch.

I used the idea that we did in the class for the digital input but instead of the light turning off the light is turning on, and instead of one switch I have three different switches connected to three different lights.

Code I am Proud of

// Control the red LED based on red button state
    if (redButtonState == HIGH) {
        digitalWrite(5, HIGH);   // Turn on red LED
    } else {
        digitalWrite(5, LOW);    // Turn off red LED
    }

    // Control the blue LED based on blue button state
    if (blueButtonState == HIGH) {
        digitalWrite(3, HIGH);   // Turn on blue LED
    } else {
        digitalWrite(3, LOW);    // Turn off blue LED
    }

    // Control the yellow LED based on yellow button state
    if (yellowButtonState == HIGH) {
        digitalWrite(4, HIGH);   // Turn on yellow LED
    } else {
        digitalWrite(4, LOW);    // Turn off yellow LED
    }
}

This code is used for turning on the light when the two pieces of aluminum foil are touching each other.

Setup

I used TinkerCad to develop my Arduino and the placement of the wires and code it to make sure everything is correct.

Code

GitHub

Demonstration

Leave a Reply