Let's Learn How To Use Finite State Machine With Arduino
Let's Learn How To Use Finite State Machine With Arduino
with Arduino © Apache-2.0
The finite state machines (FSMs) are significant for understanding the
decision making logic as well as control the digital systems.
automation
traffic light
COMPONENTS AND SUPPLIES
LED (generic)
You can use any led you want, preferably of a different color
× 3
× 3
× 1
Arduino UNO
Or any other development board you have, it doesn't matter which
one it is
× 1
In other words, a FSM can be the right choice on many occasions with our development
board for a number of very good reasons:
As automation, we will make a small variation on the very classic green / orange / red traffic
light algorithm. We will implement the code for a pedestrian traffic light: normally the green
light is on; when the pedestrian presses the call button he waits, after a short waiting time the
yellow light turns on and finally the red light
This is the graphic representation of this simple automation model:
PedestrainLight SFC model
In order to get familiarity with SFC and library, try to add an additional and helpfully
functionality!
Would be nice for pedestrians having a feedback led that will light up on the call button
pressed in order to identify that input was acquired from the controller.
CODE
PedestrianLight.inoArduino
The sketch is quite simple as you can see, it is all "declarative".
The real job you you have to done carefully is define the automation model on paper, and then easily "translate" to
states, transitions and actions.
#include <YA_FSM.h> // https://github.com/cotestatnt/YA_FSM
// State Alias
enum State {RED, GREEN, YELLOW, CALL};
// Output variables
bool redLed = false;
bool greenLed = false;
bool yellowLed = false;
void setup() {
// Setup Input/Output
pinMode(BTN_CALL, INPUT_PULLUP);
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
Serial.begin(115200);
while(!Serial) {} // Needed for native USB port only
Serial.println(F("Starting the Finite State Machine...\n"));
setupStateMachine();
}
void loop() {
// Read inputs
callButton = (digitalRead(BTN_CALL) == LOW);
PedestrianLight
cotestatnt / YA_FSM
19 5
Simple state machine. Implements a "Petri net" (GRAFCET/SFC) inspired state machine with states,
transitions and actions — Read More
Latest commit to the master branch on 7-28-2022
Download as zip
SCHEMATICS
Schematic
DOWNLOAD