Exercise: Input Matching

Objective

Understand a simple pattern recognition state machine expressed as an Arduino program using several idiomatic C/C++ program structures.

This is a very simple single-input binary pattern recognition system. However, even this state machine could be used to recognize synchronous inputs from a communications channel.

Steps and observations

  1. Set up an Arduino UNO with a 10K pullup resistor on PIN4 and a switch connecting PIN4 to ground, e.g. a simple binary input sensor which you can operate.
  2. Upload the InputMatcher1 program found in this folder.
  3. Open the Serial Console and set to 115200 baud.
  4. Observe the console messages and the onboard LED. Can you deduce an input sequence which will get to State 3 and turn on the LED?
  5. Examine the code; you will be comparing this program structure to the following two versions.
  6. Repeat the test for InputMatcher2 and InputMatcher3.
  7. Choose a new pattern for the system to recognize, and choose the input example you prefer to modify. Rewrite the finite-state machine to recognize the pattern of your choosing.

Comments

This same recognition exercise could be extended to any binary input: optical switches, wall contact switches. It could also be extended to any possible sampling rate, including cycling loop() as fast as possible.

The transition properties could be extended to include other predicates: comparisons on elapsed time to prevent exiting a state before time has elapsed, more elaborate mappings of linear inputs to transition state.

You may now recognize that the switch debouncing code introduced much earlier is a specific case of a finite-state machine implementing time-based hysteresis.

Sub-Folders

  1. InputMatcher1
  2. InputMatcher2
  3. InputMatcher3