attachedinterrupt for checking buttons

Hi all,

on the following code, buttons states are checked inside the loop.

The actions are perfomed when button in pushed.

Is it possible to link each physical button also to the interrupt pin, in order that checking button state is only performed when interrupt is triggered, so the check_button routine is only called when interrupt?

I have 5 buttons (the code shows only 2 of them).

I aim to make the code perform faster, whithout entering the check_button routine inside loop?

TIA

void loop() {
  check_button();
  switch (operation_mode) {
    case 'A': Automatic_Mode(); break;
    case 'C': Manual_Close();   break;
    case 'O': Manual_Open();    break;
    case 'P': motor_mode = 'S'; break;
  }
}

void check_button() {
  automatic.listen();
  manual_close.listen();

  if (automatic.onPress()) {
    if (operation_mode == 'A') {
      operation_mode = 'P';
      digitalWrite(LedAuto, LOW);
      digitalWrite(LedOpen, LOW);
      digitalWrite(LedClose, LOW);
      msgnum = 3;
    } else {
      operation_mode = 'A';
      digitalWrite(LedAuto, HIGH);
      digitalWrite(LedOpen, LOW);
      digitalWrite(LedClose, LOW);
      msgnum = 0;
    }
  }

  if (manual_close.onPress()) {
    if (operation_mode == 'C' && digitalRead(stop_close) == LOW) {
      operation_mode = 'P';
      digitalWrite(LedClose, LOW);
      msgnum = 5;
    } else {
      operation_mode = 'C';
      digitalWrite(LedAuto, LOW);
      digitalWrite(LedOpen, LOW);
      digitalWrite(LedClose, HIGH);
      msgnum = 4;
    }
  }
}

The way you are doing it is fine. Checking the button state is super fast; why do you think it is slowing down your code?

If the reason is that a delay elsewhere in the code is making the buttons less responsive, you should confront that issue by switching to millis for timing, as you'll just keep running into cases where the delay() is causing problems.

Otherwise, you only need interrupts to read a button if you are using the button to wake from sleep; as they make the code more complex and add new caveats, it's not advisable to use them unless your use case requires it.

Interupt service should only be reserved for timing-critical jobs. It should not be abused for checking button pressed. I know you think that having checks every time it loop() is a burden for the processor but I can surely tell you that it won't mind.

Please help by posting your complete program

Thank you for the fast answer.

The question is because inside the loop i also drive a stepper motor, and will also interface with a esp8266 (reading it's output) for being able to have the switchs pushed physically or virtually (thru wifi).....

the complete program: attached, cause it's more than 9K char long.

You haven't thought this through very well. Reading the button state is almost surely not the reason your code doesn't perform at the speed you want and using interrupts is almost surely not the solution.

Think about it, even if you have an ISR that runs on button activation, how are you going to get that information back to the main code? The only way is to use a (volatile) state variable that you'll have to POLL in your main loop anyway. So, if you have too much blocking code in your loop, you'll block the polling of that state variable same as polling the button. The key is to get rid of blocking code.

thank you for the answer. The code is attached in my previous answer. i guess the only blocking code i have inside the loop is a delay, which i decided to, to gentle pull or push the curtain when changing direction while on the move.

if there is other blocking code, and you kindly can can advise me, i'll be very grateful, as it will improve my knowledge (i'm an absolute beginner in arduino).