#include "StateMachine.h"
#include "Timer.h"
#include "Sensor.h"
#include "Drive.h"
// Connections
#define LEFT_SERVO_PIN 10
#define RIGHT_SERVO_PIN 11
// Define state machine actions
const int NUL = bit(0);
const int PST = bit(1);
const int STS = bit(2);
const int LTS = bit(3);
// Define state machine events
const int TRU = bit(0);
const int STA = bit(1);
const int LTA = bit(2);
// Create objects
StateMachine mm;
Timer shortTimer(100);
Timer longTimer(500);
Drive myDrive;
void setup() {
Serial.begin(9600);
myDrive.attach(LEFT_SERVO_PIN, RIGHT_SERVO_PIN);
myDrive.forward(0.5);
// Construct state machine
mm.addState(0, TRU, 1, STS + PST, 0, NUL);
mm.addState(1, STA, 2, LTS + PST, 1, NUL);
mm.addState(2, LTA, 1, STS + PST, 2, NUL);
}
void loop() {
// Log events
mm.logEvent(TRU, true);
mm.logEvent(STA, shortTimer.isAlarming());
mm.logEvent(LTA, longTimer.isAlarming());
// Evaluate state machine
mm.evaluate();
// Perform requested actions
if(mm.requests(PST))
{
Serial.print("New state: ");
Serial.println(mm.getState());
}
if(mm.requests(STS)) shortTimer.reset();
if(mm.requests(LTS)) longTimer.reset();
}