FiniteStateMachines
FINITE STATE MACHINES FOR ARDUINO
ONLY a collection point for ideas so far! TK
- DISCUSSION: http://forum.arduino.cc/index.php?topic=202015.0
- GrumpyMike: http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
- NickGammon: http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11425&page=1
- NickGammom: http://www.gammon.com.au/statemachine
- WIKIPEDIA: https://en.wikipedia.org/wiki/Finite-state_machine
- Nilton: http://playground.arduino.cc/Code/SMlib
- QM STATE MACHINE BUILDER: http://forum.arduino.cc/index.php?action=profile;u=38681;sa=showPosts
EXAMPLE SOFTWARE TO BLINK AN LED:
BLINK FROM ARDUINO DISTRIBUTION (annotated):
/* YourDuinoStarter Example: BLINK - WHAT IT DOES: Blinks the built-in LED ON and OFF - SEE the comments after "//" on each line below - CONNECTIONS: - Built-In LED and resistor connected to pin 13 - - V1.01 02/11/13 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ //none yet /*-----( Declare Constants and Pin Numbers )-----*/ #define led 13 // define name of pin number NOTE: NO ";" on #define ! /*-----( Declare objects )-----*/ //none yet /*-----( Declare Variables )-----*/ //none yet void setup() /****** SETUP: RUNS ONCE ******/ { pinMode(led, OUTPUT); // initialize the digital pin as an output. }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second, watching the bright LED digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second, watch the Dark }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ //None yet //*********( THE END )***********
BLINK WITHOUT DELAY FROM ARDUINO DISTRIBUTION (annotated):
/* YourDuinoStarter Example: BLINK without delay - WHAT IT DOES Turns on and off a light emitting diode(LED) connected to a digital pin, without using the delay() function. This means that other code can run at the same time without being interrupted by the LED code. - SEE the comments after "//" on each line below - CONNECTIONS: - LED attached from pin 13 to ground. - Note: on most Arduinos, there is already an LED on the board that's attached to pin 13, so no hardware is needed for this example. created 2005 by David A. Mellis, modified 8 Feb 2010 by Paul Stoffregen modified 11 Nov 2013 by Scott Fitzgerald, Annotated 12/13 by Terry King - - V1.00 12/04/2013 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ // none /*-----( Declare Constants and Pin Numbers )-----*/ const int ledPin = 13; // the number of the LED pin const long interval = 1000; // interval at which to blink (milliseconds) /*-----( Declare objects )-----*/ // none /*-----( Declare Variables )-----*/ int ledState = LOW; // ledState used to set the LED // Generally, you shuould use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMillis = 0; // will store last time LED was updated unsigned long currentMillis = 0; // Will store the current time void setup() /****** SETUP: RUNS ONCE ******/ { pinMode(ledPin, OUTPUT); // set the digital pin as output: }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { // HERE is where you'd put code that needs to be running all the time. //----( What you need to do every loop to blink the LED )---------- // check to see if it's time to blink the LED: // Test IF the difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to blink the LED. currentMillis = millis(); // Get the current time if (currentMillis - previousMillis >= interval) // Check if time is up { // IF Time is up, do the things inside these Brackets : previousMillis = currentMillis; // save the last time you blinked the LED if (ledState == LOW) // Change the variable with the State of the LED ledState = HIGH; else ledState = LOW; digitalWrite(ledPin, ledState);// set the LED with the ledState of the variable }// End of handling "Time is Up" }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ // none //*********( THE END )***********
STATE MACHINE BLINK (Using SM Library from Nilton)
/* YourDuinoStarter Example: SM State Machine Example - WHAT IT DOES - Use the SM Library to do non-blocking delays, blink LED - SEE the comments after "//" on each line below - CONNECTIONS: - Uses Onboard LED13 - V1.00 12/3/2013 Questions: terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include <SM.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define PIN13_LED 13 /*-----( Declare objects )-----*/ SM ThisProg(Start); /*-----( Declare Variables )-----*/ //none void setup() /****** SETUP: RUNS ONCE ******/ { Serial.begin(115200); pinMode(PIN13_LED, OUTPUT); }//--(end setup )--- void loop() /****** LOOP: RUNS CONSTANTLY ******/ { EXEC(ThisProg); }//--(end main loop )--- /*-----( Declare User-written Functions )-----*/ State Start() { Serial.println("+++ Start"); if (ThisProg.Timeout(500)) ThisProg.Set(TurnOn) ; } //----------------------------------------- State TurnOn() { Serial.println("ON"); digitalWrite(PIN13_LED, HIGH); if (ThisProg.Timeout(500)) ThisProg.Set(TurnOff) ; } //----------------------------------------- State TurnOff() { Serial.println("OFF"); digitalWrite(PIN13_LED, LOW); if (ThisProg.Timeout(500)) ThisProg.Set(TurnOn) ; } //*********( THE END )***********
FROM WESTFW (SWITCH STATE VARIABLE) BLINK EXAMPLE:
enum {LEDON, LEDOFF} ledstate = LEDOFF; #define PIN13LED 13 void setup () { } void loop() { delay(1000); switch (ledstate) { case LEDON : digitalWrite(PIN13LED, LOW); // if the LED was on, turn it off. ledstate = LEDOFF; // new state break; case LEDOFF : digitalWrite(PIN13LED, HIGH); //if it was off, turn it on. ledstate = LEDON; break; }//switch }//loop
SOME OBJECTIVES FOR WAYS TO ENABLE NEWBIES:
(PaulS):Some easy to understand examples of state machines (simple ones) might be all that some people are looking for. Then, there can be a section on more advanced state machines, and how to determine the states, the transitions, the actions that trigger (or not) transitions, and the actions that occur as a state change happens, with code snippets to illustrate what the state/event/transition/trigger/action looks like in Arduino-ese would be good.
(Terry) A good example of communications (like Serial, or eventually nRF24L01) 'concurrently' running with an application. Maybe a Communications State Machine which interacts with another Application State Machine??
zz