State Engine code - what's missing?

Hello All:

This code was copied from the "old forum", posted about year ago, showing how to write a state engine. It looks great, but I could not get it to blink an led.

What is the typo, or error?

Still in the lower level of the learning curve, and just cannot see what is wrong.

Thanks,

John

unsigned long currentTime = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime = 0; //no time has passed yet
int currentState = 0; //the default state
int wait = 0; //no need to wait, nothing has happened yet

void setup()
{
pinMode(13, OUTPUT); //the LED we're going to blink.. I just put 13 in as a magic number so it doesn't spoil the view of the worthwhile millis code
}

void loop()
{
pastTime = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
currentTime = millis(); //currentTime is now the current time (again).

unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed

if(timePassed >= wait) //part1 of the state engine
{
switch(currentState) //part2 of the state engine, its this simple, an if and a switch
{
case 0: //the default state.. turn the led on!
digitalWrite(13, HIGH); //light the LED up!
wait = 500; //wait 500 milliseconds before doing something from this state engine again
currentState = 1; //the next state that should be executed is 1
break; //break out of the switch, else it'll execute the case below this one too

case 1:
digitalWrite(13, LOW); //turn the LED off again
wait = 500; //wait 500 milliseconds again before turning the LED on again
currentState = 0; //turning the LED on again is done in state 0
break;
}
}
}

  pastTime    = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
  currentTime = millis();    //currentTime is now the current time (again).
 
  unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls

Got a piece of paper and pencil handy? Figure out what value is going to be stored in timePassed. Hint: it's the length of time that it takes to execute the millis() call.

  if(timePassed >= wait) //part1 of the state engine

Unless wait is defined as 0, this will never be true.

If you'd like OOP and higher level responsibility you could check out my library for state machines: Arduino Playground - FiniteStateMachine Library

If you can't figure it out with pen and paper, add this after "currenTime = millis();"

  Serial.print("Past: ");
  Serial.println(pastTime, DEC);
  Serial.print("Current: ");
  Serial.println(currentTime, DEC);

Watch in the Serial monitor for a few seconds. You'll have to pull the USB cable from your Arduino to really see it, but I think you'll notice the problem between the two numbers.

Thank you Paul, and James

I will do as suggested.

Regards,

John