A traffic light controller is to be designed at the intersection of HWY9 and CR6
ID: 665819 • Letter: A
Question
A traffic light controller is to be designed at the intersection of HWY9 and CR66. The intersection is a four way intersection with left turn lanes for both directions of HWY9 and CR66. The traffic light controller will use input sensors to determine if vehicles are waiting in certain lanes and will use that information to determine how the lights at the intersection should be controlled. The inputs and outputs will be implemented onto the Mega2560r3 board using C or C++ programing on the Arduino programmer.
Inputs:
`HWY9 Left Lane Sensor Switch simulates detecting a vehicle waiting to turn left from HWY9.
`CR66 Left Lane Sensor Switch simulates detecting a vehicle waiting to turn left from CR66
`CR66 Sensor switch simulates detecting a vehicle waiting on either side of CR66
`Train sensor switch
simulates a train arriving when in the ON position. While a train is in
the intersection, 2 red LEDs should alternately blink at 1 Hz. (± 10%)
Outputs:
- Red, Yellow, and Green LEDs to indicate the East-West CR66 status
- Red, Yellow, and Green LEDs to indicate the North-South HWY9 status
- Yellow and Green LEDs to indicate the Left Turn Lane status for both Highways
- A strobing LED at 2 Hz with a Duty Cycle of 30% whenever HWY9 is Red
- 2 Red LEDs that are off when no train is present and otherwise blink at 2 Hz
Technology: dip switch, logic bread board, Mega 2560 r3 board.
Priority review of the light sequence: Highest priority is given to the
default state which is Green for HWY9 & Red for CR66 with
all Left lanes not green. The next most priority is given to the CR66
Left Turn lanes followed by a Green light for traffic traveling straight on
CR66. Before returning to the default state, the Left Lanes for
HWY9 have priority if a sufficient number of vehicles are waiting to
turn (ie. 1 or more). If the train going across CR66 is present, the system is in the default position with the addition of the associated train led outputs. This priority characterization is quite normal for T-lights.
Explanation / Answer
/* #include "TrafficLight.h" TrafficLight::TrafficLight(int redLightPin, int yellowLightPin, int greenLightPin, Signal signalState) { this->redLightPin = redLightPin; this->yellowLightPin = yellowLightPin; this->greenLightPin = greenLightPin; if (yellowLightPin == NULL && ((signalState == YELLOW) || (signalState == RED_YELLOW))) { this->signalState = RED; } else { this->signalState = signalState; } this->buzzerPin = NULL; pinMode(redLightPin, OUTPUT); if (yellowLightPin != NULL) { pinMode(yellowLightPin, OUTPUT); } pinMode(greenLightPin, OUTPUT); switch(signalState) { case RED : switchSignalToRed(); break; case RED_YELLOW : switchSignalToRedYellow(); break; case GREEN : switchSignalToGreen(); break; case YELLOW : switchSignalToYellow(); break; } } void TrafficLight::enableAlert(int buzzerPin) { this->buzzerPin = buzzerPin; pinMode(buzzerPin, OUTPUT); } void TrafficLight::switchSignalToRedYellow() { if ((yellowLightPin == NULL) || (signalStandard == USA)) { switchSignalToRed(); return; } digitalWrite(redLightPin, HIGH); digitalWrite(yellowLightPin, HIGH); digitalWrite(greenLightPin, LOW); if (buzzerPin != NULL) { digitalWrite(buzzerPin, LOW); } } void TrafficLight::switchSignalToGreen() { digitalWrite(redLightPin, LOW); if (yellowLightPin != NULL) { digitalWrite(yellowLightPin, LOW); } digitalWrite(greenLightPin, HIGH); if (buzzerPin != NULL) { digitalWrite(buzzerPin, HIGH); } } void TrafficLight::switchSignalToYellow() { if (yellowLightPin == NULL) { switchSignalToRed(); return; } digitalWrite(redLightPin, LOW); digitalWrite(yellowLightPin, HIGH); digitalWrite(greenLightPin, LOW); if (buzzerPin != NULL) { digitalWrite(buzzerPin, LOW); } } void TrafficLight::switchSignalToRed() { digitalWrite(redLightPin, HIGH); if (yellowLightPin != NULL) { digitalWrite(yellowLightPin, LOW); } digitalWrite(greenLightPin, LOW); if (buzzerPin != NULL) { digitalWrite(buzzerPin, LOW); } } void TrafficLight::switchSignal() { switch(signalState) { case RED : switchSignalToRedYellow(); signalState = RED_YELLOW; break; case RED_YELLOW : switchSignalToGreen(); signalState = GREEN; break; case GREEN : switchSignalToYellow(); signalState = YELLOW; break; case YELLOW : switchSignalToRed(); signalState = RED; break; } } Trafficlight.h class TrafficLight { public: enum Signal {RED=0, RED_YELLOW, GREEN, YELLOW}; enum SignalStandard {GERMANY=0, USA}; static SignalStandard signalStandard; TrafficLight(int redLightPin, int yellowLightPin, int greenLightPin, Signal signalState); void enableAlert(int buzzerPin); void switchSignal(); private: int redLightPin; // make uint8_t int yellowLightPin; int greenLightPin; int buzzerPin; Signal signalState; void switchSignalToRedYellow(); void switchSignalToGreen(); void switchSignalToYellow(); void switchSignalToRed(); };
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.