I am still quite new at learning Kodular, having come from a C++ background. In C++ we often use State Machines for tasks which require different actions. See code below of an example
void MTimer1()
{
svM1_Active = EEPROM.get(0, M1_Active);
svM1_DoseTime = EEPROM.get(40, M1_DoseTime);
svM1_Doses = EEPROM.get(20, M1_Doses);
unsigned long TotalOn = svM1_Doses*svM1_DoseTime;
unsigned long TotalOff = 86400 - TotalOn;
unsigned long doseInt = TotalOff/svM1_Doses;
switch (M1state)
{
case m1SET:
{
if (svM1_Active == false)
{
MT1LED.setColor(Green);
MT1LED.on();
Blynk.virtualWrite(V5, "INPUT SCHEDULE");
M1state = m1SET;
}
else
{
M1state = m1ONset;
}
break;
}
case m1ONset:
{
if (M1_CDownRemain != svM1_DoseTime)
{
M1_CDownRemain = svM1_DoseTime;
M1state = m1ON;
}
break;
}
case m1ON:
{
if (M1_CDownRemain)
{
M1_CDownRemain--;
M1FormattedON(M1_CDownRemain);
digitalWrite(Plug1, HIGH);
MT1LED.setColor(Blue);
MT1LED.on();
}
if (M1_CDownRemain == 0)
{
M1state = m1OFFset;
}
break;
}
case m1OFFset:
{
if (M1_CDownRemain != doseInt)
{
M1_CDownRemain = doseInt;
M1state = m1OFF;
}
break;
}
case m1OFF:
{
if (M1_CDownRemain)
{
M1_CDownRemain--;
M1FormattedOFF(M1_CDownRemain);
digitalWrite(Plug1, LOW);
MT1LED.setColor(Green);
MT1LED.on();
}
if (M1_CDownRemain == 0)
{
M1state = m1SET;
}
break;
}
}
}
I need to recreate the above state machine which is basically a timer whereby the user sets the times a day it must activate and how long each active state is for. I was thinking of creating a State Machine with proceedures whereby one proceedure moves onto the next. Is this a proper way or viable way of approaching this task?