I am writing code for a peristaltic doser for my aquarium. The doser is controlled via an interface on my phone. The doser will have the option of dosing from 1 / day to 48 / day. I have decided to use a state machine to achieve this. My question is if I am in say the 48 times a day dose and it is time to dose the state machine should exist the current state and go to the dose state. The dose state will be the same for all the programming states. Once the dosing function is complete is there a way to remember what previous state it came from (in the example above it would be the 48 times a day?
State Machine SetUp
enum D1_MainFunction {D1_MAINFUNCTIONIDLE, D1_BOOSTMODE, D1_1DOSEDAY, D1_2DOSEDAY, D1_6DOSEDAY, D1_12DOSEDAY, D1_24DOSEDAY, D1_48DOSEDAY, D1_DOSERACTIVE};
D1_MainFunction D1MainFunctionState = D1_MAINFUNCTIONIDLE;
int D1_PREVSTATE; //???????WILL THIS REMEMBER THE PREVIOUS STATE
State Machine Function
void D1_MainFunction()
{
EEPROM.get(10, D1_DoseTime);
EEPROM.get(0, D1_DailyDosageFloat);
switch (D1MainFunctionState)
{
case D1_MAINFUNCTIONIDLE:
{
if (D1_CalibrateActive)
{
digitalWrite(DOS1, HIGH);
}
else if (D1_BoostActive)
{
digitalWrite(DOS1, HIGH);
}
else
{
digitalWrite(DOS1, LOW);
}
break;
}
if (D1_SolutionFlag)
{
case D1_BOOSTMODE:
{
if (D1_ALKADoseActive)
{
D1_ALKATecFlag = true;
D1_BoostActive = true;
}
break;
}
case D1_1DOSEDAY:
{
D1_Time = (D1_DailyDosageFloat * D1_DoseTime) + Round;
if (TimeNow % Mod1 == 0)
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(1, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_2DOSEDAY:
{
D1_Time = ((D1_DailyDosageFloat / 2) * D1_DoseTime) + Round;
if (TimeNow % Mod2 == 0)
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(2, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_6DOSEDAY:
{
D1_Time = ((D1_DailyDosageFloat / 6) * D1_DoseTime) + Round;
if (TimeNow % Mod6 == 0 )
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(6, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_12DOSEDAY:
{
D1_Time = ((D1_DailyDosageFloat / 12) * D1_DoseTime) + Round;
if (TimeNow % Mod12 == 0)
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(12, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_24DOSEDAY:
{
D1_Time = ((D1_DailyDosageFloat / 24) * D1_DoseTime) + Round;
if (TimeNow % Mod24 == 0)
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(24, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_48DOSEDAY:
{
D1_Time = ((D1_DailyDosageFloat / 48) * D1_DoseTime) + Round;
if (TimeNow % Mod48 == 0)
{
D1_VolFlag = true;
D1_DoserActiveFlag = true;
D1_VolUpdateFunction(48, 0, false);
D1MainFunctionState = D1_DOSERACTIVE;
}
break;
}
case D1_DOSERACTIVE:
{
//Code to dose
//HOW TO RETURN TO PREVIOUS STATE TO START NEXT ITERATION????????
break;
}
}
}
}