Traffic Lights. Suppose we want to design a traffic light controller for road cr
ID: 3636533 • Letter: T
Question
Traffic Lights. Suppose we want to design a traffic light controller for road crossing.And the lights are to be installed as shown in the figure.
And the sequence of operation is:
Start North – South East – West
0 RED RED
1 RED YELLOW
1 RED GREEN
1 RED GREEN
1 RED GREEN
1 RED YELLOW
1 RED RED
1 YELLOW RED
1 GREEN RED
1 GREEN RED
1 GREEN RED
1 YELLOW RED
1 RED RED
………………and so on.
There are six lights to operate. The Red, yellow, and Green lights in the North-South direction will be
designated as R1, A1, G1. Similarly, the lights in the East-West direction will be called R2, A2, and G2. When the digital signals are in the Logic-1 state they turn their respective lights on, otherwise the lights are off. A digital clock signal will be supplied and at each clock pulse the lights should change according the schedule given above. There are two types of road crossing: operating mode safe crossings that use a simple sequence, and unsafe crossings that require a blinking Red operation. One digital input signal called START will indicate whether the road crossing is to operate in normal mode and considered safe, or it will operate in emergency mode and blink RED on each direction. (you can add more functionality. Thus, we have a one-input, six-output synchronous system to design.
Explanation / Answer
// TrafficLight.h file
#ifndef _TRAFFIC_LIGHT_CLASS_DEFINED_
#define _TRAFFIC_LIGHT_CLASS_DEFINED_
class TrafficLight
{
public:
enum LightState {RED = 0, YELLOW, GREEN};
public:
TrafficLight();
void Advance();
void Wait();
char* CurrentState();
private:
LightState m_state;
int m_Delay[3];
};
#endif
// TrafficLight.cpp file
#include "Windows.h"
#include "TrafficLight.h"
TrafficLight::TrafficLight()
{
m_state = RED;
m_Delay[ RED ] = 10 * 1000; // msec value
m_Delay[ YELLOW ] = 4 * 1000; // msec value
m_Delay[ GREEN ] = 15 * 1000; // msec value
};
void TrafficLight::Advance()
{
switch (m_state)
{
case RED: m_state = GREEN; break;
case YELLOW: m_state = RED; break;
case GREEN: m_state = YELLOW; break;
}
};
void TrafficLight::Wait()
{
Sleep(m_Delay[m_state]);
};
char* TrafficLight::CurrentState()
{
switch (m_state)
{
case RED: return "Red"; break;
case YELLOW: return "Yellow"; break;
case GREEN: return "Green"; break;
}
return "Error";
}
// LightSim.cpp file
// LightSim.cpp : Defines the entry point for the console application.
//
#include
#include "TrafficLight.h"
int main(int argc, char* argv[])
{
TrafficLight light1;
while (1)
{
printf("Light is currently %s ", light1.CurrentState());
light1.Wait();
light1.Advance();
};
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.