Traffic Light Without Delay

Purpose:

Blink is a great place to start programming but when you start to expand to make you program more interactive you soon run into problems. Those problems are frequently because of the delay command. The delay command causes the processor to sit and count the specified time, meaning that nothing can happen during that time this is a huge waste of time. It would be like if you where making a boiled egg and to time it you would sit watching pot and counting steamboats for three minutes. This would not be a good use of your time, most people would look at the clock, mark the current time, then check the clock and comparing the start time and current time until the 3 minutes had passed. The example program Blink Without Delay uses the mark and check method to control the on and off cycles of the LED.

Learning Goals

By completing this activity student will learn about working with millis() and routines for controlling timing (Fundamentals )

Practice using if(){} statements with boolean operators. (Programming)

Problem solving and program structure (Programming)

Wire the following circuit for the traffic light program Take a picture of the circuit and add it to your Arduino Assignment sheet.

Open the example Sketch blink without delay and modify it to use the circuit above in a traffic light sequence.

/* Traffic without Delay

simulates a simple traffic light sequence using three LEDs and a mark and check type of timing to control the sequence of the lights.

The circuit:

* Red LED attached from pin 13 to ground.

* Yellow LED attached from pin 12 to ground.

* Green LED attached from pin 11 to ground.

// set pin numbers:

const int redPin = 13; // the number of the red LED pin

const int yellowPin = 12; // the number of the yellow LED pin

const int greenPin = 11; // the number of the green LED pin

// Variables will change:

int redState = LOW; // redState used to set the red LED

int yellowState = LOW; // yellowState used to set the yellow LED

int greenState = LOW; // greenState used to set the green LED

long StartTime = 0; // will store last time the traffic sequence was started

// the follow variables is a long because the time, measured in miliseconds,

// will quickly become a bigger number than can be stored in an int.

long interval = 30000; // sequence interval is the time in miliseconds from the start of the green // light to the end of the red (milliseconds)

void setup() {

// set the digital pin as output:

pinMode(redPin, OUTPUT);

pinMode(yellowPin, OUTPUT);

pinMode(greenPin, OUTPUT);

}

void loop()

{

unsigned long CheckTime = millis();

unsigned long elapsedTime = CheckTime - StartTime;

if(elapsedTime > interval) {

StartTime = CheckTime;

}

if (elapsedTime < 10000){

redState = LOW;

yellowState = LOW;

greenState = HIGH;

}

if (elapsedTime > 10000 &&elapsedTime < 15000 ){

redState = LOW;

yellowState = HIGH;

greenState = LOW ;

}

if (elapsedTime > 15000 ){

redState = HIGH;

yellowState = LOW;

greenState = LOW ;

}

digitalWrite(redPin,redState);

digitalWrite(greenPin,greenState);

digitalWrite(yellowPin,yellowState);

}

Determine the total cycle time for your circuit this would be from the start of the red light to the next time the same light comes on. For the example I will say the total cycle time is 30000.

set this as the interval time.

Create a variable to calculate the elapsed time in the interval it should be currentMillis - previousMillis

long

Use four if else statements to reset the elapsed timer at the end of every cycle and set the led states at each point in the cycle.

Create an output section to your program that writes the led states to the led pins for all your leds.

Copy your sketch into your Arduino assignment.

Add A serial print function to your program to output the current led states to the serial monitor. include the serial monitor output in your Arduino assignment.

/* Traffic without Delay

simulates a simple traffic light sequence using three LEDs and a mark and check type of timing to control the sequence of the lights.

The circuit:

* Red LED attached from pin 13 to ground.

* Yellow LED attached from pin 12 to ground.

* Green LED attached from pin 11 to ground.

Written by Mr Michael Quosai

April 2015

This example code is in the public domain.

https://sites.google.com/site/mikequosaisarduinosite/blink-without-delay

*/

// constants won't change. Used here to

// set pin numbers:

const int redPin = 13; // the number of the red LED pin

const int yellowPin = 12; // the number of the yellow LED pin

const int greenPin = 11; // the number of the green LED pin

// Variables will change:

int redState = LOW; // redState used to set the red LED

int yellowState = LOW; // yellowState used to set the yellow LED

int greenState = LOW; // greenState used to set the green LED

long startTime = 0; // will store last time the traffic sequence was started

unsigned long currentTime = millis();

long elapsedTime = currentTime-startTime;

// the follow variables is a long because the time, measured in miliseconds,

// will quickly become a bigger number than can be stored in an int.

long cycleTime = 30000; // cycle time is the time in miliseconds from the start of the red to the start of the red light in the next cycle.

void setup() {

// set the digital pin as output:

pinMode(redPin, OUTPUT);

pinMode(yellowPin, OUTPUT);

pinMode(greenPin, OUTPUT);

}

void loop()

{

// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the

// difference between the current time and last time you blinked

// the LED is bigger than the interval at which you want to

// blink the LED.

currentTime = millis();

elapsedTime = currentTime-startTime;

if(elapsedTime > cycleTime) // this if statement resets the cycle timer

startTime = currentTime;

// if the elepsed time is less then 15 seconds set the red state to high:

if ( elapsedTime > 20000)

redState = HIGH;

else

redState = LOW;

// if the elepsed time greater than 15sec and less than 20sec set the yellow state to HIGH.

if ( elapsedTime > 15000 && elapsedTime< 20000)

yellowState = HIGH;

else

yellowState = LOW;

// if the elepsed time greater than 20 sec set the green state to HIGH.

if ( elapsedTime < 15000 )

greenState = HIGH;

else

greenState = LOW;

// set the LED with the ledState of the variable:

digitalWrite(redPin, redState);

digitalWrite(yellowPin, yellowState);

digitalWrite(greenPin, greenState);

}