Question

In: Computer Science

In C code Consider the finite state machine shown below that controls a two story elevator....

In C code Consider the finite state machine shown below that controls a two story elevator. The elevator has a toggle button that can be in the UP position or the DOWN position, and an LED light that can be RED or GREEN . When the elevator is on the GROUND floor the light is RED, and when the elevator is on the FIRST floor the light is GREEN.

Assume the existence of function

enum Button getButton();

that returns a value of DOWN or UP, given enumeration

enum Button {DOWN, UP};

Note the function getButton() already exists so you do not need to define this function, you just need to call this function.

First, define two additional enumerations named State and LED to represent the values GROUND and FIRST, and RED and GREEN, respectively.

Second, write an infinite while loop that first sets the value of a variable that represents the color of the led to either RED or GREEN according to which floor the elevator is currently stationed. Then call function getButton() to read the current position of the toggle button and reassign the value of a variable to represent the next desired floor state of the elevator to either GROUND or FIRST. Assign the elevator to initially reside on the GROUND floor, and assume the elevator moves infinitely fast and will move to the desired floor immediately when the floor state variable is reassigned.

Only show the enumerations and while loop code. Do not define any functions or an entire program with a main function.

Solutions

Expert Solution

Enumerations :

enum State {GROUND,FIRST};
enum LED {RED,GREEN};

While-loop code:

//Variable declaration

enum LED light;     //variable to store led light as RED = 0 or GREEN = 1
enum STATE floor;   //variable to store floor state as GROUND = 0 or FIRST = 1
floor = GROUND;     //Since initially the elevator is at ground floor(mentioned in the problem statement)

//LOOP starts
while(1)
{
  if(floor)        //if floor = 1 i.e floor state is First floor
  {
    light=GREEN;   //since floor state is first floor, Led color is green
  }
  else             //else if floor = 0 i.e floor state is Ground floor
  {
    light=RED;     //since floor state is ground floor, Led color is red
  }

  enum BUTTON btn = getButton();   //to read the current value of the toggle button

  if(btn)            //when btn = 1 i.e toggle button is UP
  {
    floor=FIRST;     //floor value is set to FIRST i.e 1
  }
  else               //when btn = 0 i.e toggle button is DOWN
  {
    floor=GROUND;    //floor value is set to ground i.e 0
  }     
}

Related Solutions

Consider the finite state machine shown below that controls a two story elevator. The elevator has...
Consider the finite state machine shown below that controls a two story elevator. The elevator has a toggle button that can be in the UP position or the DOWN position, and an LED light that can be RED or GREEN . When the elevator is on the GROUND floor the light is RED, and when the elevator is on the FIRST floor the light is GREEN. Assume the existence of function enum Button getButton(); that returns a value of DOWN...
Code the FSM in C++, and show that the program works. Construct a Finite State Machine...
Code the FSM in C++, and show that the program works. Construct a Finite State Machine that models an old-fashioned soda machine that accepts nickels, dimes, and quarters. The soda machine accepts change until 35 cents have been put in. It gives change back for any amount greater than 35 cents. Then the customer can push buttons to receive either a cola, a root beer, or a ginger ale.
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just...
Write a VHDL code to implement a Finite State Machine that with an 8 bit sequence...
Write a VHDL code to implement a Finite State Machine that with an 8 bit sequence input (can be any sequence, but lets say it is 11001000), determine how many states there are as well; so if the input sequence is correct it will show the number 1 in a 7 segment display, otherwise it will be 0 in the same 7 segment display. If the input sequence is incorrect, start from the beginning.
Consider a finite state machine with a control input called mode. When mode = 0, the...
Consider a finite state machine with a control input called mode. When mode = 0, the machine operates as a mod-3 down counter, where the outputs are the count values. When mode = 1, the machine's output progresses through the last 4 digits of your WCU ID (1133) number (1 digit per clock cycle). Complete each of the steps which follow. (a) Draw the state diagram for this machine. (b) Write RTL Verilog code which implements this design. Submit your...
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing...
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing machine? What is a Turing complete language? Compare the finite-state machine , pushdown automaton , and Turing machine.?
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
Now considering an SR NAND latch as a finite state machine, please draw the corresponding state...
Now considering an SR NAND latch as a finite state machine, please draw the corresponding state transition diagram. You don’t have to show the outputs in the diagram.
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only)....
Translate the C function code below to the MIPS True Assembler Language code (machine instructions only). The function code should follow the conventions for MIPS function calls including passing parameters and returning results. Your function code must be written with the minimum number of machine instructions to be executed and without any use of MIPS pseudo-instructions. Myfunction(unsigned int a, unsigned int b, unsigned int c) { int i=0; while (a > c) { a /= b; i++; } return i;...
Write a truth table for Moore finite state machine modeling a traffic light.
Write a truth table for Moore finite state machine modeling a traffic light.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT