OK, I've done some more reading on C++ functions. If you follow the examples here:
then it's very easy to see how a function is called, and (2 * 3) is 6.
What I still don't quite get is how ChangeState() is called below. Is that from the first line in the loop, the static int, once PulseLength has been updated?
// State Machine example from Arduino forum, timer controls state machine
// http://forum.arduino.cc/index.php/topic,59511.0.html
// Original author: wildbill
// States are named for ease of use
#define START 0
#define SCANNING 1
#define RESULTS 2
#define RESET 3
// Timing variables
const int StartTime = 2500;
const int ScanTime = 1500;
const int RESULTSTime = 1500;
const int ResetTime = 2000;
int state=START; // initial state
unsigned long NextAction=0L; // NextAction set to 0. "L" = long data format, used for timer.
void setup() {
Serial.begin(9600); // initialize serial communication for debugging
}
void loop()
{
static int PulseLength=0; // Declared "static" so it persists and is visible only to loop function.
if(millis()>NextAction) // If time since start (millis) is greater than NextAction...
PulseLength=ChangeState(); // ...then PulseLength is updated to ChangeState value.
Serial.println(PulseLength);
}
int ChangeState() // Function declared outside of the main loop, for managing the state
{
// int PulseLength=0;
switch (state)
{
case START:
Serial.println("case START");
NextAction+=StartTime; // NextAction is incremented
// PulseLength=1;
state=SCANNING;
break;
case SCANNING:
Serial.println("case SCANNING");
NextAction+=ScanTime; // NextAction is incremented
// PulseLength=2;
state=RESULTS;
break;
case RESULTS:
Serial.println("case RESULTS");
NextAction+=RESULTSTime; // NextAction is incremented
// PulseLength=3;
state=RESET;
break;
case RESET:
Serial.println("case RESET");
NextAction+=ResetTime; // NextAction is incremented
// PulseLength=4;
state=START;
break;
}
// return PulseLength;
}