- Version: 1.0.2
- Release Date: 2020-09-03
- Download the Latest release from gitHub.
- Unzip and modify the Folder name to "StateMachine".
- Paste the modified folder on your library folder (On your
Librariesfolder inside Sketchbooks or Arduino software). - Restart the Arduino Software
#include <Arduino.h>
#include <StateMachine.h>
#define WITH_DEBUG
#include <DebugUtil.h>
// Initialization
...
class TrafficLight : public StateMachine {
public:
using StateMachine::StateMachine;
State STOP() {
if (isStateUpdated()) {
RED_ON();
YELLOW_OFF();
GREEN_OFF();
DEBUG("STOP");
return;
}
if (isTimeout(10000)) {
setState((PState) &TrafficLight::WAIT);
return;
}
};
State WAIT() {
if (isStateUpdated()) {
RED_ON();
YELLOW_ON();
GREEN_OFF();
DEBUG("WAIT");
return;
}
if (isTimeout(2000)) {
setState((PState) &TrafficLight::DRIVE);
return;
}
};
...
};
TrafficLight trafficLight((PState) &TrafficLight::STOP);
void loop() {
trafficLight.loop();
// Other code
...
}
Results:
0: TrafficLight.ino:25 STOP - STOP
10001: TrafficLight.ino:38 WAIT - WAIT
12002: TrafficLight.ino:51 DRIVE - DRIVE
22003: TrafficLight.ino:64 SLOW - SLOW
24004: TrafficLight.ino:25 STOP - STOP
34006: TrafficLight.ino:38 WAIT - WAIT
36007: TrafficLight.ino:51 DRIVE - DRIVE
...
Included on example folder, available on Arduino IDE.
- 1.0.1 (2016-07-17): Added PUSH/POP state methods.
- 1.0.0 (2016-01-28): Initial version.
- Wikipedia on Finite State Machines
- RTTTL Player library
- Push Button library