Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Build the state machine given in the next figure. You will notice that the SENSO

ID: 3816069 • Letter: B

Question

Build the state machine given in the next figure. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. This may cause unintentional state change if you hold a button for too long. First, complete the assignment, then fix this runtime behavior using any solution you come up with.

Program Behavior:

Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.

In the sensor state, print the your favorite sensor value. Due to the 100ms delay, as long as you are in this state, you should see your sensor values 10 times a second.

Hints:

The transition out of a state should be handled in the same state. In other words, if button 1 takes you out of the start state, then write your if(button1) condition should be in this start state.

Needless to mention, but whatever activity that occurs in a state should have its code underneath this state.

( C Language )

Explanation / Answer

#include <stdio.h>

#include <stdbool.h>

int main()

{

    typedef enum {start, end} Type_State;

    Type_State Curr_State = start;

    printf("Press 0 to go to END ");

    bool button0;

    bool button1;

    while(1)

    {

        delay_ms(100);

  

        switch(Curr_State)

        {

            case start:

                if(button0)

                {

                    Curr_State = end;

                                    printf("Current State: END. Press 1 to go to START ");

                }

                break;

            case end:

                if(button1)

                {

                    Curr_State = start;

                                    printf("Current State: START. Press 0 to go to END ");

                }

                break;

            default:

                printf("ERROR!!! State machine ");

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote