Push Button Debouncing improperly

I want to do something simple with 3 push buttons. All buttons, when pressed, will print a Serial message as seen here:

void setup() {

  Serial.begin(31250);                                                               //31250 - standard MIDI baud rate
  Serial.println("Press Buttons");

  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
  pinMode(BUTTON3,INPUT);
  
  digitalWrite(BUTTON1,HIGH);
  digitalWrite(BUTTON2,HIGH);
  digitalWrite(BUTTON3,HIGH);
}


boolean recorded_pressed_once = false;                                                //Makes sure the user doesn't do fishy things
boolean RECORD = false;
boolean STOP = true;
boolean LED_MODE = false;    //off by default


void loop() {
  //BUTTON 1
    if( button(BUTTON1) && RECORD == false && STOP == true ){                           //if button was pressed for 'RECORD'
      //RECORD press
      recorded_pressed_once = true;
      RECORD = true;
      STOP = false;
      Serial.println("BEGIN");
      while(button(BUTTON1));
    }else if( button(BUTTON1) && RECORD == true && STOP == false ){                          //else if button was pressed for 'STOP'
      //STOP press
      STOP = true;
      RECORD = false;
      Serial.println("STOP");
      while(button(BUTTON1));
    }
    
//BUTTON2
    if( button(BUTTON2) && RECORD == false && STOP == true ){                          //Do not generate unless STOP has been pressed
      if(recorded_pressed_once){
        Serial.println("GENERATING...");
        while(button(BUTTON2));
      }else{
        //No recording was ever taken. Do nothing
      }
    }
    
//BUTTON3
    if( button(BUTTON3) ){                                                            //Can turn on/off anytime
      if(LED_MODE == true){                                                                   //If already on, turn off
        Serial.println("LED mode OFF");
        LED_MODE = false;
        while(button(BUTTON3));
      }else if(LED_MODE == false){
        Serial.println("LED mode ON");
        LED_MODE = true;
        while(button(BUTTON3));
      }
    }
}

char button(char but_num)
{
  return (!(digitalRead(but_num)));
}

Currently my output is as follows:

Sometimes when any of the buttons is pressed (it happens with all of them, mostly on button 1) It more than one of the messeges. This poses a problem with button 1 and 3 as they print like this on occasion:

LED mode ON
LED mode OFF
LED mode ON

or

BEGIN
STOP
BEGIN

I'm not sure what is causing this, I am concerned that I am debouncing the buttons incorrectly, which for some reason leads to the inconsistent Serial output shown.

Hi,

Have you tried the bounce library just for a sanity check, Arduino Playground - HomePage

All the best

Ian

Try this.

/* 
 Debounce
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin[] = {A0,A1,A2,A3}; // Number of buttons
const int ledPins[] =  {13,12,11,10};      // the number of the LEDs

int MAX_PINS = 4; // how many buttons to look for
int i = 0;
int counter=0;
int reading[4];

//byte ledState = HIGH;

int buttonState[4];             // the current reading from the input pin
int lastButtonState[] = {LOW,LOW,LOW,LOW};   // the previous reading from the input pin
long lastDebounceTime[] = {0,0,0,0};  // the last time the output pin was toggled

long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  Serial.begin(9600);
  for(i = 0; i <= MAX_PINS; i++){
    pinMode(buttonPin[i], INPUT); //set button states
    pinMode(ledPins[i], OUTPUT); // set LED states
  }
}

void loop() {
  // read the state of the switch into a local variable:
  for(counter = 0; counter <= MAX_PINS; counter++) // cycle through and check all buttons
  {
    reading[counter] = digitalRead(buttonPin[counter]);// look at button input and record it.
    if (reading[counter] != lastButtonState[counter]) {
    lastDebounceTime[counter] = millis();
    } 

    if ((millis() - lastDebounceTime[counter]) > debounceDelay) {
      buttonState[counter] = reading[counter];
    }

    digitalWrite(ledPins[counter], buttonState[counter]);
    lastButtonState[counter] = reading[counter];
    //Serial.print("counter: ");
    //Serial.print(counter);
    //Serial.println();
  }
}