# Simple finite state machine schematic

I was not quite sure where to post this question, but I have been looking at finite state machines, and came across Alexander Brevig’s library and code example at - http://playground.arduino.cc/Code/FiniteStateMachine#Description

I am wanting to construct the simple LED circuit example with the four states, and have installed the relevant libraries etc. I am having trouble trying to work out the circuit that can utilise this code, and am wondering if anyone might be kind enough to show me or direct me to a circuit schematic or even a simple verbal description would suffice.
I have attached the code. Thanks for reading, Pedro.

``````//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 = 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
``````

Button button = Button(12,PULLUP); //initialize the button (wire between pin 12 and ground)

Pin 12 → pushbutton → GND

LED led = LED(11); //initialize the LED

Pin 11 → LED → resistor → GND

Thanks for that Coding Badly. I deduced some of it but have not encountered internal pullup before
That's my excuse and I'm sticking to it XD Pedro.

Hi, I am wanting to use this in a project, but when I just tried to Verify the code, it throws these errors;

In file included from FSM.ino:1:
C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:23:22: error: WProgram.h: No such file or directory
In file included from FSM.ino:1:
C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:34: error: expected `)' before 'buttonPin' C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:42: error: 'uint8_t' does not name a type C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:43: error: 'uint8_t' does not name a type C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:44: error: 'uint8_t' does not name a type In file included from FSM.ino:3: C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:27: error: expected `)' before 'ledPin'
C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:32: error: 'byte' has not been declared
C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:33: error: 'byte' has not been declared
C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:38: error: 'uint8_t' does not name a type
In file included from FSM.ino:5:
C:\Users\otto\Documents\Arduino\libraries\FSM/FiniteStateMachine.h:69: error: 'boolean' does not name a type
FSM:18: error: no matching function for call to 'Button::Button(int, int)'
C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:32: note: candidates are: Button::Button()
C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:32: note: Button::Button(const Button&)
FSM:19: error: no matching function for call to 'LED::LED(int)'
C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:25: note: candidates are: LED::LED()
C:\Users\otto\Documents\Arduino\libraries\LED/LED.h:25: note: LED::LED(const LED&)

Hoping that some one can baby step me. I am new to this and it would help me alot.

C:\Users\otto\Documents\Arduino\libraries\Button/Button.h:23:22: error: WProgram.h: No such file or directory

Have you searched for any of the 42 bazillion other posts with thins message in them? What was so difficult about changing WProgram.h to Arduino.h?

plotto – Being relatively new to Arduino myself I have also been caught out with this before. Older versions of the IDE prior to version 1.0 used a WProgram.h header file as a part of the library whereas versions after version 1.0 now use Arduino.h

See here