Multiple Pushbuttons - detecting which one has been pressed

Hi,

in my hardware housing i have 7 pushbuttons (3PDT footswitches) and want to detect, which one has been pressed.

Here is my code:

unsigned short butPin[8];
unsigned short butPressed[8];
unsigned short butCurState[8];
unsigned short butPrevState[8];


void setup() {
  Serial.begin(9600);
  // Pushbutton/Pin assignment
  butPin[0] = 2;
  butPin[1] = 3;
  butPin[2] = 4;
  butPin[3] = 5;
  butPin[4] = 6;
  butPin[5] = 7;
  butPin[6] = 8;
  for(short i=0; i<7; i++) {
    // Set default state (not pressed) to all pushbuttons
    butPressed[i]=false;
    butCurState[i]=0;
    butPrevState[i]=0;
  }
}

void loop() {
  // Check one after the other whether the status of a foot switch has changed.
  for (short i=0; i<7; i++){
      butCurState[i] = digitalRead(butPin[i]);
      // Compare the buttonState to its previous state
      if (butCurState[i] != butPrevState[i]) {
        // State has changed --> Button has been pressed!
        // Write this to Array
        butPressed[i]=true;
      } else {
        // State has not changed --> Button has not been pressed!
        // Write this also to array
        butPressed[i]=false;
      }
      // save the current state as the last state, for next time through the loop
      butPrevState[i] = butCurState[i];
  }

  // Now the Array butPressed contains an overview, which button(s) has/have been pressed
  if (butPressed[0]) {
     Serial.println("Button 1 was pressed!");
  } else if (butPressed[1]) {
     Serial.println("Button 2 was pressed!");
  } else if (butPressed[2]) {
     Serial.println("Button 3 was pressed!");
  } else if (butPressed[3]) {
    Serial.println("Button 4 was pressed!");
  }  else if (butPressed[4]) {
    Serial.println("Button 5 was pressed!");
  } else if (butPressed[5]) {
    Serial.println("Button 6 was pressed!");
  } else if (butPressed[6]) {
    Serial.println("Button 7 was pressed!");
  } else {
    // No button was pressed
  }
  delay(100);
}

IThe strange thing: When i press pusbutton 7 i sometimes get “Button 7 was pressed” (which is what i expect) but sometimes “Button 6 was pressed” (which is wrong)

I have only connected pushbuttons 3 and 7 to the arduino (because the other ordered pushbuttons have not been delivered by amazon…), so at Pin 6 no pushbutton is connected. Why does the arduino anyway say that button 6 has been pressed?

Pushbutton 3 is connected to 5 V and the other side to GND (10k resistor in series) AND to DigitalPin4
Pushbutton 7 is connected to 5 V and the other side to GND (10k resistor in series) AND to DigitalPin8

Can u help me solving this problem?

Thanx a lot

Daniel

at Pin 6 no pushbutton is connected.

Connect the unused inputs to either 5V or GND so that they are in a known state. Better still, set all of the input pins pinMode() to INPUT_PULLUP to activate the built in pullup resistors and change your wiring to take the pin LOW when the button is pressed and change the program logic to match.