With the help of many on this forum I completed my first arduino project. The concept of state machine kept coming up over and over during my newbie questions and the final project ended up with a pseudo state machine using if/else commands. PaulMurrayCbr (thanks again Paul) was awesome enough to write an entire program with switch/case to perform the same functions.
I had to print out his code and study it for quite some time for it to make sense to me, it finally does. One part I can't figure out though is the following line:
digitalWrite(deployPin, digitalRead(buttonDownPin)==LOW);
What this seems to me is that it will write the deployPin equal to buttonDownPin only while buttonDownPin is LOW. Is my thinking correct? The problem is, when I load the switch/case code and activate buttonDownPin the program just runs all the way through state to state and I have to unplug the arduino to get it to stop. This part isn't making sense to me, I thought if you let go of the button the program should stop. This is how I have my if/else program set up.
I'll post the two complete programs below. Is there any specific reason for using one versus the other? Maybe in a longer program where memory is an issue the switch/case would save room or speed things up?
PaulMurrayCbr's switch/case code
/**
Down()
Van door fully opens (openPin) and hits limitOpenPin
Lift fully unfolds (deployPin) and hits limitDeploy
Lift lowers (downPin) until limitFloor and stops
Load wheelchair from van
Press buttonDownPin again to lower lift to ground - limitDown
Up()
Load wheelchair from ground
upPin until limitFloor level and then stop
Roll wheelchair into van
upPin until limitDeploy
foldPin until limitStowPin
van door closePin until upPin released
*/
enum State {
CLOSED,
OPENING,
UNFOLDING,
DOWN_TO_FLOOR,
FLOOR,
DOWN_TO_GROUND,
GROUND,
UP_TO_FLOOR,
// FLOOR
UP_TO_STOW,
FOLDING,
CLOSING
} state = CLOSED;
const byte buttonDownPin = 14;
const byte buttonUpPin = 15;
const byte limitOpenPin = 2;
const byte limitDeployPin = 3;
const byte limitFloorPin = 4;
const byte limitDownPin = 5;
const byte limitStowPin = 6;
const byte limitFoldPin = 7;
const byte OpenPin = 8;
const byte ClosePin = 9;
const byte deployPin = 10;
const byte stowPin = 11;
const byte downPin = 12;
const byte upPin = 13;
void setup() {
pinMode(buttonDownPin, INPUT_PULLUP);
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(limitOpenPin, INPUT_PULLUP);
pinMode(limitDeployPin, INPUT_PULLUP);
pinMode(limitFloorPin, INPUT_PULLUP);
pinMode(limitDownPin, INPUT_PULLUP);
pinMode(limitStowPin, INPUT_PULLUP);
pinMode(limitFoldPin, INPUT_PULLUP);
pinMode(OpenPin, OUTPUT);
pinMode(ClosePin, OUTPUT);
pinMode(deployPin, OUTPUT);
pinMode(stowPin, OUTPUT);
pinMode(downPin, OUTPUT);
pinMode(upPin, OUTPUT);
}
void loop() {
switch (state) {
case CLOSED: state = state_CLOSED(); break;
case OPENING: state = state_OPENING(); break;
case UNFOLDING: state = state_UNFOLDING(); break;
case DOWN_TO_FLOOR: state = state_DOWN_TO_FLOOR(); break;
case FLOOR: state = state_FLOOR(); break;
case DOWN_TO_GROUND: state = state_DOWN_TO_GROUND(); break;
case GROUND: state = state_GROUND(); break;
case UP_TO_FLOOR: state = state_UP_TO_FLOOR(); break;
case UP_TO_STOW: state = state_UP_TO_STOW(); break;
case FOLDING: state = state_FOLDING(); break;
case CLOSING: state = state_CLOSING(); break;
}
}
State state_CLOSED() {
if(digitalRead(buttonDownPin)==LOW) {
digitalWrite(OpenPin, HIGH);
return OPENING;
}
return CLOSED;
}
State state_OPENING() {
if(digitalRead(limitOpenPin)==LOW) {
digitalWrite(OpenPin, LOW);
digitalWrite(deployPin, HIGH);
return UNFOLDING;
}
digitalWrite(OpenPin, digitalRead(buttonDownPin)==LOW);
return OPENING;
}
State state_UNFOLDING() {
if(digitalRead(limitDeployPin)==LOW) {
digitalWrite(deployPin, LOW);
digitalWrite(downPin, HIGH);
return DOWN_TO_FLOOR;
}
digitalWrite(deployPin, digitalRead(buttonDownPin)==LOW);
return UNFOLDING;
}
State state_DOWN_TO_FLOOR() {
if(digitalRead(limitFloorPin)==LOW) {
digitalWrite(downPin, LOW);
return FLOOR;
}
digitalWrite(downPin, digitalRead(buttonDownPin)==LOW);
return DOWN_TO_FLOOR;
}
State state_FLOOR() {
if(digitalRead(buttonDownPin)==LOW) {
digitalWrite(downPin, HIGH);
return DOWN_TO_GROUND;
}
if(digitalRead(buttonUpPin)==LOW) {
digitalWrite(upPin, HIGH);
return UP_TO_STOW;
}
return FLOOR;
}
State state_DOWN_TO_GROUND() {
if(digitalRead(limitDownPin)==LOW) {
digitalWrite(downPin, LOW);
return GROUND;
}
digitalWrite(downPin, digitalRead(buttonDownPin)==LOW);
return DOWN_TO_GROUND;
}
State state_GROUND() {
if(digitalRead(buttonUpPin)==LOW) {
digitalWrite(upPin, HIGH);
return UP_TO_FLOOR;
}
return GROUND;
}
State state_UP_TO_FLOOR() {
if(digitalRead(limitFloorPin)==LOW) {
digitalWrite(upPin, LOW);
return FLOOR;
}
digitalWrite(upPin, digitalRead(buttonUpPin)==LOW);
return UP_TO_FLOOR;
}
State state_UP_TO_STOW() {
if(digitalRead(limitDeployPin)==LOW) {
digitalWrite(upPin, LOW);
digitalWrite(stowPin, HIGH);
return FOLDING;
}
digitalWrite(upPin, digitalRead(buttonUpPin)==LOW);
return UP_TO_STOW;
}
State state_FOLDING() {
if(digitalRead(limitStowPin)==LOW) {
digitalWrite(stowPin, LOW);
digitalWrite(ClosePin, HIGH);
return CLOSING;
}
digitalWrite(stowPin, digitalRead(buttonUpPin)==LOW);
return FOLDING;
}
State state_CLOSING() {
if(digitalRead(buttonUpPin)==HIGH) {
digitalWrite(ClosePin,LOW);
return CLOSED;
}
digitalWrite(ClosePin, digitalRead(buttonUpPin)==LOW);
return CLOSING;
}