user983: I have the following setup and sketch.
The problem is that when I click the button it works sometimes, but other times it executes On Off straight after On as if it's executing twice.
This is after pressing the button only 7 times:
Off
On
Off
On
Off
On
Off
On
Off

I thought that was the whole point of a state machine? That it continues to execute the next state (not multiple times at random).
Very very confused with this one.

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

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

//initialize states
State On = State(ledOn);
State Off = State(ledOff); 

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

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

void setup(){
  Serial.begin(9600);
}

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

//utility functions
void ledOn(){ led.on(); }
void ledOff(){ led.off(); }
//end utility functions


user42: It doesn't look like the state machine is a problem here. More likely it's a problem of the button bouncing, as you don't have any code or circuitry to guard against it.
A button is typically a very simple component which doesn't necessarily make/break connections cleanly. Sometimes it will have a brief intermittent connection, making it look like it's been pressed/released multiple times. This is referred to as bouncing.
There's lots of material online about debouncing, whether in hardware or software. The top answer here may help:

Why does my sketch report too many button presses?


user6452: Maybe you need some sort of debouncing? There's a tutorial on the Arduino site that I think will sort your problem out.

983: <p>I have the following setup and sketch.</p>

<p>The problem is that when I click the button it works sometimes, but other times it executes On Off straight after On as if it's executing twice.</p>

<p>This is after pressing the button only 7 times:</p>

<pre><code>Off
On
Off
On
Off
On
Off
On
Off
</code></pre>

<p>I thought that was the whole point of a state machine? That it continues to execute the next state (not multiple times at random).</p>

<p>Very very confused with this one.</p>

<p><img src="https://i.stack.imgur.com/xcvLw.png" alt="enter image description here"></p>

<pre><code>//http://www.arduino.cc/playground/uploads/Code/FSM.zip
#include &lt;FiniteStateMachine.h&gt;
//http://www.arduino.cc/playground/uploads/Code/Button.zip
#include &lt;Button.h&gt;
//http://www.arduino.cc/playground/uploads/Code/LED.zip
#include &lt;LED.h&gt;

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

//initialize states
State On = State(ledOn);
State Off = State(ledOff); 

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

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

void setup(){
  Serial.begin(9600);
}

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

//utility functions
void ledOn(){ led.on(); }
void ledOff(){ led.off(); }
//end utility functions
</code></pre>
 42: <p>It doesn't look like the state machine is a problem here. More likely it's a problem of the button bouncing, as you don't have any code or circuitry to guard against it.</p>

<p>A button is typically a very simple component which doesn't necessarily make/break connections cleanly. Sometimes it will have a brief intermittent connection, making it look like it's been pressed/released multiple times. This is referred to as bouncing.</p>

<p>There's lots of material online about debouncing, whether in hardware or software. The top answer here may help:</p>

<ul>
<li><a href="https://arduino.stackexchange.com/questions/408/why-does-my-sketch-report-too-many-button-presses/411#411">Why does my sketch report too many button presses?</a></li>
</ul>
 6452: <p>Maybe you need some sort of <strong>debouncing</strong>? There's a <a href="http://arduino.cc/en/Tutorial/Debounce" rel="nofollow">tutorial</a> on the Arduino site that I think will sort your problem out.</p>
