arduino daq

6
ENGR 2167 : DAQ with MATLAB, ARDUINO Summer 2014 Week 1 Traffic Lights System Kirti Deo Mishra mishra.98 May 13, 2014

Upload: kirti-deo-mishra

Post on 22-Dec-2015

40 views

Category:

Documents


3 download

DESCRIPTION

Introduction TO DAQ using Arduino

TRANSCRIPT

Page 1: Arduino DAQ

ENGR 2167 : DAQ with MATLAB,ARDUINO

Summer 2014Week 1

Traffic Lights System

Kirti Deo Mishramishra.98May 13, 2014

Page 2: Arduino DAQ

1

Page 3: Arduino DAQ

/*

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 eastGreenPin = 14;

byte eastYellowPin = 15;

byte eastRedPin = 16;

byte northGreenPin = 19;

byte northYellowPin = 18;

byte 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

Page 4: Arduino DAQ

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);

3

Page 5: Arduino DAQ

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

Page 6: Arduino DAQ

// 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);

}

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

5