Multi Button Read Function

Hi,

I am trying to write a function to read multiple buttons at once and in one function rather then repeating the code each time. 3 of the 4 buttons work correctly with the code, but the tenBTN (10 Button) does not work correctly. It always outputs 0, wheres as the others output 1 once they are pressed. Any help would be great! Thanks.

#define oneBTN    10
#define tenBTN    9
#define fifBTN    8
#define hundBTN   7

//Array used to store button pin values
int buttons[] = {oneBTN, tenBTN, fifBTN, hundBTN};

//Array used to set previous button states
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH};

//Determine how many buttons are being used
#define NUMBUTTONS sizeof(buttons)

//Arrays to hold temp button info
int buttonCheck[NUMBUTTONS];
int buttonRead[NUMBUTTONS];
int buttonState[NUMBUTTONS];

//Debounce Button Values
long prevDebounceTime = 0;
long debounceDelay = 50;

void setup()
{
  //setup Arduino Serial Port
  Serial.begin(9600);
  Serial.println("Start of Sketch");
  
  //Set buttons as inputs
  for (int i=0; i<NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
}

void loop() 
{
  unsigned int time = 0;
  checkPhysButtons();
  
  Serial.print("1 Button: ");
  Serial.println(buttonCheck[2]);
  Serial.print("10 Button: ");
  Serial.println(buttonCheck[3]);
  Serial.print("50 Button: ");
  Serial.println(buttonCheck[4]);
  Serial.print("100 Button: ");
  Serial.print(buttonCheck[3]);
}  
  
void checkPhysButtons()
{
  int j;
  
  for (j=0; j < NUMBUTTONS; j++) {
    buttonRead[j] = digitalRead(buttons[j]);
    
    if(buttonRead[j] != prevButtonState[j]) {
      prevDebounceTime = millis();
    }
    
    if ((millis() - prevDebounceTime) > debounceDelay) {
      if (buttonRead[j] != buttonState[j]) {
        buttonState[j] = buttonRead[j];
        if (buttonState[j] == LOW) {
          buttonCheck[j] = 1;
        }
      }
    }
    prevButtonState[j] = buttonRead[j];
  }
}

Sorry, uploaded the wrong code. This is the code before I started messing around.

#define oneBTN    10
#define tenBTN    9
#define fifBTN    8
#define hundBTN   7
#define fillera   6 
#define fillerb   5

//Array used to store button pin values
int buttons[] = {oneBTN, tenBTN, fifBTN, hundBTN};

//Array used to set previous button states
int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH};

//Determine how many buttons are being used
#define NUMBUTTONS sizeof(buttons)

//Arrays to hold temp button info
int buttonCheck[NUMBUTTONS];
int buttonRead[NUMBUTTONS];
int buttonState[NUMBUTTONS];

//Debounce Button Values
long prevDebounceTime = 0;
long debounceDelay = 50;

void setup()
{
  //setup Arduino Serial Port
  Serial.begin(9600);
  Serial.println("Start of Sketch");
  
  //Set buttons as inputs
  for (int i=0; i<NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
}

void loop() 
{
  unsigned int time = 0;
  checkPhysButtons();
  
  Serial.print("1 Button: ");
  Serial.println(buttonCheck[0]);
  Serial.print("10 Button: ");
  Serial.println(buttonCheck[1]);
  Serial.print("50 Button: ");
  Serial.println(buttonCheck[2]);
  Serial.print("100 Button: ");
  Serial.print(buttonCheck[3]);
}  
  
void checkPhysButtons()
{
  int j;
  
  for (j=0; j < NUMBUTTONS; j++) {
    buttonRead[j] = digitalRead(buttons[j]);
    
    if(buttonRead[j] != prevButtonState[j]) {
      prevDebounceTime = millis();
    }
    
    if ((millis() - prevDebounceTime) > debounceDelay) {
      if (buttonRead[j] != buttonState[j]) {
        buttonState[j] = buttonRead[j];
        if (buttonState[j] == LOW) {
          buttonCheck[j] = 1;
        }
      }
    }
    prevButtonState[j] = buttonRead[j];
  }
}

The problem lies in buttonCheck[1], it does not ever get to the value 1.

//Array used to store button pin values
int buttons[] = {oneBTN, tenBTN, fifBTN, hundBTN};

//Determine how many buttons are being used
#define NUMBUTTONS sizeof(buttons)

That's a mistake. 'sizeof buttons' is 8, not 4, because the size is measured in bytes.

Try:

const int NUMBUTTONS = sizeof buttons / sizeof buttons[0];

That will give you the size of the array in elements instead of bytes.

Worked! THANKS!!!!

Why do you have buttonRead, buttonState, and prevButtonState? Why not just directly compare digitalRead(buttons[j]) with prevButtonState, this way seems a lot more efficient and you avoid the completely unnecessary extra array. Also, what is the point of buttonCheck? All it does is set each member of the array to 1 with no way to set it back to 0. If you are printing the state of the button, why not use buttonState? And since you are rolling with the for loops and arrays, you could easily do one for the Serial.prints using one of your existing arrays or making a new one for 1,10,50,100, and then just loop through.

or

#define NUMBUTTONS (sizeof(buttons) / sizeof(typeof(buttons[0]))

The outer parentheses are needed to prevent obscure problems if you should happen
to do something like

   foo/NUMBUTTONS

Which would otherwise expand wrongly.

You can even define

#define array_size(a)  (sizeof(a) / sizeof (typeof ((a)[0]))

#define NUMBUTTONS array_size(buttons)

MarkT:
or

#define NUMBUTTONS (sizeof(buttons) / sizeof(typeof(buttons[0]))

The outer parentheses are needed to prevent obscure problems

That's why I recommend using 'const int' for integer constants. It allows the compiler to provide more meaningful error messages.