Build a state-machine embedded application as an Arduino sketch (program) that does the following...
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(...) )
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
}
Build a state-machine embedded application as an Arduino sketch (program) that does the following... if the...
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...