🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

This is a simulation for a traffic light system with time durations for red, green, and yellow lights. The program includes a feature for pedestrians to request crossing. Let's create this useful application.
this_thread::sleep_for
, for delays#include <iostream>
#include <thread>
#include <chrono>
#include <string>
using namespace std;
// Function to display the traffic light
void displayLight(const string& lightColor, int duration) {
cout << "\nLight: " << lightColor << " (" << duration << " seconds)\n";
for (int i = duration; i > 0; --i) {
cout << "\rTime remaining: " << i << " seconds" << flush;
this_thread::sleep_for(chrono::seconds(1));
}
cout << endl;
}
// Function to simulate the traffic light system
void trafficLightSimulator() {
bool pedestrianRequest = false;
while (true) {
if (pedestrianRequest) {
// Pedestrian crossing
cout << "\nPedestrian crossing: Light is RED for 10 seconds.\n";
displayLight("RED", 10);
pedestrianRequest = false; // Reset pedestrian request
} else {
// Normal traffic light cycle
displayLight("RED", 10); // Red light for 10 seconds
displayLight("GREEN", 8); // Green light for 8 seconds
displayLight("YELLOW", 3); // Yellow light for 3 seconds
}
// Check for pedestrian request
cout << "Press 'P' to request a pedestrian crossing or any other key to continue: ";
char choice;
cin >> choice;
if (tolower(choice) == 'p') {
pedestrianRequest = true;
}
}
}
int main() {
cout << "--- Traffic Light Simulator ---\n";
cout << "This program simulates a traffic light system with pedestrian crossing requests.\n";
cout << "Press Ctrl+C to exit the program at any time.\n";
trafficLightSimulator();
return 0;
}
displayLight
is the function that displays the light and calculates the time duration.this_thread::sleep_for
function is used to introduce delays, simulating real-life traffic light behavior.while (true)
loop ensures that the normal light cycle continues.--- Traffic Light Simulator ---
This program simulates a traffic light system with pedestrian crossing requests.
Press Ctrl+C to exit the program at any time.
Light: RED (10 seconds)
Time remaining: 10 seconds
...
Time remaining: 1 second
Light: GREEN (8 seconds)
Time remaining: 8 seconds
...
Time remaining: 1 second
Light: YELLOW (3 seconds)
Time remaining: 3 seconds
...
Time remaining: 1 second
Press 'P' to request a pedestrian crossing or any other key to continue:
Pedestrian request
Press 'P' to request a pedestrian crossing or any other key to continue: P
Pedestrian crossing: Light is RED for 10 seconds.
Light: RED (10 seconds)
Time remaining: 10 seconds
...
Time remaining: 1 second
Traffic light control systems are crucial in modern cities to maintain efficiency and road safety. Today, smart traffic management systems use artificial intelligence and sensors to optimize light timings based on real-time traffic conditions. Notable deployments include:
This program provides a basic understanding of these systems through a simplified simulation.
Los Angeles, known for its heavy traffic congestion, implemented an advanced traffic control system that successfully reduced congestion. The system features:
This showcases how an efficient traffic system benefits urban development.
Developing an efficient traffic management system requires tackling common challenges such as:
This simulation is a basic step toward understanding real-world traffic management and its problem-solving techniques. I hope you liked this project idea. See you next time with another cool, useful, and handy project!
Comments
Post a Comment