Here is a fruit basket you can pick what you need:
//State Machine example
//LarryD
byte messageNumber;
unsigned long TaskWait;
unsigned long FlashTime;
unsigned long currentmillis;
//define the available machine states that we can have for this sketch
//add more states as needed
enum States{
stateStart, stateApple, stateOrange, statePear, statePeach, stateKiwi};
//mState = machine State
States mState = stateStart; //we start out in this machine state
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
FlashTime = millis();
TaskWait = millis(); //initialize the next wait time
mState = stateStart; //we start out in this machine state
} // >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<
void loop()
{
//leave this line of code at the top
currentmillis = millis();
//***************************
//just some code to see if the sketch is blocking
if (CheckTime(FlashTime, 100UL))
{
digitalWrite(13,!digitalRead(13));
}
//***************************
//Put your non-blocking regular stuff here
//****************************************************************
//Check machine state and do things that must be done accordingly
switch (mState)
{
//***************************
case stateStart:
{
//let us wait for 5 seconds before we print the Welcome message
if (CheckTime(TaskWait, 5000UL))
//if (CheckTime(TaskWait, waitTime)) //we could use a variable for the time to wait
{
Serial.println(F("Welcome"));
Serial.println(F("Press a KEY then press SEND"));
Serial.println();
//Other start stuff goes here
mState = stateApple; //advance to the next state
}
}
break; //end case stateStart:
//***************************
case stateApple:
{
if (Serial.available() > 0)
{
// read the incoming character:
char incomingChar = Serial.read();
// print what you received:
Serial.print(F("I received: "));
Serial.println(incomingChar);
Serial.println();
messageNumber = 1; //used in the next mState
mState = stateOrange; //advance to the next mState
}
}
break; //end case stateApple:
//***************************
case stateOrange:
{
//We will stay in this mState until some things are done
switch(messageNumber)
{
//***************************
case 1:
Serial.println();
Serial.println(F("Here is a message, we will wait 10 seconds and continue."));
Serial.println(F("Notice the LED on pin 13 keeps flashing."));
Serial.println();
TaskWait = millis(); //initialize the next wait time
messageNumber = 2; //advance to the next messageNumber state
break;
//***************************
case 2:
if (CheckTime(TaskWait, 10000UL))
{
Serial.println (F("Here is another message."));
Serial.println (F("We will now wait 5 sec and continue."));
Serial.println();
TaskWait = millis();
messageNumber = 3;
}
break;
//***************************
case 3:
if (CheckTime(TaskWait, 5000UL))
{
Serial.println (F("We can print some instructions then pause for a while,"));
Serial.println (F("this can be done without blocking other code from running."));
Serial.println (F("We will now wait 10 sec and continue, however other things will still happen."));
Serial.println();
TaskWait = millis();
messageNumber = 4;
}
break;
//***************************
case 4:
if (CheckTime(TaskWait, 10000UL))
{
//we are now finished with this mState, advance to the next mState
mState = statePear;
}
break;
} //end of switch messageNumber
break;
} //end case stateOrange:
//***************************
case statePear:
{
// You put statePear stuff here
mState = statePeach; //we are now finished with this mState, advance to the next mState
break;
} //end case statePear:
//***************************
case statePeach:
{
// You put statePeach stuff here
mState = stateKiwi; //we are now finished with this mState, advance to the next mSstate
break;
} //end case statePeach:
//***************************
case stateKiwi:
{
// You put stateKiwi stuff here
break;
} //end case stateKiwi:
} // end of switch(mState)
} // >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<
//======================================================================
// F U N C T I O N S
//======================================================================
//***************************
//Delay time expired function
boolean CheckTime(unsigned long &lastMillis, unsigned long wait)
{
//is the time up for this task?
if (currentmillis - lastMillis >= wait)
{
lastMillis += wait; //get ready for the next iteration
return true;
}
return false;
} //END of CheckTime()
//***************************
//======================================================================
// END OF CODE
//======================================================================