Problem with buttons.. please help

Hello there, I am trying to initialize a matrix of momentary switches each connected to digital pins of the Mega, and other side to ground, I initialize the switch pins as INPUT & HIGH so that the main loop checks if the are LOW when pressed and grounded momentarily, but for some reason this does not work, heres the test code for 1 switch & 1 LED, should i supply the otherside of the switch with +5v instead of ground?

void setup() {
  pinMode(51, INPUT); //switch pin, otherside connected to ground
  pinMode(49, OUTPUT); // LED pin
  digitalWrite(51, HIGH); // Initializes switch pin to high
}
int count = 1;
byte state;
byte state2 = HIGH;

void loop(){
 state = digitalRead(51);
  if(state == LOW){ //if switch is pressed
 count = count*(-1); // count becomes negative vale
  }
   if (count >0) {digitalWrite(49,HIGH);} //if count if 1, LED is on
   else { digitalWrite(49, LOW);} // if count is -1, LED is off
  
  digitalWrite(51,HIGH); // reset input pin
}

You need to have a pulldown or pullup resistor, check these tutorials please

The comment in the code is wrong. It is enabling the pull-up resistor. So, no external resistors are required.

However, as you'll see in the Button (I hate that name. Buttons are for shirts) tutorial, you need to supply +V to be switched. Switching ground on or off doesn't mean anything.

Ok so I was stupid and wired the buttons wrong, but i ended up using pinMode as INPUT and digital Write HIGH to the button input and grounding the other side and everything works fine.

I played around with my code yesterday for a while and kept on building up, but now i'm at a point where one of the buttons works right away, but another works seldom. Please note i tested both buttons with a simple button test program i wrote and they work fine, its this bit of code that the "bank_up" button only works here and there, not every press:

Note i've tried several configurations of this code, but still, no luck.

int Bank = 0;
int Bank_old = 0;

void loop() { //MAIN LOOP


  if((digitalRead(Bank_down)==LOW) &&(BANKSTATE[2]==HIGH)){// bank down works
    delay(175);
    switch (Bank){
    case 0:
      Bank = 127;
    default:
      Bank--; // reads bank down button
    }
    digitalWrite(Bank_down, HIGH);
  }
  if((digitalRead(Bank_up)==LOW) &&(BANKSTATE[1]==HIGH)){ //bank works very poorly
    delay(175);
    switch (Bank){
    case 127:
      Bank = 0;
    default:
      Bank++; // reads bank up button
    }
    digitalWrite(Bank_up, HIGH);
  }
    if(Bank != Bank_old){
    SWITCHRESET(); // if pressed reset switches & LED's - works
    LCDBANKCHANGE(Bank);
    Bank_old = Bank; 
  }


  //check if change in pedals
  
  for(int Ptest=1;Ptest<5;Ptest++){
    PEDALSTATE[Ptest] = analogRead(PEDALS[Ptest]);
    if(PEDALSTATE[Ptest] != PEDALSTATE_OLD[Ptest]){
      MIDIPEDALSEND(PEDALSTATE[Ptest],Bank,Ptest);
    }
  }
  
  AISWITCHPRESS(Bank); //after bank check and pedal check check 13 AI switches seems to work with LEDS
}

That sketch didn't have a "setup()" - how did you get it to compile?

heres the whole thing....

/*
Arduino Mega MIDI foot controller:
-LCD printout
-4 pedals
-13 Instant Access Programmable buttons
- 2 bank up down buttons

R1.2.0 as of Dec.8th 2010:
there is TX contant output...
after one read through the program, it get's stuck on midi send of the pedal..
R1.2.1 12-8-2010
fixed bank down button, still problem with midi send and bank up, resent works nicely

*/
#define S1 51
#define S2 47
#define S3 43
#define S4 39
#define S5 35
#define S6 34
#define S7 30
#define S8 26
#define S9 31
#define Bank_up 27
#define S10 3
#define S11 46
#define S12 50
#define S13 42
#define Bank_down 38
#define Pot_a A0
#define Pot_b A1
#define Pot_c A2
#define Pot_d A3
#define LED1 49
#define LED2 45
#define LED3 41
#define LED4 37
#define LED5 33
#define LED6 32
#define LED7 28
#define LED8 24
#define LED9 29
#define LED10 2
#define LED11 44
#define LED12 48
#define LED13 40

#include <MIDI.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 10, 4, 5, 6, 7);

int SWITCHES[14] = {
  0,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13};
int SWITCHSTATES[14] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int SWITCHVAR[14] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int SWITCHVAR_OLD[14] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int BANKS[3]={
  0,Bank_up,Bank_down}; 
int BANKSTATE[3]={
    0,0,0};              
int LEDS[14] = {
  0, LED1,LED2,LED3,LED4,LED5,LED6,LED7,LED8,LED9,LED10,LED11,LED12,LED13};
