Switch Case - IF Statement executing when evaluating to false

I have this IF statement

else if (digitalRead(triggerPin) == HIGH && startActivated == true) {
    Serial.println('\n');
    Serial.println("DROPPING HEAD, PRESSURE 1 ON");
    Monitor();
    
    currentState = PRESSURE1;
    releaseHead();
    enablePressure1();
    }
      break;
    case PRESSURE1:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");       
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && pressure1Done == false && pressure2Done == false) {
    Serial.println('\n');
    Serial.println("STARTING TIMER 1");
    Monitor();
    startT1();
    delay(100);
    pressure1Done = true;
    currentState = PRESSURE2;
    }
      break;

It is triggering and executing the method StartT1() Even though triggerPin is HIGH.

The previous case enables the priming pressure, and then once a microswitch is pressed (triggerPin goes LOW) StartT1() is called which starts an external timer.
However the timer is being started before the trigger pin is pulled LOW by the microswitch.

I have spent hours and I just cant work it out.

Here's my serial monitor output,

CYCLE STARTING
Trigger State: 1
Start State: 1
Home State: 0
Active State: 0
Timer 1 State: 0
Timer 2 State: 0


DROPPING HEAD, PRESSURE 1 ON
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0


STARTING TIMER 1
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0


PRESSURE 2 ON, STARTING TIMER 2
Trigger State: 0
Start State: 1
Home State: 1
Active State: 1
Timer 1 State: 1
Timer 2 State: 0


RASISING HEAD
Trigger State: 0
Start State: 1
Home State: 1
Active State: 1
Timer 1 State: 1
Timer 2 State: 1



And here's my full code

// Define states
enum State {
  IDLE,
  STOP,
  DROPHEAD,
  PRESSURE1,
  PRESSURE2,
  GOHOME,
  CYCLE_COMPLETE
};

// Define input pins
const int startPin = D12;
const int StopPin = D11;
const int triggerPin = D10;
const int homePin = D9;
const int t1OutPin = D8;
const int t2OutPin = D7;

// Define output pins
const int t1StartPin = D5;
const int t2StartPin = D1;
const int dSol1Pin = D2;
const int dSol2Pin = D3;
const int uSol1Pin = D4;
const int runningPin = D0;

// Define initial states
State currentState = IDLE;
bool startActivated = false;
bool pressure1Done = false;
bool pressure2Done = false;
int startState;
int homeState;
int triggerState;

//Timer stuff
unsigned long T1startTime;
unsigned long T2startTime;
int T1oldState;
int T2oldState;

void setup() {
  // Set input pins
  pinMode(startPin, INPUT_PULLUP);
  pinMode(StopPin, INPUT_PULLUP);
  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(homePin, INPUT_PULLUP);
  pinMode(t1OutPin, INPUT_PULLUP);
  pinMode(t2OutPin, INPUT_PULLUP);

  // Set output pins
  pinMode(t1StartPin, OUTPUT);
  pinMode(t2StartPin, OUTPUT);
  pinMode(dSol1Pin, OUTPUT);
  pinMode(dSol2Pin, OUTPUT);
  pinMode(uSol1Pin, OUTPUT);
  pinMode(runningPin, OUTPUT);

  // Initialize outputs - Outputs are sinking so LOW is ON, HIGH is OFF. 
  digitalWrite(t1StartPin, HIGH);
  digitalWrite(t2StartPin, HIGH);
  digitalWrite(dSol1Pin, HIGH);
  digitalWrite(dSol2Pin, HIGH);
  digitalWrite(uSol1Pin, LOW);
  digitalWrite(runningPin, HIGH);

  startState = digitalRead(startPin);
  homeState = digitalRead(homePin);
  triggerState = digitalRead(triggerPin);
  
  Serial.begin(9600); 
  
}

