Lesson 17 Simple Creation – Traffic Light

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

In this lesson, we will conduct an interesting experiment – simulate a traffic light. Traffic light is a road signal for directing vehicular traffic by means of colored lights, typically red for stop, green for go, and yellow for proceed with caution.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB cable

– Jumper wires

– 1 * Rotary encoder

– 3 * LED

– 3 * Resistor (220Ω)

Principle

For a real traffic light, the time length for displaying red and green is much longer than yellow. As a result, we define two cycles with code: a short cycle and a long cycle. In a short cycle, the traffic light changes its order at a rate of roughly once per second. On the other hand, the long cycle is changed by the rotary encoder which determines the time length of red and green.

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

Step 2: Program (please go to our official website www.sunfounder.com to download related code by clicking LEARN -> Get Tutorials)

Step 3: Compile the code

Step 4:  Upload the sketch to the SunFounder Uno board

Here you should see the red LED light up first, then the red LED and yellow LED, followed by the green LED, and finally the yellow LED again.

Code 

//Traffic Light//You will see the red LED light up first, then the red LED and yellow LED, followed by the green LED, and finally the yellow LED again.
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
const int redPin= 7; //red led attach to pin 7
const int yellowPin =8 ; //yellow led attach to pin 8
const int greenPin= 9; //green led attach to pin 9
const int clkPin= 2; //the clk of the 74HC595 attach to pin 2
const int dtPin= 3; //the dt of the 74HC595 attach to pin 3
const int swPin= 6; // the sw of the 74HC595 attach to pin 6 int encoderVal = 0; int state = 0;
int shortPeriod = 1000;
int longPeriod = 1000;
int targetCount = shortPeriod;
int count = 0; void setup()
{
pinMode(clkPin, INPUT); //set the clkPin as an input
pinMode(dtPin, INPUT); //set the dtPin as an input
pinMode(swPin, INPUT); //set the swPin as an input
digitalWrite(swPin, HIGH);
pinMode(redPin, OUTPUT); //set the redPin as an output
pinMode(yellowPin, OUTPUT); //set the yellowPin as an output
pinMode(greenPin, OUTPUT); //set the greenPin as an output
Serial.begin(9600); // start serial port at 9600 bps:
} void loop()
{
count++;
int change = getEncoderTurn();
longPeriod = longPeriod + change * 1000;
if (digitalRead(swPin) == LOW)
{
setLights(HIGH, HIGH, HIGH);
}
else
{
if (count == targetCount)
{
setState();
count = 0;
}
}
delay(1);
} void setState(void)
{
if (state == 0)
{
setLights(HIGH, LOW, LOW);
targetCount = longPeriod;
state = 1;
}
else if (state == 1)
{
setLights(HIGH, HIGH, LOW);
targetCount = shortPeriod;
state = 2;
}
else if (state == 2)
{
setLights(LOW, LOW, HIGH);
targetCount = longPeriod;
state = 3;
}
else if (state == 3)
{
setLights(LOW, HIGH, LOW);
targetCount = shortPeriod;
state = 0;
}
} int getEncoderTurn(void)
{
static int oldA = HIGH;
static int oldB = HIGH;
int result = 0;
int newA = digitalRead(clkPin);
int newB = digitalRead(dtPin);
if (newA != oldA || newB != oldB)
{
// something has changed
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 – 1);
}
}
oldA = newA;
oldB = newB;
return result;
} // the function to set the led with the specified state(on or off),HIGH is on, and LOW is off
void setLights(int redState, int yellowState, int greenState)
{
digitalWrite(redPin, redState);
digitalWrite(yellowPin, yellowState);
digitalWrite(greenPin, greenState);

Video