Strange midi behavior on double function button

Hi I'm doing something wrong here i have a basic midi foot controller which has 10 buttons
4 program change buttons
4 cc buttons
2 bank buttons

I recently added a double function to the bank up button when it is in run mode it works as a bank switch
when it is pressed for a long time it lets you go into an edit function.

but when I press the bank up button it makes the midi go mad and sends out 6 or 7 messages
that haven't been programmed... even with no midi code added to the bank button.

I have tried changing the button. also put in a different long code for the button but it does the same thing.
also tried the midi Library instead of sending serial midi.
when I put the bank up button into a normal state change code it works fine so i don't think it's a hardware problem.

The sketch is a bit long but I` will include it. anyway, any help with this would be great.
Tia.

//#include <LiquidCrystal.h>
#include <EEPROM.h>
//#include <LCD.h>
//#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
int Cc1add = 99;
int Cc2add = 102;
int Cc3add = 109;
int Cc4add = 106;
bool CCmode;
bool Loopmode;
unsigned long currentMillis;
const int AAbutton[] = { 15, 16, A0};
const int numButtons = sizeof(AAbutton) / sizeof(AAbutton[0]);
enum { AA_2, AA_3, AA_4};
boolean ledState = false;
boolean AledState = false;
int lastButtonState[numButtons];

#include <Wire.h>

//#include <LiquidCrystal_I2C.h>
//# define POSITIVE 1
LiquidCrystal_I2C lcd (0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int buttonStatePrevious = HIGH;                      // previousstate of the switch
unsigned long Duration = 100;
unsigned long minButtonLongPressDuration = 300;
unsigned long LDuration = 500;// Time we wait before we see the press as a long press
unsigned long buttonLongPressMillis;                // Time in ms when we the button was pressed
bool buttonStateLongPress = false;                  // True if it is a long press
int counter = 0;
const int intervalButton = 50;                      // Time between two readings of the button state
unsigned long previousButtonMillis;                 // Timestamp of the latest reading
//bool buttonStateLongPress2 = false;
unsigned long buttonPressDuration;
int SWITCH1 = 4;

int SWITCH2 = 5;
int SWITCH3 = 6;
int SWITCH4 = 7;
int CCbutold = 1;
boolean flashing = false;
boolean flashingd = false;
//byte currentButtonState = HIGH;
//byte previousButtonState = HIGH;
unsigned long currentTime;
unsigned long onoffStarted;
long flashPeriod = 500;
bool lcdstate = true;
bool toggle = false;
int count = 0;
int previousBankdState = HIGH;
int currentBankdState = HIGH;
int Bankup;
int Bankdown;
int upLastTime = 0;
int downLastTime;
int upNow = 0;
int downNow;
int coun = 0;
int cc1c = 0;
int Con3 = 0;
byte glyphs[5][8] = {
  { B11111, B11111, B00000, B00000, B00000, B00000, B00000, B00000  } ,
  { B00000, B00000, B00000, B00000, B00000, B00000, B11111, B11111  } ,
  { B11111, B11111, B00000, B00000, B00000, B00000, B11111, B11111  } ,
  { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111  } ,
  { B00000, B00000, B00000, B00000, B00000, B01110, B01110, B01110  }
};
const int digitWidth = 3; // the width in characters of a big digit (excludes space between characters)
//arrays  to index into custom characters that will comprise the big numbers
// digits 0 - 4                             0      1       2      3      4
const char bigDigitsTop[10][digitWidth] = { 3, 0, 3, 0, 3, 32, 2, 2, 3, 0, 2, 3, 3, 1, 3,
                                            // digits 5-9                               5      6      7        8      9
                                            3, 2, 2, 3, 2, 2, 0, 0, 3,   3, 2, 3, 3, 2, 3
                                          };
const char bigDigitsBot[10][ digitWidth] = { 3, 1, 3, 1, 3, 1,  3, 1, 1, 1, 1, 3, 32, 32, 3,
                                             1, 1, 3, 3, 1, 3, 32, 32, 3, 3, 1, 3, 1, 1, 3
                                           };
char buffer[12]; // used to convert a number into a string

#define BOUNCEDELAY 25

// Variables:
int switches[4] = { SWITCH1, SWITCH2, SWITCH3, SWITCH4,};
int switchState[4] = { HIGH, HIGH, HIGH, HIGH,};
// Initial state of switch is high due to internal pullup
//int leds[4] = { 7, 8, 9, 10};
int currentSwitch = 0;
int currentProgram = 0;   // current program - sent to the output
int progNo = 0;
//int upLastTime, downLastTime;
int prog;
int progdown;
int bankminus = 0;
int bank = 0;
int bankup = 0;
int detectswitch;
int leadled = 12;
int menu = 0;
boolean ledon = false;
boolean editmode = false;
//const int lead = 12;

const int L1 = 8;
const int L2 = 9;
const int L3 = 10;
const int L4 = 11;
const int AA2 = 22;
const int AA3 = 23;
const int AA1 = 24;


const int bankPin = 2;
int disp;
int dispc;
const int bankdpin = 3;
int CC1 = 14;
int Con2 = 0;
int CCC3 = 0;
int CCC4 = 0;

//************************************************************************
boolean ledVal1 = false;    // state of LED 1
boolean ledVal2 = false;    // state of LED 2
boolean ledVal3 = false;    // state of LED 3
boolean ledVal4 = false;    // state of LED 4
//*************************************************************************************
void setup() {
  //  Set MIDI baud rate:
  //Serial.begin(31250);
  //Serial.begin(9600);
  MIDI.begin();
  lcd.begin(16, 2);
  for ( int i = 0; i < numButtons; ++i ) {
    pinMode (AAbutton[i], INPUT_PULLUP);
  }
  pinMode(CC1, INPUT_PULLUP);

  pinMode(SWITCH1, INPUT_PULLUP);
  pinMode(SWITCH2, INPUT_PULLUP);
  pinMode(SWITCH3, INPUT_PULLUP);
  pinMode(SWITCH4, INPUT_PULLUP);
  // lcd.print(currentSwitch);
  lcd.setCursor(3, 0); // top left
  lcd.print("ENIGMA rev2");
  delay(1000);
  lcd.setCursor(3, 2);
  lcd.print("Press Button");
  // lcd.clear();
  // lcd.print(currentSwitch);
  // create the custom glyphs
  for (int i = 0; i < 5; i++)
    lcd.createChar(i, glyphs[i]);   // create the 5 custom glyphs
  
  //digitalWrite(14, HIGH);


  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(L3, OUTPUT);
  pinMode(L4, OUTPUT);
  pinMode(leadled, OUTPUT);
  pinMode(AA2, OUTPUT);
  pinMode(AA3, OUTPUT);
  pinMode(AA1, OUTPUT);



  //}
  pinMode(bankPin, INPUT_PULLUP);
  pinMode(bankdpin, INPUT_PULLUP);

}

void loop() {
  int b = checkButton();
  if (b == 1) clickEvent();
  if (b == 2) doubleClickEvent();
  if (b == 3) holdEvent();
  if (b == 4) longHoldEvent();

  // readButtonState();           // read the button state
  // int buttonState;
  //************************************************************************************************************************************
  /* buttonState = digitalRead(AAbutton[AA_2]);

    // compare the buttonState to its previous state
    if (buttonState != lastButtonState[AA_2]) {
     if (buttonState == LOW) {
     // CCmode = false;
       ledState  = !ledState;
       digitalWrite(AA2, ledState);
    if(CCmode){


    Con2++;
     flashing = false;  lcd.clear();
       lcd.display();
       lcd.setCursor(2, 5);
    lcd.print("CC2   ");
       lcd.print(Con2);
    }
    else   if (ledState) {
        EEPROM.get(Cc2add, Con2);
         sendControlChange(Con2, 127);






    }else  {
         sendControlChange(Con2, 0);

       }
     }

     }

    lastButtonState[AA_2] = buttonState;

    int buttonState3 = digitalRead(AAbutton[AA_3]);

    // compare the buttonState to its previous state
    if (buttonState3 != lastButtonState[AA_3]) {
     if (buttonState3 == LOW) {
      // CCmode = false;
       ledState  = !ledState;
       digitalWrite(AA3, ledState);


    if(CCmode){

    flashing = false;

       //lcd.clear();
       lcd.display();
        Con3++;
       lcd.setCursor(2, 5);
    lcd.print("CC3   ");
       lcd.print(Con3);
    }
      else if (ledState) {
         EEPROM.get(Cc3add, Con3);
         Serial.println(Con3);
         sendControlChange(Con3, 127);

       } else {
         sendControlChange(Con3, 0);

       }

     }
    }
    lastButtonState[AA_3] = buttonState3;

    buttonState = digitalRead(AAbutton[AA_4]);

    // compare the buttonState to its previous state
    if (buttonState != lastButtonState[AA_4]) {
     if (buttonState == LOW) {
       ledState  = !ledState;
       digitalWrite(leadled, ledState);
    if(CCmode){
    CCC4++;
    flashing = false;

       //lcd.clear();
       lcd.display();
       lcd.setCursor(2, 5);
    lcd.print("CC4   ");
       lcd.print(CCC4);
    }else
       if (ledState) {
         EEPROM.get(Cc4add, CCC4);
         sendControlChange(CCC4, 127);

       } else {
         sendControlChange(CCC4, 0);

       }

     }
    }
    lastButtonState[AA_4] = buttonState;*/
  //************************************************************************************************************************************************
  for ( currentSwitch = 0; currentSwitch < 4; currentSwitch++ ) {
    if ((digitalRead(switches[currentSwitch]) != switchState[currentSwitch] ) && (switchState[currentSwitch] == LOW)) {
      MIDI.sendProgramChange(currentSwitch + bank, 1);
      // midiProg( 0xC0, currentSwitch + bank  );
      // onoffStarted = currentTime;
      lcd.display();
      coun = 0;
      Con2 = 0;
      Con3 = 0;
      CCC4 = 0;
      cc1c = 0;
      flashing = false;
      ledState = false;
      //flashingd = false;

      // digitalWrite(leadled,LOW);
      lcd.clear();
      // lcd.setCursor(0,0);
      //**********************************************************************************************************
      progdisplay();
      disp = currentSwitch;
      switch (disp) {
        case 0 :

          //  lcd.setCursor(5,0);
          // lcd.print("Clean");
          //lcd.setCursor(0, 0);
          //digitalWrite(L1,HIGH);
          showNumber( disp + 1 + bank , 0);
          break;
        case 1:
          // lcd.clear();
          //  lcd.setCursor(10,0);
          //  lcd.print("Crunch");
          // lcd.setCursor(0, 0);
          showNumber( disp + 1 + bank, 0);
          // digitalWrite(L2,HIGH);
          break;
        case 2:
          // lcd.clear();
          //   lcd.setCursor(10,0);
          //  lcd.print("Lead");
          // lcd.setCursor(0, 0);
          showNumber( disp + 1 + bank, 0);
          break;
        case 3:
          //  lcd.clear();
          //  lcd.setCursor(5,0);
          // lcd.print("Drive");
          showNumber(disp + 1 + bank, 0);
          break;
      }
      //*********************************************************************************************

      //**********************************************************displayNames*************************************
      int switche;
      //for (dispc = 0; dispc<6; dispc++){


      //*************************************************************************************************







      digitalWrite(L1, (currentSwitch) == 0);
      digitalWrite(L2, (currentSwitch) == 1);
      digitalWrite(L3, (currentSwitch) == 2);
      digitalWrite(L4, (currentSwitch) == 3);
      delay( BOUNCEDELAY );
      digitalWrite(leadled, LOW);
      //showNumber( currentSwitch+1 + bank,0);
    }




    switchState[currentSwitch] = digitalRead( switches[currentSwitch] );


  }

  //***************************************Flashbankup**********************************************************

  /*  currentButtonState = digitalRead(bankPin);
    if (currentButtonState != previousButtonState && currentButtonState == LOW)  //button has become pressed
    {
      bank += 4;
      if (bank > 16) bank = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      showNumber(disp + 1 + bank, 0);
      lcd.setCursor(9, 0);
      lcd.print("Bankup");
      //showNumber(7,0);
      // lcd.print(count+=4);
      flashing = true;
      //digitalWrite(ledPin,HIGH);

    }
    previousButtonState = currentButtonState;          //save the button state for the next check
  */
  if (flashing) {
    Flashlcd();
  }
  // else
  // {
  // showNumber(disp+1 + bank,0);  //force the LED off if not flashing
  // lcd.print(count++);

  // }


  //***************************Bankdown*************************************************************************

  currentBankdState = digitalRead(bankdpin);
  if (currentBankdState != previousBankdState && currentBankdState == LOW)  //button has become pressed
  {
    bank -= 4;
    if (bank < 0) bank = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    showNumber(disp + 1 + bank, 0);
    lcd.setCursor(9, 0);
    lcd.print("Bankdn");
    //showNumber(7,0);
    // lcd.print(count+=4);
    flashing = true;
    //digitalWrite(leadled,HIGH);

  }
  previousBankdState = currentBankdState;          //save the button state for the next check




  //*****************************************************************************************************
  //CC Button 1

  int CCbut = digitalRead(CC1);

  if (CCbutold == 0 && CCbut == 1) {
    if (CCmode) {
      cc1c++;
      flashing = false;

      lcd.clear();
      lcd.display();
      lcd.setCursor(2, 5);

      lcd.print("CC1   ");
      lcd.print(cc1c);
      Serial.print(cc1c);
    } else if (coun == 0) {



      digitalWrite(leadled, HIGH);
      EEPROM.get(Cc1add, cc1c);
    
      
      //sendControlChange(cc1c, 127);
      // MIDI.sendControlChange(90,127,1);
      Serial.print(cc1c);


      coun = 1;

    } else {

      digitalWrite(leadled, LOW);
      EEPROM.get(Cc1add, cc1c);
      //sendControlChange(cc1c, 0);

      coun = 0;
    }

  }


  // coun = 0;
  CCbutold = CCbut;
  delay(20);
}

void showDigit(int digit, int position)
{
  lcd.setCursor(position * (digitWidth + 1), 0);
  for (int i = 0; i < digitWidth; i++)
    lcd.print(bigDigitsTop[digit][i]);
  lcd.setCursor(position * (digitWidth + 1), 1);
  for (int i = 0; i < digitWidth; i++)
    lcd.print(bigDigitsBot[digit][i]);
}
void showNumber(int value, int position)
{
  int index; // index to the digit being printed, 0 is the leftmost digit
  itoa(value, buffer, 10); // see Recipe 2.8 for more on using itoa
  // dislay each digit in sequence
  for (index = 0; index < 10; index++) // display up to ten digits
  {
    char c = buffer[index];
    if ( c == 0) // check for null (not the same as '0')
      return; // the end of string character is a null, see Chapter 2
    c = c - 48; // convert ascii value to a numeric value  (see Recipe 2.9)
    showDigit(c, position + index);
  }
}


//  Send a three byte midi message
void midiSend(char status, char data1, char data2) {
  Serial.write(status);
  Serial.write(data1);
  Serial.write(data2);
}

//  Send a two byte midi message
void midiProg(char status, int data ) {
  Serial.write(status);
  Serial.write(data);
}
void sendControlChange(byte Ctrl, byte Value) {

  Serial.write(0xB0);
  Serial.write(Ctrl);
  Serial.write(Value);
}

void Flashlcd() {
  currentTime = millis();
  if (currentTime - onoffStarted > flashPeriod)  //if the flash period ended ?
  {
    if (lcdstate == true) {     //check to see state of backlight
      lcdstate = false;         //flip state of backlight
    }
    else if (lcdstate == false) {
      lcdstate = true;
    }



    if ( lcdstate == true) {        //the bool argument for backlight state (true/false)

      lcd.display();
      lcd.setCursor(0, 0);

      //lcd.print("1");//the statement that actualy changes the backlight state
    }
    else if ( lcdstate == false) {
      lcd.noDisplay();
      //lcd.setCursor(0,0);
      // lcd.print(count++);
    } //flip the state of the LED
    onoffStarted = currentTime;                  //save the time when it occured
  }


}
//**********************************************************
void readButtonState() {
  currentMillis = millis();    // store the current time
  // If the difference in time between the previous reading is larger than intervalButton
  if (currentMillis - previousButtonMillis > intervalButton) {

    // Read the digital value of the button (LOW/HIGH)
    int buttonStateL = digitalRead(AAbutton[AA_4]);

    // If the button has been pushed AND
    // If the button wasn't pressed before AND
    // IF there was not already a measurement running to determine how long the button has been pressed
    if (buttonStateL == LOW && buttonStatePrevious == HIGH && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = LOW;

      Serial.println("Button pressed");

    }

    // Calculate how long the button has been pressed
    buttonPressDuration = currentMillis - buttonLongPressMillis;

    // If the button is pressed AND
    // If there is no measurement running to determine how long the button is pressed AND
    // If the time the button has been pressed is larger or equal to the time needed for a long press
    if (buttonStateL == LOW && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      /* editmode = !editmode;
        // flashing = false;
        lcd.clear();
        lcd.setCursor (0, 0);
        lcd.print("EDITMODE");
        Serial.println("Button long pressed");

        if (!editmode) {
         EEPROM.put(Cc1add, cc1c);
                 EEPROM.put(Cc2add, Con2);

         EEPROM.put(Cc3add, Con3);
         EEPROM.put(Cc4add, CCC4);
        Serial.println(Con3);
         CCmode = false;
         Serial.println("save");
         lcd.clear();
         lcd.setCursor (0, 0);
         lcd.print("Save");
         delay(1000);
         lcd.clear();
         lcd.display();
         showNumber( disp + 1 + bank , 0);
         lcd.setCursor(9, 0);
         lcd.print("Bankup");
         flashing = true;

        }*/

    }



    // If the button is released AND
    // If the button was pressed before
    if (buttonStateL == HIGH && buttonStatePrevious == LOW) {
      buttonStatePrevious = HIGH;
      buttonStateLongPress = false;

      Serial.println("Button released");
      Serial.println();



      
      if (buttonPressDuration < minButtonLongPressDuration) {
        Serial.println("Button pressed shortly");
        // bank += 4;
        //  if (bank > 16) bank = 0;
        //  lcd.clear();
        //lcd.setCursor(0, 0);
        // showNumber(disp + 1 + bank, 0);
        //lcd.setCursor(9, 0);
        // lcd.print("Bankup");
        //showNumber(7,0);
        // lcd.print(count+=4);
        // flashing = true;
        //delay(50);
        //  if (editmode) {
        //   flashing = false;
        //   lcd.display();
        //   menu++;
        //   Cmenu();
        //  }
        //  lcd.clear();
        //lcd.setCursor (0, 0);
        //lcd.print(editmode);
      }
    }

    // store the current timestamp in previousButtonMillis
    previousButtonMillis = currentMillis;

  }

}
//**************************************************************
void Cmenu() {

  switch (menu) {
    case 1:
      lcd.clear();
      lcd.setCursor (0, 0);
      lcd.print("Select CC ");
      // Serial.println("Select CC");
      CCmode = true;
      Loopmode = false;
      break;
    case 2:
      lcd.clear();
      lcd.setCursor (0, 0);
      lcd.print("Loops 1 - 4");
      Serial.println("Loops 1-4");
      Loopmode = true;
      CCmode = false;
      break;
    case 3:
      lcd.clear();
      lcd.setCursor (0, 0);
      lcd.print("Loops 4 -8");
      Serial.println("LOOPS 4-8");
      break;
    case 4:
      lcd.clear();
      lcd.setCursor (0, 0);
      lcd.print("Display");
      Serial.println("Display");
      break;
    case 5:
      lcd.clear();
      lcd.setCursor (0, 0);
      lcd.print("Triggers");
      Serial.println("Triggers");
      break;
    default:
      menu = 0;

      break;
  }



}
//****************************************************
void progdisplay() {
  dispc = currentSwitch + bank;
  switch (dispc) {
    case 0 :
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Clean");
      // lcd.setCursor(0,0);
      //digitalWrite(L1,HIGH);
      // showNumber( disp+1 + bank ,0);
      break;
    case 1:
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Crunch");
      // lcd.setCursor(0,0);
      // showNumber( disp+1 + bank,0);
      // digitalWrite(L2,HIGH);
      break;
    case 2:
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Lead");
      // lcd.setCursor(0,0);
      //showNumber( disp+1 + bank,0);
      break;
    case 3:
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Drive");
      //showNumber(disp+1 + bank,0);
      break;
    case 4:
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Country");
      //showNumber(disp+1 + bank,0);
      break;
    case 5:
      lcd.clear();
      lcd.setCursor(5, 0);
      lcd.print("Tube S");
      //showNumber(disp+1 + bank,0);

      break;
    default:
      lcd.setCursor(10, 0);
      lcd.print("              ");

      break;

      //}
  }
}
//********************************************************************
void clickEvent() {
  ledVal1 = !ledVal1;
  // digitalWrite(ledPin1, ledVal1);
  if (ledVal1) {
    //  bank++;
    // sendControlChange(90, 127);
    // Serial.println("buton");
    MIDI.sendControlChange(90, 127, 1);
  }
}
void doubleClickEvent() {
  ledVal2 = !ledVal2;
  // digitalWrite(ledPin2, ledVal2);
  
}
void holdEvent() {
  ledVal3 = !ledVal3;
  // digitalWrite(ledPin3, ledVal3);
}
void longHoldEvent() {
  ledVal4 = !ledVal4;
  // digitalWrite(ledPin4, ledVal4);
}
//=================================================
//  MULTI-CLICK:  One Button, Multiple Events

// Button timing variables
int debounce = 20;          // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250;            // max ms between clicks for a double click event
int holdTime = 1000;        // ms hold period: how long to wait for press+hold event
int longHoldTime = 3000;    // ms long hold period: how long to wait for press+hold event

// Button variables
boolean buttonVal = HIGH;   // value read from button
boolean buttonLast = HIGH;  // buffered value of the button's previous state
boolean DCwaiting = false;  // whether we're waiting for a double click (down)
boolean DConUp = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true;    // whether it's OK to do a single click
long downTime = -1;         // time the button was pressed down
long upTime = -1;           // time the button was released
boolean ignoreUp = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false;        // when held, whether to wait for the up event
boolean holdEventPast = false;    // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton() {
  int event = 0;
  buttonVal = digitalRead(bankPin);
  // Button pressed down
  if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)

  {

    // i have tryed putting    sendControlChange(82, 0);
    // sendControlChange(90, 0);

    downTime = millis();
    ignoreUp = false;
    waitForUp = false;
    singleOK = true;
    holdEventPast = false;


    longHoldEventPast = false;
    if ((millis() - upTime) < DCgap && DConUp == false && DCwaiting == true)  DConUp = true;
    else  DConUp = false;
    DCwaiting = false;

  }
  // Button released
  else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)

  {
    if (not ignoreUp)
      Serial.println("butoff");

    {
      upTime = millis();
      if (DConUp == false) DCwaiting = true;
      else
      {
        event = 2;
        DConUp = false;
        DCwaiting = false;
        singleOK = false;

      }
    }
  }
  // Test for normal click event: DCgap expired
  if ( buttonVal == HIGH && (millis() - upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
  {
    event = 1;
    DCwaiting = false;
  }
  // Test for hold
  if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
    // Trigger "normal" hold
    if (not holdEventPast)
    {
      event = 3;
      waitForUp = true;
      ignoreUp = true;
      DConUp = false;
      DCwaiting = false;
      //downTime = millis();
      holdEventPast = true;
    }
    // Trigger "long" hold
    if ((millis() - downTime) >= longHoldTime)
    {
      if (not longHoldEventPast)
      {
        event = 4;
        longHoldEventPast = true;
      }
    }
  }
  buttonLast = buttonVal;
  return event;
}


here is the double function

void readButtonState() {
  currentMillis = millis();    // store the current time
  // If the difference in time between the previous reading is larger than intervalButton
  if (currentMillis - previousButtonMillis > intervalButton) {

    // Read the digital value of the button (LOW/HIGH)
    int buttonStateL = digitalRead(AAbutton[AA_4]);

    // If the button has been pushed AND
    // If the button wasn't pressed before AND
    // IF there was not already a measurement running to determine how long the button has been pressed
    if (buttonStateL == LOW && buttonStatePrevious == HIGH && !buttonStateLongPress) {
      buttonLongPressMillis = currentMillis;
      buttonStatePrevious = LOW;

      Serial.println("Button pressed");

    }

    // Calculate how long the button has been pressed
    buttonPressDuration = currentMillis - buttonLongPressMillis;

    // If the button is pressed AND
    // If there is no measurement running to determine how long the button is pressed AND
    // If the time the button has been pressed is larger or equal to the time needed for a long press
    if (buttonStateL == LOW && !buttonStateLongPress && buttonPressDuration >= minButtonLongPressDuration) {
      buttonStateLongPress = true;
      /* editmode = !editmode;
        // flashing = false;
        lcd.clear();
        lcd.setCursor (0, 0);
        lcd.print("EDITMODE");
        Serial.println("Button long pressed");

        if (!editmode) {
         EEPROM.put(Cc1add, cc1c);
                 EEPROM.put(Cc2add, Con2);

         EEPROM.put(Cc3add, Con3);
         EEPROM.put(Cc4add, CCC4);
        Serial.println(Con3);
         CCmode = false;
         Serial.println("save");
         lcd.clear();
         lcd.setCursor (0, 0);
         lcd.print("Save");
         delay(1000);
         lcd.clear();
         lcd.display();
         showNumber( disp + 1 + bank , 0);
         lcd.setCursor(9, 0);
         lcd.print("Bankup");
         flashing = true;

        }*/

    }



    // If the button is released AND
    // If the button was pressed before
    if (buttonStateL == HIGH && buttonStatePrevious == LOW) {
      buttonStatePrevious = HIGH;
      buttonStateLongPress = false;

      Serial.println("Button released");
      Serial.println();



      
      if (buttonPressDuration < minButtonLongPressDuration) {
        Serial.println("Button pressed shortly");
        // bank += 4;
        //  if (bank > 16) bank = 0;
        //  lcd.clear();
        //lcd.setCursor(0, 0);
        // showNumber(disp + 1 + bank, 0);
        //lcd.setCursor(9, 0);
        // lcd.print("Bankup");
        //showNumber(7,0);
        // lcd.print(count+=4);
        // flashing = true;
        //delay(50);
        //  if (editmode) {
        //   flashing = false;
        //   lcd.display();
        //   menu++;
        //   Cmenu();
        //  }
        //  lcd.clear();
        //lcd.setCursor (0, 0);
        //lcd.print(editmode);
      }
    }

    // store the current timestamp in previousButtonMillis
    previousButtonMillis = currentMillis;

  }

}

Would it be millis somehow causing this?

or the Ide, im using the latest version.

Hi would someone mind trying this, I'm trying to work out if it's the hardware or code?
It's almost like a debounce with the midi.

On a short button press, it's putting out 6 or 7 midi CC messages and it should only be sending 1.

/* 4-Way Button:  Click, Double-Click, Press+Hold, and Press+Long-Hold Test Sketch

By Jeff Saltzman
Oct. 13, 2009

To keep a physical interface as simple as possible, this sketch demonstrates generating four output events from a single push-button.
1) Click:  rapid press and release
2) Double-Click:  two clicks in quick succession
3) Press and Hold:  holding the button down
4) Long Press and Hold:  holding the button for a long time
*/

#define buttonPin 2        // analog input pin to use as a digital input
#define ledPin1 13          // digital output pin for LED 1
#define ledPin2 13          // digital output pin for LED 2
#define ledPin3 0          // digital output pin for LED 3
#define ledPin4 3          // digital output pin for LED 4

// LED variables
boolean ledVal1 = false;    // state of LED 1
boolean ledVal2 = false;    // state of LED 2
boolean ledVal3 = false;    // state of LED 3
boolean ledVal4 = false;    // state of LED 4

//=================================================

void setup() {
   // Set button input pin
   pinMode(buttonPin, INPUT);
   digitalWrite(buttonPin, HIGH );
   // Set LED output pins
   pinMode(ledPin1, OUTPUT);
   digitalWrite(ledPin1, ledVal1);
   pinMode(ledPin2, OUTPUT);
   digitalWrite(ledPin2, ledVal2);
   pinMode(ledPin3, OUTPUT);   
   digitalWrite(ledPin3, ledVal3);
   pinMode(ledPin4, OUTPUT);   
   digitalWrite(ledPin4, ledVal4);
   
   Serial.begin(31250);
}

void loop() {
   // Get button event and act accordingly
   int b = checkButton();
   if (b == 1) clickEvent();
   if (b == 2) doubleClickEvent();
   if (b == 3) holdEvent();
   if (b == 4) longHoldEvent();
}

//=================================================
// Events to trigger

void clickEvent() {
   ledVal1 = !ledVal1;
   if(ledVal1){
   digitalWrite(ledPin1, ledVal1);
  sendControlChange(90, 127);
   Serial.println("buton");
   } 
}
void doubleClickEvent() {
   ledVal2 = !ledVal2;
   digitalWrite(ledPin2, ledVal2);
   //sendControlChange(82, 127);
}
void holdEvent() {
   ledVal3 = !ledVal3;
   digitalWrite(ledPin3, ledVal3);
}
void longHoldEvent() {
   ledVal4 = !ledVal4;
   digitalWrite(ledPin4, ledVal4);
}
//=================================================
//  MULTI-CLICK:  One Button, Multiple Events

// Button timing variables
int debounce = 20;          // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250;            // max ms between clicks for a double click event
int holdTime = 1000;        // ms hold period: how long to wait for press+hold event
int longHoldTime = 3000;    // ms long hold period: how long to wait for press+hold event

// Button variables
boolean buttonVal = HIGH;   // value read from button
boolean buttonLast = HIGH;  // buffered value of the button's previous state
boolean DCwaiting = false;  // whether we're waiting for a double click (down)
boolean DConUp = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true;    // whether it's OK to do a single click
long downTime = -1;         // time the button was pressed down
long upTime = -1;           // time the button was released
boolean ignoreUp = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false;        // when held, whether to wait for the up event
boolean holdEventPast = false;    // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already

int checkButton() {   
   int event = 0;
   buttonVal = digitalRead(buttonPin);
   // Button pressed down
   if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
 
   {
   
 // i have tryed putting    sendControlChange(82, 0);
  // sendControlChange(90, 0);

       downTime = millis();
       ignoreUp = false;
       waitForUp = false;
       singleOK = true;
       holdEventPast = false;
       
       
       longHoldEventPast = false;
       if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true)  DConUp = true;
       else  DConUp = false;
       DCwaiting = false;
   
   }
   // Button released
   else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
 
   {       
       if (not ignoreUp)
       //Serial.println("butoff");
     
       {
           upTime = millis();
           if (DConUp == false) DCwaiting = true;
           else
           {
               event = 2;
               DConUp = false;
               DCwaiting = false;
               singleOK = false;
               
           }
       }
   }
   // Test for normal click event: DCgap expired
   if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
   {
       event = 1;
       DCwaiting = false;
   }
   // Test for hold
   if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
       // Trigger "normal" hold
       if (not holdEventPast)
       {
           event = 3;
           waitForUp = true;
           ignoreUp = true;
           DConUp = false;
           DCwaiting = false;
           //downTime = millis();
           holdEventPast = true;
       }
       // Trigger "long" hold
       if ((millis() - downTime) >= longHoldTime)
       {
           if (not longHoldEventPast)
           {
               event = 4;
               longHoldEventPast = true;
           }
       }
   }
   buttonLast = buttonVal;
   return event;
}
void sendControlChange(byte Ctrl, byte Value){
  Serial.write(0xB0);
  Serial.write(Ctrl);
  Serial.write(Value);
}

What Arduino board are you using?

Hi Mike, Thanks for the reply.
Arduino Mega.
Ide 1.8.16.

OK so your code doesn't work correctly to distinguish between a Hold and a long hold.
I put some diagnostic prints in it and what happens is that when you hold the button down the checkButton function returns first a value of 3 and then as you continue to hold it down will eventually return a value of 4. So you are getting two events for a long hold. I suspect that is not what you want.

You also have some minor errors in the code, like not putting the INPUT_PULLUP on the pinMode call for the input button. Adding this will mean you don't have to have a pull up resistor. Also your code prints the word "buton" (note it should be spelled button) in the clickEvent function. That will send these characters to the MIDI output which explains why you get random stuff on the MIDI output.

When developing an application for MIDI over the serial port do not immediacy go with Serial.begin(31250) because that will prevent you from doing any debug printing. Better to ignore the MIDI stuff altogether until you have the various functions going and only then switch to the MIDI speed. It is easy to change your
sendControlChange function to print out messages to your serial monitor like this.

void sendControlChange(byte Ctrl, byte Value){
  if( debugToken) {
  Serial.print("Control change message with value ");
  Serial.println(Value);
  }
  else {
  Serial.write(0xB0);
  Serial.write(Ctrl);
  Serial.write(Value);
  }
}

Where the variable debugToken is set to true or false at the start of the code.
This is the code I used to look at the button presses.

void loop() {
   // Get button event and act accordingly
   int b = checkButton();
   if(b != 0){
   Serial.print("got button value ");
   Serial.println(b);
   if (b == 1) clickEvent();
   if (b == 2) doubleClickEvent();
   if (b == 3) holdEvent();
   if (b == 4) longHoldEvent();
   }
}

Having previously set the baud rate to 115200.

The point about distinguishing between a hold and a long hold is that you can only tell which one you have when the button is released.

Many thanks, Mike That makes sense now.

It never even dawned on me that the serial prints were causing that.

Regards

Greg