Pages: [1]   Go Down
Author Topic: State Engine code - what's missing?  (Read 1503 times)
0 Members and 1 Guest are viewing this topic.
Los Angeles, California
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Quote
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;
    }
  }
}
 

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45978
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.

Code:
  if(timePassed >= wait) //part1 of the state engine
Unless wait is defined as 0, this will never be true.
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you'd like OOP and higher level responsibility you could check out my library for state machines: http://www.arduino.cc/playground/Code/FiniteStateMachine
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6049
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Los Angeles, California
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Paul, and James

I will do as suggested.

Regards,

John
Logged

Pages: [1]   Go Up
Jump to: