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", "Running", "Stopped") and perform actions based on specific time intervals. The simulation will allow you to control when the machine starts, stops, or enters idle mode. This example demonstrates how to model real-time system behavior for controlling production equipment or machinery.

Group

Real-Time Programming in C++

Objective

Write a C++ program that simulates the real-time control of a production machine. The program should simulate the machine's states (Idle, Running, Stopped) and update its state at regular intervals. Allow the user to input the time the machine spends in each state, and continuously run the machine, changing its states based on the specified intervals.

Write a program that simulates the control of a production machine in real time.

Example C++ Exercise

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

// Function to simulate the production machine control system
void productionMachineControl(int idleDuration, int runningDuration, int stoppedDuration) {
    while (true) {  // Infinite loop to simulate continuous machine control
        // Simulate Idle State
        cout << "Machine is IDLE - Not in operation" << endl;
        this_thread::sleep_for(chrono::seconds(idleDuration));  // Wait for the specified idle duration

        // Simulate Running State
        cout << "Machine is RUNNING - Production in progress" << endl;
        this_thread::sleep_for(chrono::seconds(runningDuration));  // Wait for the specified running duration

        // Simulate Stopped State
        cout << "Machine is STOPPED - Production halted" << endl;
        this_thread::sleep_for(chrono::seconds(stoppedDuration));  // Wait for the specified stopped duration
    }
}

int main() {
    int idleDuration, runningDuration, stoppedDuration;

    // Ask the user to input the time durations for each machine state
    cout << "Enter the duration for IDLE state in seconds: ";
    cin >> idleDuration;

    cout << "Enter the duration for RUNNING state in seconds: ";
    cin >> runningDuration;

    cout << "Enter the duration for STOPPED state in seconds: ";
    cin >> stoppedDuration;

    // Start the production machine control system with the user-defined durations
    productionMachineControl(idleDuration, runningDuration, stoppedDuration);

    return 0;
}

 Output

Enter the duration for IDLE state in seconds: 3
Enter the duration for RUNNING state in seconds: 5
Enter the duration for STOPPED state in seconds: 2
Machine is IDLE - Not in operation
Machine is RUNNING - Production in progress
Machine is STOPPED - Production halted
...

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++.