// Simple State Machine - Steven Martin (st3v3n92)
#include "KTS_Button.h"
KTS_Button buttonArray[] = { 7, 6 };
const byte NUM_BUTTONS = sizeof(buttonArray) / sizeof(buttonArray[0]);
enum States {
stateOne = 0,
stateTwo,
stateThree,
stateFour,
NUM_STATES
}; States state;
void cycleState(int8_t change) {
state = state + change;
// The below line wraps around between the last and first state and back.
state = state == NUM_STATES ? 0 : state < 0 ? NUM_STATES-1 : state;
}
void handleButtons() {
for (int i = 0; i < NUM_BUTTONS; i++)
if (buttonArray[i].read() == SINGLE_PRESS)
switch (i) {
case 0:
cycleState(1);
break;
case 1:
cycleState(-1);
break;
default:
break;
}
}
void handleStates() {
switch (state) {
case stateOne:
Serial.println("stateOne");
break;
case stateTwo:
Serial.println("stateTwo");
break;
case stateThree:
Serial.println("stateThree");
break;
case stateFour:
Serial.println("stateFour");
break;
default:
break;
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
handleButtons();
handleStates();
}