int LEDSTATE[14] = {
  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int PEDALS[5] = {
  0,A0,A1,A2,A3};
int PEDALSTATE[5] = {
  0,0,0,0,0};
int PEDALSTATE_OLD[5] = {
  0,0,0,0,0};

void setup() {
  for(int d=1;d<14;d++){
    pinMode(LEDS[d],OUTPUT);
    pinMode(SWITCHES[d], INPUT);
    digitalWrite(SWITCHES[d], HIGH);
    SWITCHSTATES[d] = HIGH;
  }
  for(int d=1;d<3;d++){
    pinMode(BANKS[d], INPUT);
    digitalWrite(BANKS[d], HIGH);
    BANKSTATE[d] = HIGH;
  }
pinMode(36, OUTPUT);
pinMode(25, OUTPUT);

  MIDI.begin();
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Arduino GCP");
  lcd.setCursor(0,1);
  lcd.print("By EN R.1.2.0");
}

int Bank = 0;
int Bank_old = 0;

void loop() { //MAIN LOOP
/*
for(int a=1;a<3;a++){
  if(digitalRead(BANKS[a]==LOW) && (BANKSTATE[a] ==HIGH)){
    delay(75);
    if(BANKS[a] == Bank_up){
      if (Bank ==127) {Bank = 0;}
      else {Bank ++;}
    }
     else if(BANKS[a] == Bank_down){
      if (Bank ==0) {Bank = 127;}
      else {Bank--;};
    }
    else {break;}
  }
  digitalWrite(BANKS[a], HIGH);
}
*/

  if((digitalRead(Bank_down)==LOW) &&(BANKSTATE[2]==HIGH)){// bank down works
    digitalWrite(36,HIGH);
    delay(175);
    digitalWrite(36,LOW);
    switch (Bank){
    case 0:
      Bank = 127;
    default:
      Bank--; // reads bank down button
    }
    digitalWrite(Bank_down, HIGH);
  }
  if((digitalRead(Bank_up)==LOW) &&(BANKSTATE[1]==HIGH)){ //bank works very poorly
    digitalWrite(36, HIGH);
    delay(100);
    digitalWrite(36,LOW);
   // switch (Bank){
    //case 127:
    //  Bank = 0;
   // default:
      Bank++; // reads bank up button
   // }
    digitalWrite(Bank_up, HIGH);
  }
  
    if(Bank != Bank_old){
    SWITCHRESET(); // if pressed reset switches & LED's - works
    LCDBANKCHANGE(Bank);
    Bank_old = Bank; 
  }


  //check if change in pedals
  
  for(int Ptest=1;Ptest<5;Ptest++){
    PEDALSTATE[Ptest] = analogRead(PEDALS[Ptest]);
    if(PEDALSTATE[Ptest] != PEDALSTATE_OLD[Ptest]){
      MIDIPEDALSEND(PEDALSTATE[Ptest],Bank,Ptest);
    }
  }
  
  AISWITCHPRESS(Bank); //after bank check and pedal check check 13 AI switches seems to work with LEDS
}

void SWITCHRESET(){
  for(int a=1;a<14;a++){
    LEDSTATE[a] = -1;
    digitalWrite(SWITCHES[a], HIGH);
    digitalWrite(LEDS[a], LOW);
    SWITCHSTATES[a] = HIGH;
  }

}

void LCDBANKCHANGE(int bank){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Bank: ");
  lcd.print(bank);
}

void MIDIPEDALSEND(int val, int bank, int Pedal){

  int CC_Designation[4][128]={ //which CC designation does each pedal have? 4 pedals x 128 CC's
    {
      0,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14        }
    ,
    {
      0,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14        }
    ,
    {
      0,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14        }
    ,
    {
      0,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14        }
  };
  int CH_designation[4][128]={
    {
      0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2        }
    ,
    {
      0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2        }
    ,
    {
      0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2        }
    ,
    {
      0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2        }
    ,
  };

 // MIDI.send(CC,CC_Designation[Pedal][Bank],val,CH_designation[Pedal][Bank]); //with this commented out, no TX problem, but no Bank response
}

void AISWITCHPRESS(int Bank){
  for(int a=1;a<16;a++){
    if ((digitalRead(SWITCHES[a])==LOW)&& (SWITCHSTATES[a] == HIGH)){
      LEDSTATE[a]=LEDSTATE[a]*(-1);
      delay(175); //RELEASE TIME - varies depending on program size. allows for release to only trigger one change.
      //Break at this point, need to send to MASTER AI BANK CHANGE program
      //where Switch #, LEDSTATE, SWITCH assignment is taken into account and then sorted out what to do
      MASTERSWITCHER(a, Bank);

    }
    if(SWITCHVAR_OLD[a] != SWITCHVAR[a]){
      //MIDISEND(e,SWITCHVAR[e]);
      SWITCHVAR_OLD[a]=SWITCHVAR[a];
    }

  }
  for(int f=1;f<16;f++){ //this part resets the switches to high
    digitalWrite(SWITCHES[f], HIGH);
    SWITCHSTATES[f] = HIGH;
  }
}

void MASTERSWITCHER(int Switch, int Bank) { //this program needs to read the switch & bank value, and spit out correct
                                            // midi message and LED display.
  if (LEDSTATE[Switch] <0){
    SWITCHVAR[Switch] =0;
    digitalWrite(LEDS[Switch],LOW); //writes to LED, need to change for MAIN BANK/AI Program
  }
  else {
    SWITCHVAR[Switch] = 127;
    digitalWrite(LEDS[Switch], HIGH);
  }

}

Ok got it to work, there was a problem with digitalWrite HIGH to the reading pins

int BANKS[3]={
  0,Bank_up,Bank_down};
//
//
for(int a=1;a<3;a++){
  if(digitalRead(BANKS[a]==LOW) && (BANKSTATE[a] ==HIGH)){

You could save memory by using zero indices for your arrays.

well that code i'm using the banks as designations for the momentary switches, so i was supposed to digitalWrite the Bank_up and Bank_down as HIGH but i was only doing so for the Bank down button, so in the actual reading of the buttons the up would only work every other time, but i got it sorted out, will have to cut the fat out of the program after i get it to do everything i want, but thank you! :wink:

You could also check out the keypad library in the forum, that works very well.

I used it for this design in a remote control, with an interrupt generated on every keypress, then going to sleep afterwards.