/*
0.5
framework for traffic light
*/
#include <Bounce2.h>
#include "libFsmA.h"
#include "misc.h"
#include "z_macros.h"
#include "stFunctions.h"
#include "trafficLight.h"
// all buttons
const uint8_t btnPins[] = { 2, 3, 4, 5 };
Bounce *buttons = new Bounce[NUMELEMENTS(btnPins)];
// ledPin
const uint8_t ledPin = LED_BUILTIN;
// print output with millis()
const bool enableMillis = true;
// debug fsm
const bool debugFsm = true;
enum StateNames : uint16_t
{
stBB, // blink tl and pl
stRR, // tl red, pl red
stGR, // tl green, pl red
stX, // delay cycle
stOR, // tl orange, pl red
stRR1, // tl red, pl red
stRG, // tl red, pl green
stRB, // tl red, pl blink green
stRR2, // tl red, pl red
};
Timer blinkTimer; // timer for blink
Timer stateTimer; // timer for states (how long do we stay in a state)
// create instances of the LEDs for the traffic light
const uint8_t tlRedPin = 6;
Led tlRed = Led(tlRedPin, LOW);
const uint8_t tlGreenPin = 8;
Led tlGreen = Led(tlGreenPin, LOW);
const uint8_t tlYellowPin = 7;
Led tlYellow = Led(tlYellowPin, LOW);
TrafficLightThreeLeds trafficLight(tlGreen, tlRed, tlYellow);
// transitions from state BB
Transition stBB_Transitions[] = {
{ stRR, stBB_stRR_Timer },
};
State states[] = {
{ stBB, "tl B / pl B", stBB_onEnter, stBB_onState, stBB_onLeave, stBB_Transitions, 1 },
{ stRR, "tl R / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
{ stGR, "tl G / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
{ stX, "tl G / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
{ stOR, "tl O / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
{ stRR1, "tl R / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
{ stRG, "tl R / pl G", nullptr, nullptr, nullptr, nullptr, 0 },
{ stRB, "tl R / pl B", nullptr, nullptr, nullptr, nullptr, 0 },
{ stRR2, "tl R / pl R", nullptr, nullptr, nullptr, nullptr, 0 },
};
// the fsm
Fsm fsm = { states, NUMELEMENTS(states), stBB };
void setup()
{
Serial.begin(115200);
Serial.println(F("FSM demo"));
// configure buttons
for (uint8_t cnt = 0; cnt < NUMELEMENTS(btnPins); cnt++)
{
buttons[cnt].attach(btnPins[cnt], INPUT_PULLUP); // setup the bounce instance for the current button
buttons[cnt].interval(25); // interval in ms
}
tlYellow.begin();
// validate fsm
if (fsm.validate() == false)
{
Serial.println(F("Fix your code"));
for (;;) {}
}
else
{
Serial.println("Succesfully validated");
}
// enable/disable printing of millis() in the fsm
fsm.enableMillis = enableMillis;
// enable/disable debugging of fsm
fsm.debug = debugFsm;
// print fsm info
fsm.print();
}
void loop()
{
fsm.run();
return;
}