loading

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.

Example:

Here is the 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.

Step 1: 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:

switch(state) {
   case INITIAL:
     // process INITIAL state  
   break;
   case STATE1:
     // process STATE1 state       
   break;
   case CLEAR:
     clearAll();
     state = INITIAL;
   break;
   default:
   break;
}

Step 2: 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
    • 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#)
      • 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

Step 3: Create a State Diagram

  • Create a state diagram.
    • Start with a table or spreadsheet with all the states entered

      See state diagram template.


  • For each state determine what keys are valid and the next state.
    • INITIAL is the first digit of the value so valid keys are 0-9
    • VALUE1 can be the second digit of the value or the at sign (@)
    • Continue until you have completed all the valid key and next state.

      See completed state diagram.

Step 4: Create the Program

  • Next create the program
    • Create #defines for each state
    • Create a switch statement with each state as a case.

      // States
      #define INITIAL       1
      #define VALUE1        2
      #define VALUE2        3
      #define ATSIGN        4
      #define HOUR1         5
      #define HOUR2         6
      #define COLON         7
      #define MIN1          8
      #define MIN2          9
      #define EXECUTE       10

      switch(state) { case INITIAL: break; case VALUE1: break; case VALUE2: break; case ATSIGN: break; case HOUR1: break; case HOUR2: break; case COLON: break; case MIN1: break; case MIN2: break; case EXECUTE: break; default: break; }

Step 5: Create Some Helper Functions

  • Next create the some helper functions
    • Create functions to validate input keys

      // Validate keys: @ (at sign)
      // return true if valid, else false int validAtSign(int key) { int valid=0; switch(key) { case '@': valid=1; break; } return valid; } // Validate keys: : (colon) // return true if valid, else false int validColon(int key) { int valid=0; switch(key) { case ':': valid=1; break; } return valid; } // Validate keys: # (pound sign) // return true if valid, else false int validPoundSign(int key) { int valid=0; switch(key) { case '#': valid=1; break; } return valid; } // Validate keys: 0-9 // return true if valid, else false int validKeys0to9(int key) { int valid=0; switch(key) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': valid=1; break; } return valid; } // Validate keys: 0-5 // return true if valid, else false int validKeys0to5(int key) { int valid=0; switch(key) { case '0': case '1': case '2': case '3': case '4': case '5': valid=1; break; } return valid; }

Step 6: Put It All Together...

  • Add the helper functions and minimal logic to the switch statements (states)
    • Go state by state following the state diagram rules above

      switch(state) {
      	case INITIAL:
              // X
              if(validKeys0to9(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = VALUE1;
              } else {
                invalidFormat();
              }
            break;
            case VALUE1:
              // XX
              if(validKeys0to9(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = VALUE2;
              } else if (validAtSign(key)) {
                // X@
                pos = displayKey(key, pos);
                state = ATSIGN;          
              } else {
                invalidFormat();
              }
            break;
            case VALUE2:
              // XX@
              if(validAtSign(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = ATSIGN;
              } else {
                invalidFormat();
              }
            break;
            case ATSIGN:
              // X@H, HH@H
              if(validKeys0to9(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = HOUR1;
              } else {
                invalidFormat();
              }      
            break;
            case HOUR1:
               // X@HH, XX@HH
              if(validKeys0to9(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = HOUR2;
              } else if (validColon(key)) {
                // X@HH:, XX@H:
                pos = displayKey(key, pos);
                state = COLON;          
              } else {
                invalidFormat();
              }
            break;
            case HOUR2:
               // X@HH:, XX@HH:
              if (validColon(key)) {
                // X@HH:, XX@H:
                pos = displayKey(key, pos);
                state = COLON;          
              } else {
                invalidFormat();
              }
            break;
            case COLON:     
              // X@HH:M, XX@HH:M
              if(validKeys0to5(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = MIN1;
              } else {
                invalidFormat();
              }
            break;
            case MIN1:
              // X@HH:MM, XX@HH:MM
              if(validKeys0to9(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = MIN2;
              } else {
                invalidFormat();
              }
            break;
            case MIN2:
              // X@HH:MM#, XX@HH:MM#
              if(validPoundSign(key)) {
                pos = displayKey(key, pos);
                storeKey(key);
                state = EXECUTE;
              } else {
                invalidFormat();
              }
            break;
            case CLEAR:
              clearAll();
            break;
            default:
            break;      
          } // switch

Step 7: Conclusion

  • Test the results
    • Add any additional limit/range checking on values.
    • For example: valid time format is 00:00 to 24:00
      • See example code. It utilizes the serial terminate and some helper functions for debugging.

It's as simple as that!

Source Code:

The complete working example is available here.

Working Demo:

See video for working demo.

Future Enhancements / Ideas:
The working example is meant as a teaching tool and doesn't have full validation on the values.This example does include code to use the serial terminal to help you debug.

Questions?

If you have any questions please feel free to ask.

I hope this article is helpful and useful to someone interested in validating keypad input using state machine programming. Feel free to use this code and example to validate you next project using a keypad for input command.

Thanks, Tony

<p>i no understand english anythinkg thats but how i make lcd main menu &quot;select milliliter&quot; value 1-100 exept 12,4mL how use full mL 12 and little 0,4ml ?</p><p>then lcd show 12,4mL and calculate number down (sametime motor run) and then have 0 lcd say &quot;ready end&quot; , and program go back main menu.</p><p>program i think make calculate and set motor time /milliliter how long motor run and if number have about 5milliliter motor run about 5 millisecond . need measure what time. but im newbie and no understand how make lcd, 4x4 keybad runnin number down and menu ???? i have i2c lcd 16x2 character and 4x4 keybad and motorshield who running motor, please help me how i make thats. my father need he hobby thats timer.if i can make thats later need 5 different motor same. HELP please.</p>

About This Instructable

2,976views

17favorites

License:

Bio: Passionate about Technology, online payments, online security, Lighting Industry, Q/A automation and Arduino based projects.
More by onewithhammer:Web Driver IO Tutorial Using A Live Web Site And Working Examples Interests  Keypad Input Validation Using State Machine Programming 
Add instructable to: