I am having problems with a state machine and cant seen to figure out why.
The state machine is activated by a set of parameters from the "D1_StateFunction()". When this function calls the state machine is seems to get stuck in the last position.
The State Machine setup
enum D1_SaveSettings {D1_SAVEIDLE, D1_SAVEDATA, D1_EVALDATA};
D1_SaveSettings D1SaveState = D1_SAVEIDLE;
Function which calls the state machine
void D1_StateFunction()
{
int x = EEPROM.get(0, D1_FunctionInt);
Serial.print("Function :");//////////////////////////////
Serial.println(x);//////////////////////////
if (x == 0) //OFF/BLEED
{
D1_MainFunctionFlag = false;
if (D1_CalibrateActive)
{
digitalWrite(DOS1, HIGH);
}
else
{
digitalWrite(DOS1, LOW);
}
}
else if (x == 1) //CALIBRATE
{
D1_MainFunctionFlag = false;
if (D1_CalibrateActive)
{
D1_CalibrateFlag = true;
D1_CalibrateFunction();
}
else if (D1_SaveActive)
{
D1_SaveFlag = true; //FLAG TO ACTIVATE THE SAVE SETTINGS ONNCE
D1_SaveSettings();
}
}
else if (x == 2) //HOSE CHANGE
{
D1_MainFunctionFlag = false;
if (D1_SaveActive)
{
D1_Hose = 0;
EEPROM.put(30, D1_Hose);
EEPROM.commit();
D1_HoseFlag = false;
D1_HoseFlagAlert = true;
}
}
else if (x == 3 || x == 4 || x == 5 || x == 6 || x == 7 || x == 8 || x == 9)
{
D1_MainFunctionFlag = true;
D1_MainFunction();
}
else if (D1_BoostActive)
{
D1_MainFunctionFlag = false;
D1_BoostFlag = true;
D1_BoostFunction();
}
else
{
}
}
The state machine
void D1_SaveSettings()
{
if (D1_SaveFlag)
{
switch (D1SaveState)
{
case D1_SAVEIDLE:
{
Serial.println("idle");//////////////////////////////
D1SaveState = D1_SAVEDATA;
break;
}
case D1_SAVEDATA:
{
EEPROM.put(5, D1_DailyDosageFloat);
EEPROM.put(25, D1_RemainVolume);
EEPROM.put(20, D1_DoseTime);
EEPROM.put(10, D1_SolutionInputInt);
EEPROM.put(15, D1_BoostDosageFloat);
EEPROM.commit();
Serial.println("save");//////////////////////////////
D1SaveState = D1_EVALDATA;
break;
}
case D1_EVALDATA:
{
D1_RemainVolFlag = true;
Serial.println("evaluate");//////////////////////////////
D1SaveState = D1_SAVEIDLE;
break;
}
}
D1_SaveFlag = false;
D1_RemainVolumeCheck();
}
}
The serial output
evaluate
1665334308
Function :1
idle
Function :1
save
Function :1
evaluate
1665334311
Function :1
idle
Function :1
save
Function :1
evaluate
1665334314
Function :1
idle
Function :1
save
Function :1
evaluate
1665334317
Function :1
idle
Function :1
save
Function :1
evaluate
1665334320
Function :1
idle
Function :1
save
Function :1
evaluate
1665334323
Can anybody shed some light as to where i am going wrong. What expect to happen is once the state machine has completed serial print will be just a list of "Function : 1