InputMatcher2 Arduino Sketch¶
This sketch is used by Exercise: Input Pattern Matching.
Full Source Code¶
The full code is all in one file InputMatcher2.ino.
1// InputMatcher2.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// Standard Arduino polling function.
37
38// This demonstrates a conventional switch-case structure for representing a
39// finite state machine as an Arduino program. The current state is represented
40// as the value of the state_index variable. Note that this structure allows
41// for easily evaluating other subroutines or state machines concurrently with
42// this one, as the execution passes through the loop() function on each
43// iteration.
44
45void loop()
46{
47 // select the code block corresponding to the state to generate the appropriate outputs for the state
48 switch( state_index) {
49
50 case START:
51 Serial.println("Entering start state.");
52 digitalWrite(LED_BUILTIN, LOW);
53 break;
54
55 case STATE1:
56 Serial.println("Entering state 1.");
57 digitalWrite(LED_BUILTIN, LOW);
58 break;
59
60 case STATE2:
61 Serial.println("Entering state 2.");
62 digitalWrite(LED_BUILTIN, LOW);
63 break;
64
65 case STATE3:
66 Serial.println("Entering state 3.");
67 digitalWrite(LED_BUILTIN, HIGH);
68 break;
69 }
70
71 delay(1000);
72 Serial.println("Sampling input.");
73 int input = digitalRead( INPUT_PIN );
74
75 // select the code block corresponding to the current state to evaluate the next input
76 switch( state_index) {
77
78 case START:
79 if (input) state_index = STATE1;
80 else state_index = START;
81 break;
82
83 case STATE1:
84 if (input) state_index = START;
85 else state_index = STATE2;
86 break;
87
88 case STATE2:
89 if (input) state_index = STATE3;
90 else state_index = STATE1;
91 break;
92
93 case STATE3:
94 if (input) state_index = STATE3;
95 else state_index = START;
96 break;
97 }
98}
99
100/****************************************************************/