CrossGuard
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
TrafficLight Class Reference

A class to control a traffic light system using LEDs. More...

#include <TrafficLight.h>

Inheritance diagram for TrafficLight:
CarsTrafficLightSystem PedestriansTrafficLightSystem

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.
 

Detailed Description

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.

Member Enumeration Documentation

◆ State

enum class TrafficLight::State
strong

Represents the possible states of a traffic light.

Enumerator
OFF 

Indicates that all lights are off.

RED 

Indicates that the red light is on.

GREEN 

Indicates that the green light is on.

YELLOW 

Indicates that the yellow light is on.

Constructor & Destructor Documentation

◆ TrafficLight() [1/2]

TrafficLight::TrafficLight ( int redPin,
int greenPin )

Constructor for a traffic light with specified pins for the red and green LEDs.

Parameters
redPinGPIO pin connected to the red LED.
greenPinGPIO pin connected to the green LED.

◆ TrafficLight() [2/2]

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.

Parameters
redPinGPIO pin connected to the red LED.
greenPinGPIO pin connected to the green LED.
yellowPinGPIO pin connected to the yellow LED.
redPinPin for the red LED.
greenPinPin for the green LED.
yellowPinPin for the yellow LED.

Member Function Documentation

◆ turnGreen()

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.

◆ turnOff()

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.

◆ turnRed()

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.

◆ turnYellow()

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.


The documentation for this class was generated from the following files: