I'm having ago after reading through a few examples and it's not easy! I wish my brain worked more like yours. This is what I've tried but I'm getting invalid use of template-name 'State" without an argument for State Random = State(LED_PINSRandom);
where I initialise the states. I'm sure I've done many more things wrong too!
I think I've ended up with too many libraries too, when I've added it by going Sketch; Include Libraries; Finite-State-Machine. Seven libraries come up. The code looks like this
#include <Singleton.h>
#include <StateMachine.h>
#include <Transition.h>
#include <Action.h>
#include <Decision.h>
#include <ArrayList.h>
#include <State.h>
#include <Bounce2.h>
#define BUTTON_PIN 2 // Button connected to pin 2
#define NUM_LEDS 4 // Number of LEDs used on project
const int LED_PINS[NUM_LEDS] = {3, 4, 5, 6}; // Pins where the LEDs are connected
const byte NUMBER_OF_STATES = 3; //how many states are we cycling through?
//initialize states
State Off = State(LED_PINSWaiting);
State Flashing = State(LED_PINSFlashing);
State Random = State(LED_PINSRandom);
FSM ledStateMachine = FSM(On); //initialize state machine, start in state: On
Buounce2::Button button = Button::Bounce(); //initialize the button (wire between pin 12 and ground)
byte buttonPresses = 0;
void blink(int positionOnArray )
{
digitalWrite(LED_PINS[positionOnArray], HIGH);
delay(10000); // Keep the LED on by 1 second
digitalWrite(LED_PINS[positionOnArray], LOW);
}
void setup()
{
for(int i = 0; i < NUM_LEDS; i++)
{
pinMode(LED_PINS[i], OUTPUT);
}
button.attach(BUTTON_PIN, INPUT);
button.interval(5); // Bounce interval in ms
button.setPressedState(HIGH);
}
void loop()
{
case 1: ledStateMachine.transitionTo(Waiting); break;
case 2: ledStateMachine.transitionTo(Flashing); break;
case 3: ledStateMachine.transitionTo(Random); break;
}
{
ledStateMachine.update();
}
void LED_PINSWaiting()
{
interval(300000);
digitalWrite(LED_PINS[1], HIGH);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], HIGH);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], HIGH);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], HIGH);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], HIGH);
interval(300000);
}
void button.press (){LED_PINSFlash();
digitalWrite(LED_PINS[1], HIGH);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], HIGH);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], HIGH);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], HIGH);
digitalWrite(LED_PINS[4], LOW);
digitalWrite(LED_PINS[1], LOW);
digitalWrite(LED_PINS[2], LOW);
digitalWrite(LED_PINS[3], LOW);
digitalWrite(LED_PINS[4], HIGH);
}
void Random {LED_PINSRandom()
randomSeed(micros());
int randomNumber = random(0, NUM_LEDS);
blink(randomNumber);
// blink(0); // Will blink pin 3
// blink(1); // Will blink pin 4
// blink(2); // Will blink pin 5
// blink(3); // Will blink pin 6
}
I think I've lost the instruction to do the blink for 10 seconds along the way too.