State Machine stuck

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

Please post a complete sketch that illustrates the problem

where is this output coming from? what is it?

looks like you're repeatedly invoking you SaveSetting() because x==1, calibrateActive is false and saveActive is true, and i don't see where those boolean variables change state

saveSetting() sequences thru each of the states: idle, saveData, evalData, idle, ...

not clear what your input is and what invokes stateFunction()?

EEPROM.get(address, variable) does NOT return the value of the variable. If you want to set 'x' to the retrieved value:

  EEPROM.get(0, D1_FunctionInt);
  int x = D1_FunctionInt;

For a finite state machine, you have a large number of flags controlling its operation. Maybe you need more states and less flags.
Anyway, writing in a continuous loop to an EEPROM may damage it because it tolerates, during its life time, only a limited number of write operations.

Obviously your D1_FunctionInt returns always the same value. Are you missing () to call a function?

Your state machine implementation is quite strange.

I take on board all the comments made above but after futher debugging i think i have found the problem. The savesettings function is essentially pulling 5 parameters from firebase and then saving them to eeprom. This appears to stall and block the function. The way i found this out was i removed the save settings function and and the blocking still occured when firebase getting the values at the same time. I rearranged the firebase stream so that it pulls 1 value at a time and the problem is no more.

That's not 'D1_FunctionInt'. It's printing 'x' which is the value returned by 'EEPROM.get(0, D1_FunctionInt);'. I believe the value '1' means 'EEPROM.get() is reporting success'.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.