Hi to all... In the pasts days I was working on a proyect that need some state machines. In the theory all is good, but bad thing happens if the machines are interrupted by an external factor (In my case the temperature sensor get a wrong reading)
So, my basic structure is something like this;
void loop()
{
rawTemp = get.temperature
temperature();
termostat();
}
void temperature ()
{
if (sensor == badReading ) temperature = unknow;
else if(sensor == goodReading)
{
if ( temC > highsetPoint) temperature = HIGH
if ( temC < lowsetPoint) temperature = LOW
}
void termostat()
{
if(temperature == unknow) stopAll();
else if( temperature == HIGH) refrigerationON();
else if(temperature == LOW) refrigerationOFF();
}
refrigerationON ///the fun begins yeiiii
{
if( refrigeration == true ) // bool to make the cycleof the machine just once
{
switch(c)
case 1: break; //the system
case 2: break; // is on
case 3: refrigeration = false; break;
}
The problem, is I need to make the sequence of steps in each refrigeration ON and OFF just once as long the function gets call. the boolean guard above do that and allow me to start the components just once.
PROBLEM:
The function gets interrumped by a sensor error, or any other of disturbance.
Solution(???):
Make the machine starts form zero each time, do its job and stop until the next time.
I was thinking in something like:
static enum{ noSate, begin, end } machineState;
refrigerationON ///the fun begins yeiiii
{
if(refrigerattion == true && machineState == begin) //I dont think this works, will prevent the machine from fuctioning
{ //clears machine }
if( refrigeration == true ) // bool to make the cycleof the machine just once
machineState = begin;
{
switch(c)
case 1: break; //the system
case 2: break; // is on
case 3:
refrigeration = false;
machineState = end;
break;
}
Im very confused, The idea is to use one or two static variables in to the fuction to know if it dont end properly, an if its the case.
Or in the best case scenario, use the static variables to re-start the machine the first time the main fuction call it.
The interest or the need to make it in the subrutine is to make each subrutine start and stop with out the need to insert more viariables in the code to do so, or depend of another subrutines to end properly to restart this.
I cant figure out.
Any one with more ideas?
Thanks.
-Alex.