FSM Lecture 7: Exercise-002 LED control Moore machine implementation

  • Post author:
  • Post category:Blog

 

Exercise-002 LED control Moore machine implementation

 

 

We implemented a simple Mealy machine for our light control application in the previous article. You can treat these exercises as just some ‘hello world’ applications.

LED control Moore machine entry actions
Figure 1. Moore machine example

 

In the Moore machine, there is no action during the transition. The ‘action’ or the ‘output’ is produced inside a state, called ‘Entry actions.’ There are four entry actions here.

  1. Light off
  2. Make light dim
  3. Make light-medium brightness
  4. Make light full brightness

As you can see(figure 1), whenever the state ‘OFF’ is entered, the entry action is ‘Light Off.’ Whenever the state ‘Dim’ is entered, the entry action is ‘dim the light,’ like that.

The code is almost the same as the previous one.

 

The new thing here is(Look at below),  it shows the new function run_entry_action.

For this function, you have to pass the state, and as per the state, the run_entry_action function executes the entry action.

void light_state_machine(uint8_t event);
void light_change_intensity(uint8_t pin, uint8_t intensity);
void run_entry_action(light_state_t state);

void light_init(void)
{
curr_state = LIGHT_OFF;
run_entry_action(LIGHT_OFF);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Light control application");
Serial.println("-------------------------");
Serial.println("Send 'x' or 'o'");

light_init();
}

In the setup function (figure 3), I first init the light state machine. This function is to set the first state. The initial state is LIGHT_OFF.

When you first set the state as per the diagram(figure 1), you have to execute the ‘Light off’ entry action. That’s why I call run_entry_action.

enum event{
   ON,
   OFF,
   ENTRY
 };

ENTRY event

Instead of creating the run_entry_action function, you can do one more trick,i.e., implement one more event called ‘ENTRY.’ You can create one more event called ‘ENTRY,’ which is an internal event.

 

void light_state_machine(uint8_t event)
{
light_state_t prev_state;

prev_state = curr_state;
switch(curr_state)
{
  case LIGHT_OFF:{
   switch(event){
    case ON:{
      curr_state = LIGHT_DIM;
    break;
  }
 }
break;
}

Switch case function

And you can implement it here itself—a case ON, case ENTRY, like that. 

That’s about the LED control Moore machine implementation; the output remains the same, there is no change in the output.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.