When using FSM, I get "not declared in this scope" all the time

Hi, when using my own program or your LED example, I keep getting error messages as if items are not declared. I ran out of options, please advice. Cheers, Gerrit Jan

I've included the compiler output.

Arduino: 1.6.6 (Windows 7), Board:"Arduino/Genuino Uno"

LED_FSM:11: error: 'ledOn' was not declared in this scope

State On = State(ledOn);

^

LED_FSM:12: error: 'ledOff' was not declared in this scope

State Off = State(ledOff);

^

LED_FSM:13: error: 'ledFadeIn' was not declared in this scope

State FadeIn = State(ledFadeIn);

^

LED_FSM:14: error: 'ledFadeOut' was not declared in this scope

State FadeOut = State(ledFadeOut);

^

Bibliotheek FSM in map: C:\Users\Gregory Weber\Documents\Arduino\libraries\FSM (legacy) wordt gebruikt Bibliotheek Button in map: C:\Users\Gregory Weber\Documents\Arduino\libraries\Button (legacy) wordt gebruikt Bibliotheek LED in map: C:\Users\Gregory Weber\Documents\Arduino\libraries\LED (legacy) wordt gebruikt exit status 1 'ledOn' was not declared in this scope

Despite the word "legacy", the libraries are the latest I could find on your site.

I've included the compiler output.

But not the code. No code == no help.

Hi Gregory, welcome.
To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

Hi everyone,

Here is the code he is speaking about; I am actually having the same problem. Running Arduino 1.6.7 on a Mac with all libraries freshly installed/updated. This sample code provided by Arduino gives the errors the original poster described.

//http://playground.arduino.cc/uploads/Code/FSM.zip
#include <FiniteStateMachine.h>
//http://playground.arduino.cc/uploads/Code/Button.zip
#include <Button.h>
//http://playground.arduino.cc/uploads/Code/LED.zip
#include <LED.h>

const byte NUMBER_OF_STATES = 4; //how many states are we cycling through?

//initialize states
State On = State(ledOn);
State Off = State(ledOff); 
State FadeIn = State(ledFadeIn);
State FadeOut = State(ledFadeOut); 

FSM ledStateMachine = FSM(On);     //initialize state machine, start in state: On

Button button = Button(12,PULLUP); //initialize the button (wire between pin 12 and ground)
LED led = LED(11);                 //initialize the LED
byte buttonPresses = 0;            //counter variable, hols number of button presses

void setup(){ /*nothing to setup*/ }

//poor example, but then again; it's just an example
void loop(){
  if (button.uniquePress()){
    //increment buttonPresses and constrain it to [ 0, 1, 2, 3 ]
    buttonPresses = ++buttonPresses % NUMBER_OF_STATES; 
    switch (buttonPresses){
      case 0: ledStateMachine.transitionTo(On); break;
      case 1: ledStateMachine.transitionTo(Off); break;
      case 2: ledStateMachine.transitionTo(FadeIn); break;
      case 3: ledStateMachine.transitionTo(FadeOut); break;
    }
  }
  ledStateMachine.update();
}

//utility functions
void ledOn(){ led.on(); }
void ledOff(){ led.off(); }
void ledFadeIn(){ led.fadeIn(500); }
void ledFadeOut(){ led.fadeOut(500); }
//end utility functions

You can get it working by doing this:

//http://playground.arduino.cc/uploads/Code/FSM.zip
#include <FiniteStateMachine.h>
//http://playground.arduino.cc/uploads/Code/Button.zip
#include <Button.h>
//http://playground.arduino.cc/uploads/Code/LED.zip
#include <LED.h>

const byte NUMBER_OF_STATES = 4; //how many states are we cycling through?

//initialize states
State On = State(&ledOn);
State Off = State(&ledOff);
State FadeIn = State(&ledFadeIn);
State FadeOut = State(&ledFadeOut);

FSM ledStateMachine = FSM(On);     //initialize state machine, start in state: On

Button button = Button(12,PULLUP); //initialize the button (wire between pin 12 and ground)
LED led = LED(11);                 //initialize the LED
byte buttonPresses = 0;            //counter variable, hols number of button presses

void setup(){ /*nothing to setup*/ }

//poor example, but then again; it's just an example
void loop(){
  if (button.uniquePress()){
    //increment buttonPresses and constrain it to [ 0, 1, 2, 3 ]
    buttonPresses = ++buttonPresses % NUMBER_OF_STATES;
    switch (buttonPresses){
      case 0: ledStateMachine.transitionTo(On); break;
      case 1: ledStateMachine.transitionTo(Off); break;
      case 2: ledStateMachine.transitionTo(FadeIn); break;
      case 3: ledStateMachine.transitionTo(FadeOut); break;
    }
  }
  ledStateMachine.update();
}

//utility functions
void ledOn(){ led.on(); }
void ledOff(){ led.off(); }
void ledFadeIn(){ led.fadeIn(500); }
void ledFadeOut(){ led.fadeOut(500); }
//end utility functions

This is caused by a change in the way the function parameters are automatically generated starting in Arduino IDE 1.6.6(arduino-builder): Auto prototype generation with function as argument to class instantiation · Issue #50 · arduino/arduino-builder · GitHub
So now you need to add a & to the callback function reference like so:

//initialize states
State On = State(&ledOn);
State Off = State(&ledOff);
State FadeIn = State(&ledFadeIn);
State FadeOut = State(&ledFadeOut);

You could also manually declare the function prototypes for the callback functions(or as they are called in the sketch “utility functions”) before those lines.

Sorry for the very late response. The & certainly does the trick, also for my other project, thanks! Regards, Gerrit Jan btw, I was using someone else computer, that's why it said Gregory Weber in the messages. :-)

Glad to hear it! Thanks for taking the time to share your results. That is a helpful thing to do for others who encounter the same problem and find this thread while searching for a solution.