void loop() {
  switch (currentState) {
    case IDLE:
      if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");
        currentState = STOP;
        disableOutputs();
      } else if (digitalRead(homePin) == LOW && digitalRead(startPin) == LOW && startActivated == false) {
        Serial.println('\n');
        Serial.println("CYCLE STARTING");
        Monitor();       
        currentState = DROPHEAD;
        startActivated = true;
      }
      break;

    case STOP:
      if (digitalRead(StopPin) == HIGH) {
        Serial.println('\n');
        Serial.println("STOP RELEASED RETURNING TO IDLE");
        
        currentState = IDLE;
        resetProgram();
      }
      break;
    
    case DROPHEAD:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");
        
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(startPin) == HIGH) {
        Serial.println('\n');
        Serial.println("START NOT PRESSED, RETURNING TO IDLE");
        
        currentState = IDLE;
        startActivated = false; 
      }
    else if (digitalRead(triggerPin) == HIGH && startActivated == true) {
    Serial.println('\n');
    Serial.println("DROPPING HEAD, PRESSURE 1 ON");
    Monitor();
    
    currentState = PRESSURE1;
    releaseHead();
    enablePressure1();
    }
      break;
    case PRESSURE1:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");       
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && pressure1Done == false && pressure2Done == false) {
    Serial.println('\n');
    Serial.println("STARTING TIMER 1");
    Monitor();
    startT1();
    delay(100);
    pressure1Done = true;
    currentState = PRESSURE2;
    }
      break;

    case PRESSURE2:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");     
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && digitalRead(t1OutPin) == HIGH && pressure1Done == true && pressure2Done == false) {
    Serial.println('\n');
    Serial.println("PRESSURE 2 ON, STARTING TIMER 2");
    Monitor();
    enablePressure2();
    startT2();
    delay(100);
    pressure2Done = true;
      currentState = GOHOME;
    }
      break;
    
    case GOHOME:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && digitalRead(t2OutPin) == HIGH &&  pressure1Done == true && pressure2Done == true) {
    Serial.println('\n');
    Serial.println("RASISING HEAD");
    Monitor();
    StopT1();
    StopT2();
    clampHead();
    currentState = CYCLE_COMPLETE;
    }
      break;
    
    case CYCLE_COMPLETE:
      if (digitalRead(homePin) == LOW) {
        Serial.println('\n');
        Serial.println("CYCLE COMPLETED SUCCESSFULLY RETURNING TO IDLE");
        
        currentState = IDLE;
        resetProgram();
      } else if (digitalRead(startPin) == LOW) {
        Serial.println('\n');
        Serial.println("START BUTTONS HELD - CYCLE COMPLETED SUCCESSFULLY RETURNING TO IDLE");
        
        currentState = IDLE;
        resetProgram();
      }
      break;
  }
}
    
void releaseHead() {
  digitalWrite(uSol1Pin, HIGH);
}

void clampHead() {
  digitalWrite(uSol1Pin, LOW);
  digitalWrite(dSol1Pin, HIGH);
  digitalWrite(dSol2Pin, HIGH);
}

void enablePressure1() {
  digitalWrite(dSol1Pin, LOW);
}

void enablePressure2() {
  digitalWrite(dSol2Pin, LOW);
}

void startT1() {
  digitalWrite(t1StartPin, LOW);
}
void StopT1() {
  digitalWrite(t1StartPin, HIGH);
}

void startT2() {
  digitalWrite(t2StartPin, LOW);
}
void StopT2() {
  digitalWrite(t2StartPin, HIGH);
}
void Monitor() {
    startState = digitalRead(startPin);
    homeState = digitalRead(homePin);
    triggerState = digitalRead(triggerPin);
    Serial.println((String)"Trigger State: " + triggerState);
    Serial.println((String)"Start State: " + startState);
    Serial.println((String)"Home State: " + homeState);
    Serial.println((String)"Active State: " + startActivated);
    Serial.println((String)"Timer 1 State: " + pressure1Done);
    Serial.println((String)"Timer 2 State: " + pressure2Done);
}

