So, I've been playing with the Simple State Machine Library, for the purpose of automating a home brewery. State machines seem like the way to go given the finite, sequential states for brewing beer. I've started small, with a sketch just focusing on the state machine itself, but I'm having a problem - serial reads stop working! I'm using serial.reads to fake a button press on the computer - eventually I want to run a program on the PC that talks to the arduino for uploading recipes, logging, and managing state transitions.
Here's my sketch thus far:
#include <SM.h> //Simple State Machine Library
//State Machine names go here MachineName(headstate,bodystate)
SM Hold0(H0h,H0b);
SM Strike(S1);
SM Hold1(H1b);
SM Mash(M1);
//SM Hold2(H2) ;
//SM Boil(B1) ;
/**** global variables *****/
double Setpoint;
//String str;
void setup(){
Serial.begin(9600);
}
void loop(){
EXEC(Hold0);
}
State H0h(){
Serial.println("hello");
}
State H0b(){
while (Serial.available()>0) {
String str = Serial.readStringUntil('\n');
if(str == "next"){
Serial.println("going to Strike");
EXEC(Strike);
Hold0.Finish();
}
}
}
State S1(){
Serial.println("S1");
Setpoint = 155;
int temp = 150; //manual faking of temperature
while (temp < Setpoint){
temp++;
Serial.print(Strike.Statetime());
Serial.print(" setpoint= ");
Serial.print(Setpoint);
Serial.print(" temp= ");
Serial.println(temp);
}
Serial.println("going to Hold1");
Strike.Finish();
EXEC(Hold1);
}
/*State H1h(){
Serial.println("H1");
//Hold1.Set(H1b);
}*/
State H1b(){
Serial.println("h1b");
while (Serial.available()>0) {
String foo = Serial.readStringUntil('\n');
if(foo == "next"){
Serial.println("going to Mash");
EXEC(Mash);
Hold1.Finish();
}
else if(foo !="next"){
Serial.println("HUH?");
}
}
}
State M1(){
Serial.println("Mash");
}
The problem is that the sketch gets stuck at the entry to Hold1 - it won't accept any more input. I'm looking for "next" to come across the serial, but when I enter that in serial monitor, I get nothing. Here's the output that I do get:
hello (here I enter "next")
going to Strike
S1
0 setpoint= 155.00 temp= 151
1 setpoint= 155.00 temp= 152
16 setpoint= 155.00 temp= 153
49 setpoint= 155.00 temp= 154
81 setpoint= 155.00 temp= 155
going to Hold1 (here I enter "next" but nothing happens)
Any help would be appreciated. I'm new to the idea of state machines, but it seems like everything is flowing properly.... until it doesn't
thanks,
JR