Real-Time Traffic Control System with Traffic Lights in C++

In this exercise, you will create a C++ application that simulates a real-time traffic control system using traffic lights. The program will simulate the changing of traffic lights (red, yellow, and green) at regular intervals, similar to how traffic lights work in real life. The system will cycle through the lights in the proper order, and the user can specify the duration of each light color. This simulation demonstrates how to implement a basic real-time control system for managing traffic lights.

Group

Real-Time Programming in C++

Objective

Write a C++ program that simulates a traffic control system with traffic lights. The program should cycle through the traffic light colors (red, yellow, and green) at regular intervals, simulating the real-time operation of a traffic light. Allow the user to input the duration for each light color. The system should continuously repeat this cycle.

Implement a real-time traffic control system with traffic lights.

Example C++ Exercise

 Copy C++ Code
#include <iostream>     // Include the input-output stream library
#include <thread>        // Include the thread library for controlling time intervals
#include <chrono>        // Include the chrono library for time manipulation
using namespace std;

// Function to simulate the traffic light control system
void trafficLightControl(int redDuration, int yellowDuration, int greenDuration) {
    while (true) {  // Infinite loop to simulate continuous traffic light cycles
        // Simulate Red Light
        cout << "RED LIGHT - Stop" << endl;
        this_thread::sleep_for(chrono::seconds(redDuration));  // Wait for the specified red light duration

        // Simulate Yellow Light
        cout << "YELLOW LIGHT - Prepare to stop or go" << endl;
        this_thread::sleep_for(chrono::seconds(yellowDuration));  // Wait for the specified yellow light duration

        // Simulate Green Light
        cout << "GREEN LIGHT - Go" << endl;
        this_thread::sleep_for(chrono::seconds(greenDuration));  // Wait for the specified green light duration
    }
}

int main() {
    int redDuration, yellowDuration, greenDuration;

    // Ask the user to enter the duration for each light color
    cout << "Enter the duration for RED light in seconds: ";
    cin >> redDuration;

    cout << "Enter the duration for YELLOW light in seconds: ";
    cin >> yellowDuration;

    cout << "Enter the duration for GREEN light in seconds: ";
    cin >> greenDuration;

    // Start the traffic light control system with the specified durations
    trafficLightControl(redDuration, yellowDuration, greenDuration);

    return 0;
}

 Output

Enter the duration for RED light in seconds: 5
Enter the duration for YELLOW light in seconds: 2
Enter the duration for GREEN light in seconds: 3
RED LIGHT - Stop
(Yellow light cycle after 5 seconds)
YELLOW LIGHT - Prepare to stop or go
(Green light cycle after 2 seconds)
GREEN LIGHT - Go
(Red light cycle after 3 seconds)
...

Share this C++ Exercise


More C++ Programming Exercises of Real-Time Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Real-Time Production Machine Control Simulation in C++

    In this exercise, you will develop a C++ program that simulates the control of a production machine in real time. The program will simulate different machine states (e.g., "Idle", ...

  • Real-Time Event-Based Notification System in C++

    In this exercise, you will develop a C++ program that simulates a real-time notification system based on events. The system will monitor specific events, such as changes in the sta...

  • Task Scheduling System in C++

    In this exercise, you will implement a C++ program that simulates a task scheduling system. The system will allow tasks to be scheduled at specific intervals and execute them based...

  • Real-Time Fleet Tracking Application in C++

    This program simulates the tracking of a fleet of vehicles in real-time. It uses basic data structures to store vehicle information such as position (latitude and longitude), speed...

  • Event-Driven Alarm System Simulation in C++

    This program simulates an alarm system that detects specific events and triggers alarms based on those events. The system has multiple sensors (e.g., motion, temperature, and door ...

  • Real-Time Clock System in C++ that Updates Every Second

    In this exercise, you will develop a real-time clock system in C++ that updates every second. The goal is to create a simple digital clock that continuously updates the time every ...