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 yetvoid 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 toocase 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;
}
}
}