Hello!
I think I have the concept of using FSM from LINK incorrectly implemented.
Could someone look over my work and tell me what I am doing wrong? I am trying to use a single button to move from one state machine to the other. So, I start at no LED's turning on, then move into turning the green LED on after pressing the button HIGH and checking my current state. Then to turn on the red LED I check if the button is HIGH and am I currently in the green LED state. And so on.
#include <Arduino.h>
#include <FiniteStateMachine.h>
#include <Streaming.h>
#define buttonPin 32
#define ledWhitePin 31
#define ledRedPin 30
#define ledGreenPin 29
int buttonState = 0;
unsigned long previousGreenMillis = 0;
unsigned long previousRedMillis = 0;
#define ledTimer 1000
State nothing = State(noLED);
State go = State(greenLED);
State hault = State(redLED);
FSM Display = FSM(nothing);
void setup() {
Serial.begin(115200);
pinMode(ledGreenPin, OUTPUT);
pinMode(ledRedPin, OUTPUT);
pinMode(ledWhitePin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && Display.isInState(nothing)){
Display.immediateTransitionTo(go);
}
else if(buttonState == HIGH && Display.isInState(go)){
Display.immediateTransitionTo(hault);
}
else if(buttonState == HIGH && Display.isInState(hault)){
Display.immediateTransitionTo(nothing);
}
else{
Serial << "The End" << endl;
}
}
void noLED(){
digitalWrite(ledGreenPin, LOW);
digitalWrite(ledRedPin, LOW);
}
void greenLED(){
unsigned long currentMillis = 0;
if (currentMillis - previousGreenMillis >= ledTimer){
previousGreenMillis = currentMillis;
digitalWrite(ledGreenPin, HIGH);
}
}
void redLED(){
unsigned long currentMillis = 0;
if (currentMillis - previousRedMillis >= ledTimer){
previousRedMillis = currentMillis;
digitalWrite(ledRedPin, HIGH);
}
}