{"id":7483,"date":"2022-02-16T04:46:43","date_gmt":"2022-02-16T04:46:43","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=7483"},"modified":"2023-08-09T12:37:36","modified_gmt":"2023-08-09T07:07:36","slug":"fsm-lecture-39-exercise-004-implementation-using-state-handler-approach","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/","title":{"rendered":"FSM Lecture 39: Exercise-004 Implementation using state handler approach"},"content":{"rendered":"<div class=\"boldgrid-section\" style=\"background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-top: 35px; padding-bottom: 0px; background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 col-lg-10\">\n<h1 class=\"\" style=\"text-align: center; font-size: 30px; border-width: 0px; line-height: 50px;\"><strong><span style=\"color: #000080;\">Exercise-004 Implementation using state handler approach<\/span><\/strong><\/h1>\n<div class=\"row bg-editor-hr-wrap\" style=\"border-width: 0px; margin-top: -25px;\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\">\n<div>\n<p>&nbsp;<\/p>\n<div class=\"bg-hr bg-hr-10 color2-color\" style=\"border-style: solid; border-width: 0px 0px 3px;\"><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">In this article, let&#8217;s understand the State handler approach. This is almost similar to the previous one. But the important difference is we used the <\/span><b>active_state<\/b><span style=\"font-weight: 400;\"> variable in the main application structure to switch between different states. The <\/span><b>active_state <\/b><span style=\"font-weight: 400;\">reveals what exactly is the current active state. <\/span><\/span><\/p>\n<figure id=\"attachment_7486\" aria-describedby=\"caption-attachment-7486\" style=\"width: 1280px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-7486\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-1-1.png\" alt=\"Figure 1. State handler approach\" width=\"1280\" height=\"720\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png 1280w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-300x169.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-768x432.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-1024x576.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-600x338.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-120x68.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-500x281.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-200x113.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-400x225.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-800x450.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1-1200x675.png 1200w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption id=\"caption-attachment-7486\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 1. State handler approach<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400;\"><span style=\"color: #000000;\">In the state handler approach, the active_state variable will be a function pointer, and by using this variable, we can directly switch between different state handlers of our project. And in the state handler, you use a switch case statement to switch between different signals.<\/span>&nbsp;<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The advantage of this method is you can get rid of the outer switch statement.&nbsp;<\/span><\/p>\n<figure id=\"attachment_7487\" aria-describedby=\"caption-attachment-7487\" style=\"width: 1351px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-7487\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch.png\" alt=\"Figure 2. Outer switch\" width=\"1351\" height=\"633\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch.png 1351w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-300x141.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-768x360.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-1024x480.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-600x281.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-120x56.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-500x234.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-200x94.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-400x187.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-800x375.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-2-outer-switch-1200x562.png 1200w\" sizes=\"(max-width: 1351px) 100vw, 1351px\" \/><figcaption id=\"caption-attachment-7487\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 2. Outer switch<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">There are 2 Switch statements. One switch statement is inside the protimer_state__handler function (every state handler function). The red color line shows an outer switch statement.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s create a new project. To create a new project, go to PlatformIO and Home, and click on New project,&nbsp; as shown in figure 3.<\/span><\/p>\n<figure id=\"attachment_7488\" aria-describedby=\"caption-attachment-7488\" style=\"width: 1731px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-7488\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project.png\" alt=\"Figure 3. Creating a project\" width=\"1731\" height=\"830\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project.png 1731w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-300x144.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-768x368.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-1024x491.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-600x288.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-120x58.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-500x240.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-200x96.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-400x192.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-800x384.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-3-creating-a-project-1200x575.png 1200w\" sizes=\"(max-width: 1731px) 100vw, 1731px\" \/><figcaption id=\"caption-attachment-7488\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 3. Creating a project<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7489\" aria-describedby=\"caption-attachment-7489\" style=\"width: 1333px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7489\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1.png\" alt=\"Figure 4. Creating a project\" width=\"1333\" height=\"669\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1.png 1333w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-300x151.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-768x385.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-1024x514.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-600x301.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-120x60.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-500x251.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-540x272.png 540w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-200x100.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-400x201.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-800x402.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-4-creating-a-project-1-1200x602.png 1200w\" sizes=\"(max-width: 1333px) 100vw, 1333px\" \/><figcaption id=\"caption-attachment-7489\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 4. Creating a project<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The project name is 004Protimer_SH, select the board \u2192 Arduino Uno, and finish the project after that.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7490\" aria-describedby=\"caption-attachment-7490\" style=\"width: 1649px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7490\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-5-1.png\" alt=\"Figure 5. 004Protimer_SH project\" width=\"1649\" height=\"1025\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1.png 1649w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-300x186.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-768x477.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-1024x637.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-600x373.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-320x200.png 320w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-120x75.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-500x311.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-200x124.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-400x249.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-800x497.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-5-1-1200x746.png 1200w\" sizes=\"(max-width: 1649px) 100vw, 1649px\" \/><figcaption id=\"caption-attachment-7490\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 5. 004Protimer_SH project<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">We created a 004Protimer_SH project. Go to that project folder, right-click and go to Reveal in File Explorer, shown in figure 5.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7492\" aria-describedby=\"caption-attachment-7492\" style=\"width: 1395px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7492\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-6.-source-code.png\" alt=\"Figure 6. Source code\" width=\"1395\" height=\"665\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code.png 1395w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-300x143.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-768x366.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-1024x488.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-600x286.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-120x57.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-500x238.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-200x95.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-400x191.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-800x381.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-6.-source-code-1200x572.png 1200w\" sizes=\"(max-width: 1395px) 100vw, 1395px\" \/><figcaption id=\"caption-attachment-7492\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 6. Source code<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And go to the previous project, copy all the source code as shown in Figure 6, return to the new project and paste those files.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And go back to the Visual Code. In 004Protimer_SH, the yellow color line shows all those files (Figure 7).<\/span><\/p>\n<figure id=\"attachment_7493\" aria-describedby=\"caption-attachment-7493\" style=\"width: 1771px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7493\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-7.png\" alt=\"Figure 7. 004Protimer_SH project file\" width=\"1771\" height=\"981\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7.png 1771w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-300x166.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-768x425.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-1024x567.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-600x332.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-120x66.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-500x277.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-200x111.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-400x222.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-800x443.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-7-1200x665.png 1200w\" sizes=\"(max-width: 1771px) 100vw, 1771px\" \/><figcaption id=\"caption-attachment-7493\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 7. 004Protimer_SH project file<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">If you go to the lcd, it shows a red line because we have to add the lcd library. Because, in the previous project, we added that, but it was project-specific.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Go to PlatformIO, click on Open\u2192 Libraries\u2192 type Liquid crystal, as shown in Figure 8.<\/span><\/p>\n<figure id=\"attachment_7494\" aria-describedby=\"caption-attachment-7494\" style=\"width: 1920px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7494\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding.png\" alt=\"Figure 8. Adding LiquidCrystal Library\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding.png 1920w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-300x169.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-768x432.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-1024x576.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-600x338.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-120x68.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-500x281.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-200x113.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-400x225.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-800x450.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-8-liquidcrystal-library-adding-1200x675.png 1200w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><figcaption id=\"caption-attachment-7494\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 8. Adding LiquidCrystal Library<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_7495\" aria-describedby=\"caption-attachment-7495\" style=\"width: 1142px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7495\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/02\/figure-9.png\" alt=\"Figure 9. Adding LiquidCrystal library\" width=\"1142\" height=\"585\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9.png 1142w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-300x154.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-768x393.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-1024x525.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-600x307.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-120x61.png 120w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-500x256.png 500w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-200x102.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-400x205.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-9-800x410.png 800w\" sizes=\"(max-width: 1142px) 100vw, 1142px\" \/><figcaption id=\"caption-attachment-7495\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 9. Adding LiquidCrystal library<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><b>LiquidCrystal by Arduino<\/b><span style=\"font-weight: 400;\">, Click on that(1) \u2192&nbsp; Add to project(2)\u2192&nbsp; select the project(3)\u2192 Add(4), as shown in Figure 9.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s make some changes to the 004Protimer_SH project. First, let&#8217;s go to the main.h. In the main.h, here, various application states are not required; we have to remove it. Because now we are not using these values to store it into a variable, then decode it using a switch statement to find out which handler to call. That&#8217;s why I&#8217;m going to remove various application states. Instead of that, we will use a function pointer.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Go to your main application structure. Now, <\/span><b>active_state<\/b><span style=\"font-weight: 400;\"> has to be a function pointer variable. This function pointer variable will hold the address of a state handler. And the state handlers prototype is something like this, as shown in figure 1, it returns event_status, and it takes these things (<\/span><b>protimer_t *mobj,event_t,*e<\/b><span style=\"font-weight: 400;\">).<\/span><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #008000;\">\/* Main application structure *\/<\/span>\r\n<span style=\"color: #ff99cc;\">typedef struct<\/span> {\r\n    <span style=\"color: #008000;\">uint32_t<\/span> curr_time;\r\n    <span style=\"color: #008000;\">uint32_t<\/span> elapsed_time;\r\n   <span style=\"color: #008000;\"> uint32_t<\/span> pro_time;\r\n    event_status_t (*active_state)(protimer_t&nbsp; *const, event_t const *const);\r\n}protimer_t;<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Function pointer variable<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">That&#8217;s why, how to convert active_state to a function pointer variable? As I explained, it takes the variable name active_state and gives a *; This is a pointer variable. Put that in the parenthesis and give its parameter list and the return value.<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Our state handler prototype function&nbsp; returns event_status_t. That&#8217;s a return type, and it takes input parameter list, which is of this form\u2192 <\/span><b>protimer_t *const mobj, event_t const *const e<\/b><span style=\"font-weight: 400;\">. So, here you need not mention this variable name; this is sufficient. Now, this is a function pointer variable.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Instead of using that function variable, we can use a typedef version, as shown below. That&#8217;s the first change you have to make.&nbsp;<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\"><span style=\"color: #ff99cc;\">typedef<\/span> event_status_t (*protimer_state_t)(protimer_t *const , struct event_t const *const);\r\n\r\n<span style=\"color: #008000;\">\/* Main application structure *\/<\/span>\r\n<span style=\"color: #ff99cc;\">typedef struct<\/span> {\r\n   <span style=\"color: #008000;\">uint32_t<\/span> curr_time;\r\n  <span style=\"color: #008000;\"> uint32_t<\/span> elapsed_time;\r\n   <span style=\"color: #008000;\">uint32_t<\/span> pro_time;\r\n   protimer_state_t active_state;\r\n}protimer_t;<\/pre>\n<p class=\"\" style=\"text-align: center;\"><span style=\"color: #000000;\">Function pointer variable<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And after that, now let&#8217;s go to the protimer_init.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px;\"><span style=\"color: #ff99cc;\">#define<\/span> IDLE           &amp;protimer_state_handler_IDLE\r\n<span style=\"color: #ff99cc;\">#define<\/span> TIME_SET       &amp;protimer_state_handler_TIME_SET\r\n<span style=\"color: #ff99cc;\">#define<\/span> COUNTDOWN      &amp;protimer_state_handler_COUNTDOWN\r\n<span style=\"color: #ff99cc;\">#define<\/span> PAUSE          &amp;protimer_state_handler_PAUSE\r\n<span style=\"color: #ff99cc;\">#define<\/span> STAT           &amp;protimer_state_handler_STAT\r\n\r\n<span style=\"color: #ff99cc;\">void<\/span> protimer_init(protimer_t *mobj){\r\n     event_t ee;\r\n     ee.sig = ENTRY;\r\n     mobj-&gt;active_state = IDLE;\r\n     mobj-&gt;pro_time = 0;\r\n    (*mobj-&gt;active_state)(mobj,&amp;ee); <span style=\"color: #008000;\">\/\/jump to IDLE handler<\/span>\r\n}<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">protimer_init<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">You can use a couple of macros and use this macro to represent the protimer_state_handler address, as shown above. And in the protimer_init, you can use IDLE. So, you directly store the address of the handler. Directly dereference active_state and call that handler initially.&nbsp; (*mobj-&gt; active_state)(mobj, &amp;ee). The first argument is mobj; the second argument is the address of the event. Jump to the IDLE handler to execute the Entry action. That is a second change you have to note.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Now, we have to modify the dispatcher code. By the way, the logic, whatever you had implemented in the loop function, remains as it is. That is no change at all.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\"><span style=\"color: #ff99cc;\">static void<\/span> protimer_event_dispatcher(protimer_t *const mobj,event_t const *const e){\r\n\r\n   event_status_t status;\r\n   protimer_state_t source, target;\r\n\r\n   source = mobj-&gt;active_state;\r\n   status = (*mobj-&gt;active_state)(mobj,e);\r\n\r\n  <span style=\"color: #ff99cc;\"> if<\/span>(status == EVENT_TRANSITION){\r\n     target = mobj-&gt;active_state;\r\n     event_t ee;\r\n     <span style=\"color: #008000;\">\/\/1. run the exit action for the source state<\/span>\r\n     ee.sig = EXIT;\r\n     (*source)(mobj,&amp;ee);\r\n\r\n    <span style=\"color: #008000;\"> \/\/2. run the entry action for the target state<\/span>\r\n     ee.sig = ENTRY;\r\n     (*target)(mobj,&amp;ee);\r\n  }\r\n\r\n}<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Dispatcher code<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">In the dispatcher code, source and target are function pointers. First, mobj-&gt; active_state value is stored in source. And after that, call the current active_state handler with mobj and e. <\/span><b>status=(*mobj-&gt;active_state)(mobj,e)<\/b><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">After that, if there is an EVENT_TRANSITION, then save the target. We have to execute the EXIT action first, copying the source to the active_state. That&#8217;s correct. After that, call the state handler represented by active_state <\/span><b>(*mobj-&gt;active_state)(mobj, e)<\/b><span style=\"font-weight: 400;\">.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And after that, you prepare the ENTRY signal. Now change mobj-&gt;active_state value to target and again call the state handler represented by active_state.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Instead of doing all these things, you can do this, as shown below. So, that&#8217;s the change you have to make.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #ff99cc;\"> if<\/span>(status == EVENT_TRANSITION){\r\n    target = mobj-&gt;active_state;\r\n    event_t ee;\r\n<span style=\"color: #008000;\">    \/\/1. run the exit action for the source state<\/span>\r\n    ee.sig = EXIT;\r\n    (*source)(mobj,&amp;e);\r\n\r\n<span style=\"color: #008000;\">    \/\/2. run the entry action for the target state<\/span>\r\n    ee.sig = ENTRY;\r\n    (*target)(mobj,&amp;e);\r\n  }<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Dispatcher code<\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Please test this on the hardware to see whether everything works or not, or you can even simulate this on the Tinkercad or any simulator software.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000080;\"><b>FastBit Embedded Brain Academy Courses<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px;\"><span style=\"color: #000000;\">C<span style=\"font-weight: 400;\"><span style=\"color: #000000;\">lick here:<\/span>&nbsp;<\/span><\/span><span style=\"color: #3366ff;\"><a style=\"color: #3366ff; text-decoration: underline;\" href=\"http:\/\/fastbitlab.com\/course1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/fastbitlab.com\/course1<\/span><\/a><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Exercise-004 Implementation using state handler approach &nbsp; &nbsp; In this article, let&#8217;s understand the State handler approach. This is almost similar to the previous one. But the important difference is we used the active_state variable in the main application structure to switch between different states. The active_state reveals what exactly is the current active [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[17],"class_list":["post-7483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-finite-state-machine","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>State handler approach - Exercise |FSM<\/title>\n<meta name=\"description\" content=\"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"State handler approach - Exercise |FSM\" \/>\n<meta property=\"og:description\" content=\"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-16T04:46:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-09T07:07:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"FSM Lecture 39: Exercise-004 Implementation using state handler approach\",\"datePublished\":\"2022-02-16T04:46:43+00:00\",\"dateModified\":\"2023-08-09T07:07:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/\"},\"wordCount\":1029,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/figure-1-1.png\",\"keywords\":[\"Finite state Machine(FSM)\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/\",\"name\":\"State handler approach - Exercise |FSM\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/figure-1-1.png\",\"datePublished\":\"2022-02-16T04:46:43+00:00\",\"dateModified\":\"2023-08-09T07:07:36+00:00\",\"description\":\"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/figure-1-1.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/figure-1-1.png\",\"width\":1280,\"height\":720,\"caption\":\"Figure 1. State handler approach\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"FSM Lecture 39: Exercise-004 Implementation using state handler approach\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"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.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"State handler approach - Exercise |FSM","description":"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/","og_locale":"en_US","og_type":"article","og_title":"State handler approach - Exercise |FSM","og_description":"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main","og_url":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2022-02-16T04:46:43+00:00","article_modified_time":"2023-08-09T07:07:36+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png","type":"image\/png"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"FSM Lecture 39: Exercise-004 Implementation using state handler approach","datePublished":"2022-02-16T04:46:43+00:00","dateModified":"2023-08-09T07:07:36+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/"},"wordCount":1029,"commentCount":0,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png","keywords":["Finite state Machine(FSM)"],"articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/","url":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/","name":"State handler approach - Exercise |FSM","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png","datePublished":"2022-02-16T04:46:43+00:00","dateModified":"2023-08-09T07:07:36+00:00","description":"Exercise-004 Implementation using state handler approach. The important difference is we used the active_state variable in the main","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/02\/figure-1-1.png","width":1280,"height":720,"caption":"Figure 1. State handler approach"},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/fsm-lecture-39-exercise-004-implementation-using-state-handler-approach\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"FSM Lecture 39: Exercise-004 Implementation using state handler approach"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"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."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=7483"}],"version-history":[{"count":4,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7483\/revisions"}],"predecessor-version":[{"id":15373,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/7483\/revisions\/15373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/7486"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=7483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=7483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=7483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}