11.17. InputMatcher2 Arduino Sketch

This sketch is used by Exercise: Input Pattern Matching.

11.17.1. Full Source Code

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
// InputMatcher2.ino : Arduino program to demonstrate a simple finite state machine program structure.

// Copyright (c) 2014, Garth Zeglin.  All rights reserved. Licensed under the terms
// of the BSD 3-clause license as included in LICENSE.

// The baud rate is the number of bits per second transmitted over the serial port.
const long BAUD_RATE = 115200;

// The state machine uses a single switch input.
const int INPUT_PIN = 4;

// Define the name for each state, and an index variable to represent the
// current state.
enum state_name_t { START = 0, STATE1, STATE2, STATE3 } state_index;

/****************************************************************/
/**** Standard entry points for Arduino system ******************/
/****************************************************************/

// Standard Arduino initialization function to configure the system.

void setup()
{
  // initialize the Serial port
  Serial.begin( BAUD_RATE );

  // configure our trivial inputs
  pinMode( INPUT_PIN, INPUT );
  pinMode( LED_BUILTIN, OUTPUT );

  // initialize the state machine
  state_index = START;
}

/****************************************************************/
// Standard Arduino polling function.

// This demonstrates a conventional switch-case structure for representing a
// finite state machine as an Arduino program.  The current state is represented
// as the value of the state_index variable.  Note that this structure allows
// for easily evaluating other subroutines or state machines concurrently with
// this one, as the execution passes through the loop() function on each
// iteration.

void loop()
{
  // select the code block corresponding to the state to generate the appropriate outputs for the state
  switch( state_index) {

  case START:
    Serial.println("Entering start state.");
    digitalWrite(LED_BUILTIN, LOW);
    break;

  case STATE1:
    Serial.println("Entering state 1.");    
    digitalWrite(LED_BUILTIN, LOW);
    break;

  case STATE2:
    Serial.println("Entering state 2.");    
    digitalWrite(LED_BUILTIN, LOW);
    break;

  case STATE3:
    Serial.println("Entering state 3.");
    digitalWrite(LED_BUILTIN, HIGH);
    break;
  }

  delay(1000);
  Serial.println("Sampling input.");
  int input = digitalRead( INPUT_PIN );

  // select the code block corresponding to the current state to evaluate the next input
  switch( state_index) {

  case START:
    if (input) state_index = STATE1;
    else state_index = START;
    break;

  case STATE1:
    if (input) state_index = START;
    else state_index = STATE2;
    break;

  case STATE2:
    if (input) state_index = STATE3;
    else state_index = STATE1;
    break;

  case STATE3:
    if (input) state_index = STATE3;
    else state_index = START;
    break;
  }
}    

/****************************************************************/