Summary of Keypad Input Validation using State Machine Programming
This article explains how to validate 16-button keypad commands using state machine logic. It details a protocol (XX@HH:MM#) where specific keys map to characters like '@', ':', and '#'. The solution involves breaking the command into distinct states (e.g., VALUE1, HOUR1), defining valid ranges for time and values, and implementing transitions via a switch statement to ensure accurate input processing before execution.
Parts used in Keypad Input Validation Project:
- 16-button keypad
- 4 x 4 keypad matrix
- Switch statement
- State machine logic
- Command protocol (XX@HH:MM#)
- Alpha-Numeric key mappings
- Value range definitions
- State diagram
- Spreadsheet or table
The Problem:
You have a project that accepts commands using a 16 button keypad and want to perform validation on the commands as each character is typed. But how?
Here is the sample or typical protocol (commands) using only a 4 x 4 – 16 button keypad:
XX@HH:MM#
Where:
XX is a value from 1-99
HH:MM is a time format (24 hr clock or military time)
Alpha-Numeric Key Mappings:
A = @ (at sign)
B = NOT USED
D = NOT USED
C = Clear
* = : (colon)
# = Execute (accept or enter or execute) the command
The Solution:
Use state machine logic / programming to solve the problem.
Introduction to State Machine Logic / Programming
If you aren’t familiar with or haven’t used state machine logic in programming, it is the easiest way to to break complex problems into manageable states and state transitions especially for handling serial input.
One of the easiest ways to implement a state machine is to use a switch statement. In my opinion it is the only way to implement serial input commands.
Example of a state machine using a switch statement:
Let’s now apply this logic to your project.
Here is a step by step approach to solve the problem:
- Break the commands into states.
- The easiest way is to consider each character in the command as a state.
- Given the command: XX@HH:MM#
- Here are suggested state names:
- VALUE1 – First digit of value
- VALUE2 – Second digit of value
- ATSIGN – At sign (@)
- HOUR1 – First digit of hour
- HOUR2 – Second digit of hour
- COLON – Colon (:)
- MIN1 – First digit of minute
- MIN2 – Second digit of minute
- EXECUTE – Pound sign (#)
- Then create an additional state called INITIAL
- Here are suggested state names:
- Create a list or table of all the combinations within a command or commands.
- Given the command: XX@HH:MM#
- All combinations of the command:
- XX@HH:MM# (45@12:45#)
- XX@HH:MM# (45@1:26#)
- X@HH:MM# (9@12:45#)
- X@H:MM# (9@1:26#)
- All combinations of the command:
- Given the command: XX@HH:MM#
- Determine all ranges for values
- XX has a range 1 to 99 but 01-09 is also valid
- HH has a range of 0-24 but 00-09 is also valid
- MM has a range of 00-59
- Create a state diagram.
- Start with a table or spreadsheet with all the states entered:
For more detail: Keypad Input Validation using State Machine Programming
-
How can I solve the problem of validating commands as each character is typed?
Use state machine logic or programming to break complex problems into manageable states and transitions. -
What is the easiest way to implement a state machine for serial input commands?
The easiest way is to use a switch statement. -
Which key on the keypad executes the command?
The pound sign (#) key is used to execute, accept, or enter the command. -
What does the colon symbol represent in the alpha-numeric key mapping?
The asterisk (*) key maps to the colon (:). -
What are the suggested state names for the command XX@HH:MM#?
Suggested states include VALUE1, VALUE2, ATSIGN, HOUR1, HOUR2, COLON, MIN1, MIN2, EXECUTE, and INITIAL. -
What is the valid range for the hour value in the protocol?
The hour has a range of 0 to 24, and formats like 00-09 are also valid. -
Can the first digit of the value be zero in the command protocol?
Yes, 01-09 is considered valid for the value range. -
What is the recommended tool to create a state diagram?
Start with a table or spreadsheet listing all the states entered.

