InputMatcher3 Arduino Sketch

This sketch is used by Exercise: Input Pattern Matching.

Full Source Code

The full code is all in one file InputMatcher3.ino.

 1// InputMatcher3.ino : Arduino program to demonstrate a simple finite state machine program structure.
 2
 3// Copyright (c) 2014, Garth Zeglin.  All rights reserved. Licensed under the terms
 4// of the BSD 3-clause license as included in LICENSE.
 5
 6// The baud rate is the number of bits per second transmitted over the serial port.
 7const long BAUD_RATE = 115200;
 8
 9// The state machine uses a single switch input.
10const int INPUT_PIN = 4;
11
12// Define the name for each state, and an index variable to represent the
13// current state.
14enum state_name_t { START = 0, STATE1, STATE2, STATE3 } state_index;
15
16/****************************************************************/
17/**** Standard entry points for Arduino system ******************/
18/****************************************************************/
19
20// Standard Arduino initialization function to configure the system.
21
22void setup()
23{
24  // initialize the Serial port
25  Serial.begin( BAUD_RATE );
26
27  // configure our trivial inputs
28  pinMode( INPUT_PIN, INPUT );
29  pinMode( LED_BUILTIN, OUTPUT );
30
31  // initialize the state machine
32  state_index = START;
33}
34
35/****************************************************************/
36/****************************************************************/
37// Define a table of state transitions and state outputs.
38
39struct state_t {
40  const char *name;
41  int led_output;
42  enum state_name_t high_transition;
43  enum state_name_t low_transition;
44
45};
46
47struct state_t states[] = {
48  { "START",  LOW,  STATE1, START  },
49  { "STATE1", LOW,  START,  STATE2 },
50  { "STATE2", LOW,  STATE3, STATE1 },
51  { "STATE3", HIGH, STATE3, START  }
52  };
53
54/****************************************************************/
55// This demonstrates a table-based approach for representing a finite state
56// machine as an Arduino program.  The current state is represented as the value
57// of the state_index variable.  All transitions are specified in the
58// table. Note that this structure is highly extensible and allows for easily
59// evaluating other subroutines or state machines concurrently with this one.
60// It is effectively an extremely simple language interpreter.
61
62void loop()
63{
64  // select the state table entry corresponding to the state
65  struct state_t& current = states[state_index];
66
67  // generate the appropriate outputs for the state
68  Serial.print( "Entering state: " );
69  Serial.println( current.name );
70  digitalWrite( LED_BUILTIN, current.led_output );
71
72  // process inputs
73  delay(1000);
74  Serial.println("Sampling input.");
75  int input = digitalRead( INPUT_PIN );
76
77  // choose the successor state based on the table entry
78  if (input) state_index = current.high_transition;
79  else       state_index = current.low_transition;
80}
81
82/****************************************************************/