void disableOutputs() {
  digitalWrite(t1StartPin, HIGH);
  digitalWrite(t2StartPin, HIGH);
  digitalWrite(dSol1Pin, HIGH);
  digitalWrite(dSol2Pin, HIGH);
  digitalWrite(uSol1Pin, HIGH);
  digitalWrite(runningPin, HIGH);
}

void enableReturn() {
  digitalWrite(t1StartPin, HIGH);
  digitalWrite(t2StartPin, HIGH);
  digitalWrite(dSol1Pin, HIGH);
  digitalWrite(dSol2Pin, HIGH);
  digitalWrite(uSol1Pin, LOW);
  digitalWrite(runningPin, HIGH);
}

void resetProgram() {
  startActivated = false;
  pressure1Done = false;
  pressure2Done = false;
  currentState = IDLE;
  enableReturn();
}

very likely not.

how do you handle bouncing?

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

If you are using an AVR-based Arduino, like the Uno R3 or Mega, use of String operations causes program malfunctions and crashes, so avoid them.

In any case, replacing that line with these will save program space as well.

    Serial.print("Trigger State: ");
    Serial.println(triggerState);

It's a Portenta H7

I have physically stopped the trigger microswitch from being pressed, so its always HIGH and it still does it.

This is my monitor output

CYCLE STARTING
Trigger State: 1
Start State: 0
Home State: 0
Active State: 0
Timer 1 State: 0
Timer 2 State: 0


DROPPING HEAD, PRESSURE 1 ON
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0


STARTING TIMER 1
Trigger State: 1
Start State: 0
Home State: 0
Active State: 1
Timer 1 State: 0
Timer 2 State: 0

It hangs at this point and cant progress.

case PRESSURE2:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");     
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && digitalRead(t1OutPin) == HIGH && pressure1Done == true && pressure2Done == false) {
    Serial.println('\n');
    Serial.println("PRESSURE 2 ON, STARTING TIMER 2");
    Monitor();
    enablePressure2();
    startT2();
    delay(100);
    pressure2Done = true;
      currentState = GOHOME;
    }
      break;

If I remove the mechanical stop allowing the trigger to be pressed, then it can continue.

However, as far as I figure, it shouldn't be able to evaluate the case leading into that point.

    case PRESSURE1:
    if (digitalRead(StopPin) == LOW) {
        Serial.println('\n');
        Serial.println("STOP PRESSED");       
        currentState = STOP;
        disableOutputs();
      }
    else if (digitalRead(triggerPin) == LOW && pressure1Done == false && pressure2Done == false) {
    Serial.println('\n');
    Serial.println("STARTING TIMER 1");
    Monitor();
    startT1();
    delay(100);
    pressure1Done = true;
    currentState = PRESSURE2;
    }
      break;

Please post all the code. The mistake may be elsewhere.

The full code is in the OP

How?

Print the value of the digitalRead to be sure


// Define output pins
const int t1StartPin = D5;
const int t2StartPin = D1;
const int dSol1Pin = D2;
const int dSol2Pin = D3;
const int uSol1Pin = D4;
const int runningPin = D0;

  • OP shouldn’t use D0 or D1.

I did, its shown as 1

This is on a Portenta H7, does it make a difference?
On the H7 Tx and Rx are on pins D13 and D14 respectively.

Hi @bnet ,

in your sketch you could write

   triggerState = HIGH;

in loop() before entering switch{} and replace every "digitalRead(triggerPin)" in the case conditions by triggerState, here just as an example:

//  else if (digitalRead(triggerPin) == LOW && digitalRead(t1OutPin) == HIGH && pressure1Done == true && pressure2Done == false) {
  else if (triggerState == LOW && digitalRead(t1OutPin) == HIGH && pressure1Done == true && pressure2Done == false) {

See if the behaviour is as expected. If yes replace the first line by reading the digital value:

// triggerState = HIGH;
 triggerState = digitalRead(triggerPin);

and cross fingers ....

Did you read it twice, one for the print and again in the if?

I'll give it a crack! Thank you

Hmm. I'll investigate

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.