Create Traffic Light Simulation in C

Faraz

By Faraz -

Learn how to create a traffic light simulation in C programming with our easy-to-follow guide.


create-traffic-light-simulation-in-c.webp

Creating a traffic light simulation in C programming is a great way to enhance your coding skills and understand basic programming concepts. This project involves designing a simple user interface, implementing the traffic light logic, and running the simulation. Whether you're a beginner or looking to refresh your knowledge, this guide will walk you through each step with clear instructions and tips. By the end, you'll have a fully functional traffic light simulation with a beautiful user interface menu.

Step-by-Step Guide

1. Setting Up the Project

First, ensure you have a C compiler installed on your system, such as GCC or Clang. Create a new C file for your project, for example, traffic_light_simulation.c.

2. Designing the User Interface Menu

A user-friendly interface is crucial for any program. In this project, we'll use simple text-based menu options for interaction. Here’s a sample code to create a basic menu:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  
#include <stdbool.h>

int redDuration = 3;
int yellowDuration = 2;
int greenDuration = 3;
bool isPaused = false;

void displayMenu() {
    printf("\n");
    printf("=====================================\n");
    printf("       Traffic Light Simulation       \n");
    printf("=====================================\n");
    printf("1. Start Simulation\n");
    printf("2. Adjust Timers\n");
    printf("3. Pause/Resume Simulation\n");
    printf("4. Exit\n");
    printf("Please choose an option: ");
}

void displayLight(int light) {
    printf("\n");
    printf("Traffic Light State:\n");
    printf("      _______\n");
    printf("     |       |\n");
    printf("     |  %s  |\n", (light == 1) ? " RED " : "   ");
    printf("     |_______|\n");
    printf("     |       |\n");
    printf("     |  %s  |\n", (light == 2) ? " YLW " : "   ");
    printf("     |_______|\n");
    printf("     |       |\n");
    printf("     |  %s  |\n", (light == 3) ? " GRN " : "   ");
    printf("     |_______|\n");
}

3. Implementing Timer Adjustment Function

  • Add a function to allow users to adjust the timer values.
void adjustTimers() {
    printf("\nAdjust Timer Durations:\n");
    printf("Enter duration for Red Light (default 3 seconds): ");
    scanf("%d", &redDuration);
    printf("Enter duration for Yellow Light (default 2 seconds): ");
    scanf("%d", &yellowDuration);
    printf("Enter duration for Green Light (default 3 seconds): ");
    scanf("%d", &greenDuration);
}

4. Implementing the Traffic Light Logic

Now, let’s write the logic for the traffic light simulation. We’ll use a loop to simulate the traffic light changing colors.

void startSimulation() {
    int choice;
    while (1) {
        printf("\nTraffic Light Simulation Menu:\n");
        printf("1. Red Light\n");
        printf("2. Yellow Light\n");
        printf("3. Green Light\n");
        printf("4. Exit Simulation\n");
        printf("Choose an option: ");
        scanf("%d", &choice);

        if (isPaused) {
            printf("Simulation is paused. Please resume to continue.\n");
            continue;
        }

        switch (choice) {
            case 1:
                displayLight(1);
                printf("Red Light - Stop\n");
                sleep(redDuration); // Light duration
                break;
            case 2:
                displayLight(2);
                printf("Yellow Light - Prepare to Stop\n");
                sleep(yellowDuration); // Light duration
                break;
            case 3:
                displayLight(3);
                printf("Green Light - Go\n");
                sleep(greenDuration); // Light duration
                break;
            case 4:
                printf("Exiting simulation...\n");
                exit(0);
            default:
                printf("Invalid option. Please try again.\n");
        }
    }
}

int main() {
    int option;
    while (1) {
        displayMenu();
        scanf("%d", &option);

        switch (option) {
            case 1:
                startSimulation();
                break;
            case 2:
                adjustTimers();
                break;
            case 3:
                isPaused = !isPaused;
                printf(isPaused ? "Simulation paused.\n" : "Simulation resumed.\n");
                break;
            case 4:
                printf("Exiting...\n");
                exit(0);
            default:
                printf("Invalid option. Please try again.\n");
        }
    }
    return 0;
}

5. Compiling and Running the Program

To compile the program, open your command line or terminal and navigate to the directory containing your traffic_light_simulation.c file. Use the following command:

gcc -o traffic_light_simulation traffic_light_simulation.c

This command compiles the C file and creates an executable named traffic_light_simulation. To run the program, use:

./traffic_light_simulation

6. Testing the Simulation

Once the program is running, test all options in the menu to ensure the traffic light simulation works as expected. Verify that the lights change correctly and that the exit option closes the program.

Conclusion

In this guide, we've walked through creating a traffic light simulation in C programming from start to finish. We covered setting up the project, designing a simple user interface, implementing the traffic light logic, and compiling and running the program. This project is a fantastic way to practice C programming and learn how to build interactive applications. By following these steps, you now have a working traffic light simulation with a user-friendly menu.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