CrossGuard
|
A class to control a traffic light system using LEDs. More...
#include <TrafficLight.h>
Public Types | |
enum class | State { OFF , RED , GREEN , YELLOW } |
Represents the possible states of a traffic light. More... | |
Public Member Functions | |
TrafficLight (int redPin, int greenPin) | |
Constructor for a traffic light with specified pins for the red and green LEDs. | |
TrafficLight (int redPin, int greenPin, int yellowPin) | |
Constructor for a traffic light with specified pins for the red, green, and yellow LEDs. | |
void | turnRed () |
Sets the traffic light to red. | |
void | turnGreen () |
Sets the traffic light to green. | |
void | turnYellow () |
Sets the traffic light to yellow. | |
void | turnOff () |
Turns off all lights. | |
A class to control a traffic light system using LEDs.
Manages the state of a traffic light using individual LEDs for each color.
This class encapsulates the functionality required to control a traffic light system consisting of red, yellow, and green LEDs connected to GPIO pins. It allows for changing the state of the traffic light among red, yellow, green, and off states.
The TrafficLight class uses three LED instances to represent the red, green, and yellow lights of a traffic light. It allows for control of these lights through public methods that simulate typical traffic light operations.
|
strong |
TrafficLight::TrafficLight | ( | int | redPin, |
int | greenPin ) |
TrafficLight::TrafficLight | ( | int | redPin, |
int | greenPin, | ||
int | yellowPin ) |
Constructor for a traffic light with specified pins for the red, green, and yellow LEDs.
Constructor for initializing the TrafficLight with all three LEDs. This constructor initializes a traffic light with separate LEDs for red, green, and yellow colors. Each LED is connected to a specific GPIO pin, which is passed to the LED's constructor for initialization. The initial state of the traffic light is set to OFF, ensuring that all LEDs are deactivated at the start.
void TrafficLight::turnGreen | ( | ) |
Sets the traffic light to green.
Activates the green LED and deactivates other LEDs.
This method changes the traffic light's state to GREEN, allowing traffic to proceed through the intersection. The green LED is activated, and both the red and yellow LEDs are deactivated. This ensures clear signaling without any ambiguity for the drivers.
void TrafficLight::turnOff | ( | ) |
Turns off all lights.
Deactivates all LEDs, setting the traffic light state to OFF.
This method sets the traffic light's state to OFF, turning off all LEDs. This state might be used during times of low traffic demand or specific traffic management scenarios. Turning off the lights ensures power savings and can also be part of safety protocols during emergencies or maintenance.
void TrafficLight::turnRed | ( | ) |
Sets the traffic light to red.
Turns the traffic light red by activating the red LED and turning off others. This method changes the traffic light's state to RED. It ensures that the red LED is turned on while the green and yellow LEDs are turned off to prevent any conflicting signals. This state change reflects the typical behavior of a traffic light controlling traffic at an intersection.
void TrafficLight::turnYellow | ( | ) |
Sets the traffic light to yellow.
Activates the yellow LED and deactivates other LEDs.
This method changes the traffic light's state to YELLOW. The yellow LED is turned on, and both the red and green LEDs are turned off. The activation of the yellow light typically signals that the traffic light will soon change to red, preparing drivers to stop.