digitalRead() doesn't work inside interrupt.

Hi,
I am working on a motorized camera slider and am stuck on a weird problem. There are two switches to stop the slider when it has reached the end. Now, the one is on the far end with a 2,5m cable. This cable is long enough to pick up electromagnetic noise from the motor and trigger an interrupt. digitalRead() turns out to be a lot more reliable than just the interrupt, but I don’t want to poll it with every single loop. So I want to use digitalRead() inside the interrupt.
But for some reason it won’t work. Can you help? I hope it’s just a syntax error. :sweat_smile:

This works:

void setup() {
  contactLeft = digitalRead(buttonStopLeft);
  attachInterrupt(digitalPinToInterrupt(buttonStopLeft), contactToggleLeft, CHANGE);
}

void contactToggleLeft(){
  static uint32_t lastInterruptTimeLeft = 0;//Debounce
  if(millis() - lastInterruptTimeLeft > debounce){
    contactLeft = !contactLeft;
    if(contactLeft == true && slideDirection == -1){
      md.setM1Speed(0);
    }  
  }
  lastInterruptTimeLeft = millis();
}

This doesn't:

void setup() {
  attachInterrupt(digitalPinToInterrupt(buttonStopLeft), contactToggleLeft, RISING);
}

void contactToggleLeft(){
  static uint32_t lastInterruptTimeLeft = 0;//Debounce
  if(millis() - lastInterruptTimeLeft > debounce){ 
    if(digitalRead(buttonStopLeft) == HIGH && slideDirection == -1){
      md.setM1Speed(0);
    }   
  }
  lastInterruptTimeLeft = millis();
}

Well given you track rising you are 100% sure that your digitalRead will return HIGH anyway in case 2.

digitalRead should work in an interrupt.

You attach the interrupt on RISING, so this means that whenever contactToggleLeft is executed, buttonStopLeft will be HIGH (unless there is some very fast bouncing happening). If you thought that buttonStopLeft should sometimes be high, sometimes be low - well, no. Not unless you use CHANGING rather than RISING.