Multiple Button, Mode INPUT, Interrupt - wrong signals detected

Hello.

Testing four buttons triggering interrupts to flag variables results in uncontrollable behavior.
For example: Pressing multiple times button1 to signal PIN22 (interrupt) SOMETIMES triggers ANOTHER PIN too.
Same with other buttons. Pressing them should only trigger correspondending interrupts, but SOMETIMES it triggers other interrupts too.
Looking at the software it should be not possible, but is does do it in this way.

Maybe possible, that signal from button1(PIN22) is flooding backwards to PIN23?

Using: Arduino DUE, oDrive3.6

[EDIT: Changed picture]
[EDIT2: Changed code. Added "nointerrupts()" and "interrupts()"]


// // #include <SoftwareSerial.h>
#include <ODriveArduino.h>

// Printing with stream operator
template<class T> inline Print& operator <<(Print &obj,     T arg) { obj.print(arg);    return obj; }
template<>        inline Print& operator <<(Print &obj, float arg) { obj.print(arg, 4); return obj; }

// ODrive object
ODriveArduino odrive(Serial1);

int buttonPin22 = 22, buttonPin23 = 23, buttonPin24 = 24, buttonPin25 = 25; // Interrupt 2
volatile int vel_m0 = 0, vel_m1 = 0;
volatile boolean runnow0 = false, stop0 = false, runnow1 = false, stop1 = false;

void buttonInterrupt22() {
  runnow0 = true;
  stop0 = false;
  }

void buttonInterrupt23() {
  stop0 = true;
  runnow0 = false;
 }

void buttonInterrupt24() {
  runnow1 = true;
  stop1 = false;
  }

void buttonInterrupt25() {
  stop1 = true;
  runnow1 = false;
 }

void setup() {
  pinMode(buttonPin22, INPUT);
  pinMode(buttonPin23, INPUT);
  pinMode(buttonPin24, INPUT);
  pinMode(buttonPin25, INPUT);
  attachInterrupt(digitalPinToInterrupt(buttonPin22), buttonInterrupt22, RISING);
  attachInterrupt(digitalPinToInterrupt(buttonPin23), buttonInterrupt23, RISING);
  attachInterrupt(digitalPinToInterrupt(buttonPin24), buttonInterrupt24, RISING);
  attachInterrupt(digitalPinToInterrupt(buttonPin25), buttonInterrupt25, RISING);
  // ODrive uses 115200 baud
  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor to open
  Serial1.begin(115200);
  while (!Serial1) ; // wait for Arduino Serial Monitor to open
  Serial1 << "w axis0.controller.config.vel_limit 50.0\n";
  Serial1 << "w axis0.motor.config.current_lim 10.0\n";
  Serial1 << "w axis1.controller.config.vel_limit 500.0\n";
  Serial1 << "w axis1.motor.config.current_lim 10.0\n";

  runnow0 = false;
  stop0 = false;
  runnow1 = false;
  stop1 = false;
  Serial.println("Ready!");
}

void loop() {
  if (runnow0 == true) {
    noInterrupts();
    Serial1 << "w axis0" << ".requested_state " << "8" << '\n'; // AXIS_STATE_CLOSED_LOOP_CONTROL
    vel_m0 = 20;
    Serial1 << "v " << "0"  << " " << vel_m0 << "\n";
    printVar();
    runnow0 = false;
  interrupts(); 
  }

  if (stop0 == true){
   noInterrupts();
      Serial1 << "w axis0" << ".requested_state " << "1" << '\n'; // AXIS_STATE_IDLE
      printVar();
      stop0 = false;
    interrupts(); 
  }

  if (runnow1 == true) {
    noInterrupts();
      Serial1 << "w axis1" << ".requested_state " << "8" << '\n'; // AXIS_STATE_CLOSED_LOOP_CONTROL
      vel_m1 = 50;
      Serial1 << "v " << "1"  << " " << vel_m1 << "\n";
      printVar();
      runnow1 = false;
    interrupts(); 
  }

  if (stop1 == true){
    noInterrupts();
      Serial1 << "w axis1" << ".requested_state " << "1" << '\n'; // AXIS_STATE_IDLE
      printVar();
      stop1 = false;
    interrupts(); 
	}
  delay(100);
}

void printVar() {
      Serial.print(runnow0);
      Serial.print(" ");
      Serial.print(stop0);
      Serial.print(" ");
      Serial.print(runnow1);
      Serial.print(" ");
      Serial.println(stop1);
}

Your schematic can't possibly be correct. E.g. one side of each resistor is not connected anything.

Have a look at the schematic for button wiring here: Help with Buttons - #6 by UKHeliBob (image by @LarryD)

// Edit
I also suspect that your button wiring shorts 5V and GND when you press the button.

Please check https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/ which pins support interrupts on a Mega.

Thanks for your advice.
I made a mistake representing wrong wiring picture.
I changed the picture and the wiring used is like the new picture.

It works not too bad, but like I said, sometimes one button seem to trigger more than the dedicated interrupt.

I have found the failure.
As the Due uses 3.3V you have to use this voltage.
I have mistakenly used 5V and the results were unpredictable.
Now I have chnaged to 3.3V and everything works like it should.

it's much more common to wire switches between the pin and ground and configure the in with the internal pull-up resistor, INPUT_PULLUP. this avoids wiring an external resistor as well as using the correct voltage

it's UNcommon to use interrupts for switches because they need to be debounced.

look this over

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

Hi gcjr.

Thanks for your qualified advice.
Maybe I will use internal PULLUP later.
I've read many times, that switches and interrupts do not work very well together.
That is maybe true, but I do not know how to work around.
I am using interrupts because I had to stop one motor immediately.
To trigger this interrupt, I had to use a normal switch.
Debouncing I tried before but I erased it temporarily, because I had to clean the errors caused by my error using 5V instead of 3.3V for an Arduino Due.

Thanks also for the code-example showing debouncing.