In the following code, the && statement is not working correctly for limitFloorPin
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == LOW && digitalRead (limitFloorPin) == LOW) {
digitalWrite (downPin, HIGH);
Stage = 3;
}
Even when limitFloorPin is HIGH the program skips straight to Stage =3. I know that limitFloorPin is behaving correctly through the hardware because if I change the limitFloorPin state in the previous lines of code it will never get to Stage = 3.
if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == HIGH && digitalRead (limitFloorPin) == HIGH) {
digitalWrite (deployPin, LOW);
}
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == LOW && digitalRead (limitFloorPin) == HIGH) {
digitalWrite (deployPin, HIGH);
digitalWrite (downPin, LOW);
}
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == LOW && digitalRead (limitFloorPin) == LOW) {
digitalWrite (downPin, HIGH);
Stage = 3;
}
Also, it behaves correctly on the way up, entire code below.
int buttonDownPin = 14;
int buttonUpPin = 15;
int limitOpenPin = 2;
int limitDeployPin = 3;
int limitFloorPin = 4;
int limitDownPin = 5;
int limitStowPin = 6;
int limitFoldPin = 7;
int OpenPin = 8;
int ClosePin = 9;
int deployPin = 10;
int stowPin = 11;
int downPin = 12;
int upPin = 13;
/*Stage1 //Door closed, ramp stowed, lift up
Stage2 //Door open, ramp stowed, lift up
Stage3 //Door open, ramp deployed, lift floor level
Stage4 //Door open, ramp deployed, lift down*/
int Stage = 1;
void setup() {
// put your setup code here, to run once:
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() {
// put your main code here, to run repeatedly:
if (digitalRead (buttonUpPin) == LOW || digitalRead (buttonDownPin) == LOW) {
OutIn();
}
else {
for (int i = 8; i <= 13; i++) {
// all outputs (8-13) off (relay board is active low)
digitalWrite(i, HIGH);
delay (10);
}
}
}
void OutIn() {
if (Stage == 1) {
Door();
}
if (Stage == 2) {
Fold();
}
if (Stage == 3) {
Floor();
}
if (Stage == 4) {
Platform();
}
}
void Door() {
if (digitalRead (buttonUpPin) == LOW) {
digitalWrite(ClosePin, LOW);
}
if (digitalRead (buttonDownPin) == LOW && digitalRead (limitOpenPin) == HIGH) {
digitalWrite (OpenPin, LOW);
}
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitOpenPin) == LOW) {
digitalWrite (OpenPin, HIGH);
Stage = 2;
}
}
void Fold() {
if (digitalRead (buttonUpPin) == LOW && digitalRead (limitStowPin) == HIGH && digitalRead (limitFoldPin) == HIGH) {
digitalWrite(upPin, LOW);
}
else if (digitalRead (buttonUpPin) == LOW && digitalRead (limitStowPin) == LOW && digitalRead (limitFoldPin) == HIGH) {
digitalWrite (upPin, HIGH);
digitalWrite (stowPin, LOW);
}
else if (digitalRead (buttonUpPin) == LOW && digitalRead (limitStowPin) == LOW && digitalRead (limitFoldPin) == LOW) {
digitalWrite (stowPin, HIGH);
Stage = 1;
}
if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == HIGH && digitalRead (limitFloorPin) == HIGH) {
digitalWrite (deployPin, LOW);
}
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == LOW && digitalRead (limitFloorPin) == HIGH) {
digitalWrite (deployPin, HIGH);
digitalWrite (downPin, LOW);
}
else if (digitalRead (buttonDownPin) == LOW && digitalRead (limitDeployPin) == LOW && digitalRead (limitFloorPin) == LOW) {
digitalWrite (downPin, HIGH);
Stage = 3;
}
//if upButton fold platform until fold limit, set to stage1
//if downButton lower platform until floor limit, set to stage3
}
void Floor() {
if (digitalRead (buttonUpPin) == LOW) {
delay (1000);
Stage = 2;
}
else if (digitalRead (buttonDownPin) == LOW) {
delay (1000);
Stage = 4;
}
//if upButton stop motion while button pressed, set to stage2
// if downButton stop motion while button pressed, set to stage4
}
void Platform() {
if (digitalRead (buttonUpPin) == LOW && digitalRead (limitFloorPin) == LOW) {
digitalWrite(upPin, LOW);
}
else if (digitalRead (buttonUpPin) == LOW && digitalRead (limitFloorPin) == HIGH) {
digitalWrite (upPin, HIGH);
Stage = 3;
}
else if (digitalRead (buttonDownPin) == LOW) {// && (limitDownPin) == HIGH) {
digitalWrite (downPin, LOW);
}
// else if (digitalRead (buttonDownPin) == LOW && (limitDownPin) == LOW) {
// digitalWrite (downPin, HIGH);
// }
// if upButton raise lift to floor, set to Stage3
// if downButton lower lift until down limit* /
}
I know there's another way to write this code using switch/case. I will work on getting a great example that was sent to me working, but for now I'd like to understand what I have, any thoughts?
Video of the project can be seen here - Crow River wheelchair lift using Arduino microcontroller - YouTube