Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:10:00+00:00 2026-06-08T03:10:00+00:00

I have implemented a simple state machine for an embedded system using C’s switch

  • 0

I have implemented a simple state machine for an embedded system using C’s switch statement. I know it would be better if I used function pointers with a look up table but I’m saving that as a next step.

My state machine has the following states:

  1. Startup (the initial state)
  2. Startup Error.
  3. Idle (the system only checks for input in this state. It does not update the display or anything else. It’s just ‘idle’).
  4. Check (this is the actual application)
  5. Program
  6. Copy to Internal Memory

When the system starts up, it enter the startup state which configures the ports, initializes the display and does a hand-shake with the ICs connected on the SPI bus to ensure everything is A-OK. If so, the system enters the Idle state. If not, it enters the Startup Error state, displays an error on the LCD, flags a variable and then enters the Idle state.

In the Idle state, the program polls 3 pins on the microcontroller to check if one of the 3 buttons (Check, Program, Copy to Mem.) is pressed. Depending on which button is pressed, it enters the appropriate state, executes some code, updates the LCD and then returns back to the Idle state. NOTE: The system does NOT care if a button is pressed if there was a hardware fault in the system. The Startup Error state flags a variable called hardware_fault which, if set, ensures that the Idle state does not bother polling any of the input buttons.

This is the first time I’m implementing a state machine and I was just unsure if this was a good design. I haven’t really seen any examples of FSMs where they poll for input in an Idle state. Instead, it seems, most examples are rather sequential in nature (like a counter, for instance). So, my question is, is my design reasonable? It DOES work but as everyone here knows, there is bad design and then there is good design.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:10:02+00:00Added an answer on June 8, 2026 at 3:10 am

    You should refactor your code away from a switch statement and use a transition table as you mention. Code around a switch doesn’t scale, is messy and quickly becomes difficult to read and update.

    To answer your question, I would say that most state machines are actually like yours: they react to events (in your case, the poll). If they are purely sequential, without events at all, they are for specialized usages, like implementing a regex…

    As a note, the Idle state is equivalent to the runtime core of a state machine library: it intercepts events and posts them to the state machine. With a state machine library, it would be “hidden” from the client code, while a bare bones implementation like yours has to do event polling explicitly.

    And now a design critique: one should avoid global variables in general, and especially so in a state machine. State Idle should not know about a hardware_fault global variable. With state machines, one should strive to “embed” global variables in the state machine itself, by adding states (when it makes sense!). A very good introduction to (hierarchical) state machines and explanation of the rationale is here.

    Using the UML notation (see here for a tutorial), your state machine is:
    state machine v1

    An easy refactoring to remove the hardware_fault dependency all together is:
    state machine v2

    That is, we just stay forever in the StartupError state, without transitioning to Idle.

    The ε (epsilon) represents an empty transition, that is, a transition that is taken as soon as the activity associated with the source node is over, without any event. This is what you mean by “sequential” state machines.

    I am also including the sources to the first diagram (the second one is very similar), generated with the very easy and powerful PlantUML.

    @startuml
    
    skinparam shadowing false
    title Embedded v1
    
    state Startup
    state StartupError
    state Idle
    state Program
    state Check
    state Copy
    
    Startup : entry/ init HW
    StartupError: entry/\n  display error\n  hw_fault = true
    Idle : do/ if not hw_fault: poll
    Program : do/ program
    Check : do/ check
    Copy : do/ copy
    
    [*] -> Startup
    
    Startup -> StartupError : ε [error]
    Startup --> Idle : ε [not error]
    
    StartupError --> Idle : ε
    
    Idle --> Program : program_btn
    Idle --> Check : check_btn
    Idle --> Copy : copy_btn
    
    Program --> Idle : ε
    
    Check --> Idle : ε
    
    Copy --> Idle : ε
    
    @enduml
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a simple C class I have implemented, using function pointers in a
I am creating a simple messaging system in WCF and have implemented a minimal
I implemented a simple state machine in Python: import time def a(): print a()
I have created a simple order manager wf service (state machine) in WF4. Order
I have implemented a simple entity ejb with a @version annotation. I expect that
I have implemented a simple linux shell in c. Now, I am adding some
I have implemented a simple RSS reader in Java. I hit a URL and
I have implemented my own simple version of a navigation window, mainly because navigation
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have a simple rails search implemented in my app. At the time I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.