Hi Everyone
I have successfully ran my code with nested if /else statements but due to the project size i was persuaded to try the State Machine way..
Unfortunately i can't seem to get my head round the programming code side of this
const long DBTIME = 25; // number of ms allowed to eliminate key bounce
int machinestate();
bool debounce(bool state, bool val, long time, long dbTime, bool& lastVal, long& lastTime) {
// We will only register a change of state if the input value has not
// changed for a certain time
bool newState;
newState = state; // by default the new state is the old state
if (lastVal != val) { // if the input value has changed
lastTime = time; // set the last time it changed to "now"
lastVal = val; // and retain the current input value
} else { // otherwise (input value has NOT changed)
if ((time - lastTime) >= dbTime) { // has it remained stable long enough
newState = val; // change the state
}
}
return newState; // This is the new state
}
bool simpleDebounce(int inPin, bool& state, bool& lastVal, long& lastTime) {
// A debounce routine with a simpler interface.
long time = millis(); // time in milliseconds
bool val = digitalRead(inPin); // current pin value
long dbTime = DBTIME; // the debounce interval
state = debounce(state, val, time, dbTime, lastVal, lastTime); // debounce the pin
return state; // return state
}
//
// INPUTS
//
// button A1 - London End Fiddle Yard Down "Train Ready Dispatch"
const int buttonA1 = 8;
bool lastButtonA1;
bool currentButtonA1;
long lastButtonA1Time;
bool btnA1Edge;
// Button B1 - London End Main Control Down "Train Accepted"
const int buttonB1 = 7;
bool lastButtonB1;
bool currentButtonB1;
long lastButtonB1Time;
bool btnB1Edge;
// Button B2 - London End Main Control Down "Ready Next Service"
const int buttonB2 = 6;
bool lastButtonB2;
bool currentButtonB2;
long lastButtonB2Time;
bool btnB2Edge;
//
// OUTPUTS
//
// LED A1 - Amber - "Train Ready Dispatch"
const int ledA1 = 5;
bool ledOnA1 = false;
// LED B1 - Green - "Train Accepted"
const int ledB1 = 4;
bool ledOnB1 = false;
// LED B2 - Red - "Ready Next Service"
const int ledB2 = 3;
bool ledOnB2 = false;
void writeLEDStates() {
// writes the current states to the LEDs
digitalWrite(ledA1, ledOnA1);
digitalWrite(ledB1, ledOnB1);
digitalWrite(ledB2, ledOnB2);
}
void setup(){
// initialize pins
pinMode(buttonA1, INPUT);
pinMode(buttonB1, INPUT);
pinMode(buttonB2, INPUT);
pinMode(ledA1, OUTPUT);
pinMode(ledB1, OUTPUT);
pinMode(ledB2, OUTPUT);
// set output states
writeLEDStates();
// get the initial state of buttons
currentButtonA1 = lastButtonA1 = digitalRead(buttonA1);
lastButtonA1Time = millis();
currentButtonB1 = lastButtonB1 = digitalRead(buttonB1);
lastButtonB1Time = millis();
currentButtonB2 = lastButtonB2 = digitalRead(buttonB2);
lastButtonB2Time = millis();
}
void readSwitchStates() {
// read the current button states
simpleDebounce(buttonA1, currentButtonA1, lastButtonA1, lastButtonA1Time);
simpleDebounce(buttonB1, currentButtonB1, lastButtonB1, lastButtonB1Time);
simpleDebounce(buttonB2, currentButtonB2, lastButtonB2, lastButtonB2Time);
}
bool pressEdge(bool& state, bool lastVal) {
// detects the leading edge of a button press.
if ((!state) && lastVal) { // button pressed, but state not yet set
state = true; // set the state without waiting for debouncing
return true; // return a valid edge
} else {
return false; // it's not a press edge.
}
bool btnA1Edge = pressEdge(currentButtonA1, lastButtonA1);
bool btnB1Edge = pressEdge(currentButtonB1, lastButtonB1);
bool btnB2Edge = pressEdge(currentButtonB2, lastButtonB2);
}
void function setStateB(bool &bState, long &lTime, bool bValue,bool bChanged) { // gather data and pass to void function stateChange
if (bState != bValue) {
bState = bValue;
lTime = millis();
bChanged = true;
}
}
void function setStateI(int &iState, long &lTime, int iValue, bool bChanged) { // gather data
if (iState != iValue) {
iState = iValue;
lTime = millis();
bChanged = true;
}
}
void function stateChange () { // run test to decide machineState pass to void function machineStates
setbState(bState,ltime= true)
bChanged = false;
switch(bState) {
case 0:
if (!btnA1Edge || !btnB1Edge || !btnB2Edge) {
machineState(-1)
}
break;
case 1:
if (btnA1Edge) {
machineState(1)
}
break;
case 2:
if (btnB1Edge) {
machineState(2);
}
break;
case 3:
if (btnB2Edge) {
machineState(3);
}
default:
machineState = (0);
break;
}
}
void function machineState(int newState) { // Set output states
setStateI(iState, lStateTime, true);
switch ( iState ) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
}
}
void loop() {
while (true) {
// get input states
readSwitchStates();
// set output states
writeLEDStates();
}
void function setStateB;
void function setStateI;
void function stateChange();
void function machineState();
}
}
Can't Get Rid of these faults
Steve_FSM1:8: error: expected initializer before 'setStateB'
Steve_FSM1:9: error: expected initializer before 'setStateI'
Steve_FSM1:10: error: expected initializer before 'stateChange'
Steve_FSM1:11: error: expected initializer before 'machineState'
Steve_FSM1:131: error: expected initializer before 'setStateB'
Also how do i get the machineState() function to work ? how and where to i declare this?
machineState(-1); machineState(1)
switch(bState) {
case 0:
if (!btnA1Edge || !btnB1Edge || !btnB2Edge) {
machineState(-1)
}
break;
case 1:
if (btnA1Edge) {
machineState(1)
}
break;