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 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 | // InputMatcher3.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.
#define BAUD_RATE 115200
#define INPUT_PIN 4
// Some version of the Arduino IDE don't correctly define this symbol for an
// Arduino Uno.
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
// 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;
}
/****************************************************************/
/****************************************************************/
// Define a table of state transitions and state outputs.
struct state_t {
const char *name;
int led_output;
enum state_name_t high_transition;
enum state_name_t low_transition;
};
struct state_t states[] = {
{ "START", LOW, STATE1, START },
{ "STATE1", LOW, START, STATE2 },
{ "STATE2", LOW, STATE3, STATE1 },
{ "STATE3", HIGH, STATE3, START }
};
/****************************************************************/
// This demonstrates a table-based approach for representing a finite state
// machine as an Arduino program. The current state is represented as the value
// of the state_index variable. All transitions are specified in the
// table. Note that this structure is highly extensible and allows for easily
// evaluating other subroutines or state machines concurrently with this one.
// It is effectively an extremely simple language interpreter.
void loop()
{
// select the state table entry corresponding to the state
struct state_t& current = states[state_index];
// generate the appropriate outputs for the state
Serial.print( "Entering state: " );
Serial.println( current.name );
digitalWrite( LED_BUILTIN, current.led_output );
// process inputs
delay(1000);
Serial.println("Sampling input.");
int input = digitalRead( INPUT_PIN );
// choose the successor state based on the table entry
if (input) state_index = current.high_transition;
else state_index = current.low_transition;
}
/****************************************************************/
|