0% found this document useful (0 votes)
67 views

Arduino DAQ

This document describes an Arduino program that simulates a traffic light system with four states: 1) north-south red, east-west green; 2) north-south red, east-west yellow; 3) north-south green, east-west red; and 4) north-south yellow, east-west red. It defines pins for the traffic lights and pedestrian button/light. The main loop switches between states using delays or a waitFor() function. waitFor() uses a while loop to flash an LED until the time period ends. The program also checks for a pedestrian button press and flashes the crossing light.

Uploaded by

Kirti Deo Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Arduino DAQ

This document describes an Arduino program that simulates a traffic light system with four states: 1) north-south red, east-west green; 2) north-south red, east-west yellow; 3) north-south green, east-west red; and 4) north-south yellow, east-west red. It defines pins for the traffic lights and pedestrian button/light. The main loop switches between states using delays or a waitFor() function. waitFor() uses a while loop to flash an LED until the time period ends. The program also checks for a pedestrian button press and flashes the crossing light.

Uploaded by

Kirti Deo Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ENGR 2167 : DAQ with MATLAB,

ARDUINO
Summer 2014
Week 1

Traffic Lights System

Kirti Deo Mishra


mishra.98
May 13, 2014

/*
ENGR 2167 : HW1, TRAFFIC LIGHT SYSTEM
KIRTI DEO MISHRA
About :
The system has basically four states:
1. N-S Red, E-W Green, All others off - longer period
2. N-S Red, E-W Yellow, All others off - shorter period
3. N-S Green, E-W Red, All others off - longer period
4. N-S Yellow, E-W Red, All others off - shorter period
First a basic control flow is written which uses delay() to simulate automated
traffic light system(this doesn't take into account real time element). Later on
the control will be modified to incorporate pedestrian walking.
After the basic testing of traffic light system with the delay() statement, a function
waitFor() was created which is a while loop which in turns call flashLt() to flash the LED.
*/
// Defining pins for traffic light(TL)
byte
byte
byte
byte
byte
byte

eastGreenPin = 14;
eastYellowPin = 15;
eastRedPin = 16;
northGreenPin = 19;
northYellowPin = 18;
northRedPin = 17;

// For pedestrian scanning


int pedFlashTime = 400;
byte pFlag = 0;
byte pedCrossingPin = 5; // Pedestrian Wlking Light
byte pedButtonPin = 4; // Walk-request button
unsigned long fTimer = 0;// For Flashing the LED
byte dummyFlash = 1; // For Flashing the LED
byte state = 1;
void setup()
{
Serial.begin(9600);
// Seting TL pin modes to output
2

pinMode(eastGreenPin, OUTPUT);
pinMode(eastYellowPin, OUTPUT);
pinMode(eastRedPin, OUTPUT);
pinMode(northGreenPin, OUTPUT);
pinMode(northYellowPin, OUTPUT);
pinMode(northRedPin, OUTPUT);
// For pedestrian scanning
pinMode(pedCrossingPin, OUTPUT);
pinMode(pedButtonPin, INPUT_PULLUP);
// Checking the light system(all lights on and off)
// Arguments of lightControl() - eG, eY, eR, nG, nY, nR
//lightControl(1, 1, 1, 1, 1, 1);
//delay(2000);
//lightControl(0, 0, 0, 0, 0, 0);
//delay(1000);
}

void loop()
{
//First Getting the basic TL system working(control Flow) with delay()
// Then Real Time Programming
switch(state)
{
case 1:
lightControl(1, 0, 0, 0, 0, 1);
//delay(5000);
// If in a cycle pFlag is raised(button pressed, LED goes solid here.
if(pFlag == 1)
{
digitalWrite(pedCrossingPin, HIGH);
// Also the status of pedestrian walk-request goes low
pFlag = 0;
}
waitFor(5000);
// When the system leaves this state then the pedestrian light which
// was solid(if blinking at all) should turn off.
digitalWrite(pedCrossingPin, LOW);

break;
case 2:
lightControl(0, 1, 0, 0, 0, 1);
//delay(2000);
waitFor(2000);
break;
case 3:
lightControl(0, 0, 1, 1, 0, 0);
//delay(5000);
waitFor(5000);
break;
case 4:
lightControl(0, 0, 1, 0, 1, 0);
//delay(2000);
waitFor(2000);
break;
}
state++;
if(state>4)
{
state = 1;
}

}
/*
Time Period is the time states in the problem statement like 5000 ms for Green
and Red lights.
*/
void waitFor(int timePeriod)
{
unsigned long startDelayTime = millis();
// Creating a count-down loop
while((millis()-startDelayTime) <= timePeriod)
{
// checking the button press and setting the flag if pressed
if(digitalRead(pedButtonPin) == 0)
{
pFlag = 1;
}
// Extra if statement to make sure that as soon as ped. presses
4

// button the light starts flashing


if(pFlag == 1)
{
flashLt(pedFlashTime);
}
}
}
void flashLt(int pedFlashTime)
{
if(fTimer == 0)
{
fTimer = millis();
}
if((millis()-fTimer < pedFlashTime))
{
digitalWrite(pedCrossingPin, dummyFlash);
}
else
{
fTimer = millis();
dummyFlash =! dummyFlash;
}
}
void lightControl(byte eG, byte eY, byte eR, byte nG, byte nY, byte nR)
{
digitalWrite(eastGreenPin, eG);
digitalWrite(eastYellowPin, eY);
digitalWrite(eastRedPin, eR);
digitalWrite(northGreenPin, nG);
digitalWrite(northYellowPin, nY);
digitalWrite(northRedPin, nR);
}

**********************

You might also like