Question

Build a state-machine embedded application as an Arduino sketch (program) that does the following... if the...

Build a state-machine embedded application as an Arduino sketch (program) that does the following...

  • if the motor is stopped, when the user pushes a push-button (which I will call INPUT1), the motor begins rotating
    • if there is a currVelocity set, the motor rotates at this velocity
    • if currVelocity has not been set, the motor rotates at an initial slow velocity (chosen by you)
  • if the motor is running, when the user pushes the push-button INPUT1, the motor speeds up again (to 2nd velocity, and beyond). Once the motor reaches the maximum velocity, additional pushes of INPUT1 have no effect. The various velocities are chosen by you, but you must use at least 4 different velocities.
  • if the motor is running, and the user activates another piece of input hardware (*YOUR CHOICE*...but I will call it INPUT2) the motor stops, but remembers its current velocity in variable currVelocity
  • if the motor is stopped, and the user activates INPUT2, the currVelocity is cleared (reset to 0)

Requirements:

* The states are listed in an enumeration in the code, and have capital letters as their name

* When a state is entered (including the initial state, named something like INIT), a function is called which is named after that state, like stateInit(...)

* Each state function (named after the state, e.g. stateInit(...) ) has two segments of code

   * a portion of code that is only executed once each time the state is entered

   * a portion of code that executes repeatedly as long as you're in the state (typically a conditional to see if we must transition out of the state)

* The loop() function has a switch statement inside which identifies the current state (e.g. INIT) and calls that state's function (e.g. stateInit(...) )

0 0
Add a comment Improve this question Transcribed image text
Answer #1

What Is a State Machine?
A state machine it’s not a machine in the same way that a lawn mower or a typewriter is a machine. It is more of an abstract concept or system that helps you systematically design and implement the logic behaviour of an embedded system. In fact, the state machine concept is so abstract that you can use it to much more than just embedded system logic, but in this post we’ll concentrate on state machine usage for embedded systems.

To create a state machine you need a set of states, inputs and outputs.

States
An embedded system should at any given time be in a defined state, whether it is “moving left”, “door open”, “stopped”, “init”, “error” or any other imaginable state. The states and the transitions between them make up the main structure of the state machine.

Inputs
Inputs are what makes the system switch states and can for instance be switches, buttons and sensors or any other typical embedded input.

Outputs
Outputs in a state machine can be motor movement, lights or any other typical embedded output.

enum State_enum {STOP, FORWARD, ROTATE_RIGHT, ROTATE_LEFT};
enum Sensors_enum {NONE, SENSOR_RIGHT, SENSOR_LEFT, BOTH};

void state_machine_run(uint8_t sensors);
void motors_stop();
void motors_forward();
void motors_right();
void motors_left();
uint8_t read_IR();

uint8_t state = STOP;

void setup(){
}

void loop(){
state_machine_run(read_IR());

delay(10);
}

void state_machine_run(uint8_t sensors)
{
switch(state)
{
case STOP:
if(sensors == NONE){
motors_forward();
state = FORWARD;
}
else if(sensors == SENSOR_RIGHT){
motors_left();
state = ROTATE_LEFT;
}
else{
motors_right();
state = ROTATE_RIGHT;
}
break;

case FORWARD:
if(sensors != NONE){
motors_stop();
state = STOP;
}
break;

case ROTATE_RIGHT:
if(sensors == NONE || sensors == SENSOR_RIGHT){
motors_stop();
state = STOP;
}
break;

case ROTATE_LEFT:
if(sensors != SENSOR_RIGHT)
{
motors_stop();
state = STOP;
}
break;
}
}

void motors_stop()
{
//code for stopping motors
}

void motors_forward()
{
//code for driving forward
}

void motors_right()
{
//code for turning right
}

void motors_left()
{
//code for turning left
}

uint8_t read_IR()
{
//code for reading both sensors
}

Add a comment
Know the answer?
Add Answer to:
Build a state-machine embedded application as an Arduino sketch (program) that does the following... if the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT