ARDUINO 101
By Nicholas Borko

Alternative Project: Traffic Signal Controller

Objective: Design a traffic signal controller for an intersection, including a cross-walk control.

Goals: Create 2 cross traffic signals using red, yellow and green LEDs. Use the red-green LED to create a cross-walk signal for one of the directions. Use one of the push buttons as a pedestrian control to shorten the light to activate the WALK signal.

Hints: Activate the hint links below to reveal some hints to help you accomplish the project.

Hint #1: How should it work?

Each traffic signal should be RED (or GREEN) for 30 seconds. Between green and red transition, the signal should be YELLOW for 2 seconds. There should be a 1 second delay when BOTH signals are RED.

When the traffic signal with the cross-walk signal turns green, the cross-walk signal should be FLASHING GREEN for 10 seconds, SOLID GREEN for 10 seconds, FLASHING RED for 10 seconds, and SOLID RED for opposing yellow and red signals.

If the opposing traffic signal has more than 10 seconds left being green when the cross-walk button is pressed, then it should immediately be reduced to 10 seconds. The button will have no effect in any other situation.

The times set forth are for "real world" timing. You cut times in half to make development and debugging easier.


Hint #2: Pseudocode

The following is suggested pseudocode to control the traffic signal. There is more than one way to do it, so your code doesn't necessarily have to match as long as it works correctly. Note that this code does not include any "setup" code you might need to control your input and output pins on the Arduino.

TURN SIGNAL1 RED
TURN SIGNAL2 RED
TURN CROSSWALK RED
BEGIN LOOP
    WAIT 1 SECOND
    TURN SIGNAL1 GREEN
    WHILE WAITING 20 SECONDS
        IF BUTTON IS PRESSED THEN
            STOP WAITING
        END IF
    END WHILE
    WAIT 10 SECONDS
    TURN SIGNAL1 YELLOW
    WAIT 2 SECONDS
    TURN SIGNAL1 RED
    WAIT 1 SECOND
    TURN SIGNAL2 GREEN
    WHILE WAITING 10 SECONDS
        FLASH CROSSWALK GREEN
    END WHILE
    WHILE WAITING 10 SECONDS
        TURN CROSSWALK GREEN
    END WHILE
    WHILE WAITING 10 SECONDS
        FLASH CROSSWALK RED
    END WHILE
    TURN CROSSWALK RED
    TURN SIGNAL2 YELLOW
    WAIT 2 SECONDS
    TURN SIGNAL2 RED
END LOOP

Hint #3: Reading the Button (code)

If you recall, the Arduino delay() function effectively halts exection of your code, so you can't read the button during a delay(). The answer is to loop through a series of small delays that add up to the time you want to wait. Although it won't be exact, it will be close enough to human users. Here is an example of some code to do this:

    for (int i = 0; i < 2000; i++) {
        if (digitalRead(buttonPin) == LOW) {
            break; // The break statement breaks out of the for loop
        }
        delay(10); // Delay 10ms. When run 2000 times, this equals 20 seconds
    }

If more precise timing is needed, you could instead use the millis() function to figure out how much time has passed, but that's left as an exercise to the reader.


Hint #4: Flashing the Cross-Walk Light (code)

Your bicolor (red-green) LED has 3 leads. This LED is in a common cathode configuration, meaning that the 2 LEDs inside this single package share a common cathode, while the anodes are separate for each color. The common cathode is the longest lead, which is the center lead. The red anode is the next longest lead, and the green anode is the shortest lead. If you treat it as though it's 2 separate LEDs, then wiring should be a snap.

Flashing a LED is one of the first projects we looked at, but if you still need help, here is some sample code to get you started:

    for (int i = 0; i < 10; i++) {
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
    


Creative Commons Logo for BY-NC-SA
Except where otherwise noted, this work is licensed under
http://creativecommons.org/licenses/by-nc-sa/3.0