Getting ghost note_On out of code conditions

Hi,
please look at my code inwhich i am trying a Notes Run UP/DOWN from slide Potentiometer,

     SLIDE1 = map(analogRead(0),50,975,1,14);
               if (analogRead(0)<50) {  SLIDE1=0;  }
               if (analogRead(0)>975) {  SLIDE1=15;  }
               
              if  (SLIDE1 !=SLIDE1_OLD) {
                 if  ((SLIDE1>0) && (SLIDE1<15))          { 
                  Serial.println(SLIDE1);
                     MIDI.sendNoteOff(Last_Note,0,1); 
                    MIDI.sendNoteOn((SLIDE1+59+TRANS_KEY),127,1);
                    Last_Note = SLIDE1+59+TRANS_KEY; 
                  }
            if  (SLIDE1 == 0)           {  MIDI.sendNoteOff(Last_Note,0,1); } 
             if  (SLIDE1 == 15)         {  MIDI.sendNoteOff(Last_Note,0,1); } 
            
           SLD_SCN (SLIDE1);
                SLIDE1_OLD = SLIDE1;
              }

from above code i tried , when Variable "SLIDE1" start upward from 0 . it should hit note_on when it is 1 .and note_off 1 and Note_on 2 when it is at 2......................................... to last note_on at 14 . according to my think it should Note_off 14 , when it go to 15 . using this line
if (SLIDE1 == 15) { MIDI.sendNoteOff(Last_Note,0,1); }
same in Downward , it should start note_on when "Slide1" start from 14 to 1.
it should Note_off Last at 0. using this line
if (SLIDE1 == 0) { MIDI.sendNoteOff(Last_Note,0,1); }
i am also posting video result you can see , my display showing "SLIDE1" changes .when i move full slider to left at 15 or move to Left at 0 , I am hearing holded Lower & high note in Piano sound. Please also watch result with second video of Obey Sound.
Result With Piano
Result with Obey Sound
please check what part of code make hold ghost notes ????
i want to get run start from 1 to 14 and last note of at 15 when upward.
and start from 14 to 1 and last note of at 0 when downward.

At a glance the logic looks plausible.

I'm in transit, I'll try the snippet you posted later.

One small change that should not be your problem would be to read the analog pin but once. Store it in a variable and use that N times, so all decisions are made from the same data.

That's the first thing I will do.

a7

building on what @alto777 said (and applying a bit of formatting)

int val = analogRead(0);
SLIDE1 = map(val, 50, 975, 1, 14);
if (val < 50) {  SLIDE1 = 0;}
else if (val > 975) { SLIDE1 = 15; }

if  (SLIDE1 != SLIDE1_OLD) {
  if  ((SLIDE1 > 0) && (SLIDE1 < 15)) {
    Serial.println(SLIDE1);
    MIDI.sendNoteOff(Last_Note, 0, 1);
    int New_Note = SLIDE1 + 59 + TRANS_KEY; 
    MIDI.sendNoteOn(New_Note, 127, 1);
    Last_Note = New_Note;
  }
  else {
    MIDI.sendNoteOff(Last_Note, 0, 1);
  }

  SLD_SCN (SLIDE1);
  SLIDE1_OLD = SLIDE1;
}

I don't know if this is what @charnjit is seeing and complaining about:

SLIDE1> 13
             note off :  1..14 81
              note on :  1..14 82
     SLD_SCN : 13
SLIDE1> 14
             note off :  1..14 82
              note on :  1..14 83
     SLD_SCN : 14
SLIDE1> 15
             note off :  FIFTEEN 83
     SLD_SCN : 15
SLIDE1> 13
             note off :  1..14 83
              note on :  1..14 82

It look like the logic turns off a note that has already been turned off.

15 on the analog mapped result turns off the right note, then when we slide back, 14 thinks it has to also. The above skipped 14 because I have a throttled loop and ran the slider down too fast, but the same thing would happen.

I think the same thing might Yes, the same thing happens coming out of the 0 slide position.

A variety of solutions suggest themselves, have fun working that out.

But I ask if this is indeed the problem and if so, what is the harm of turning off a MIDI note what has already been turned off?

Play with it here:


My code. Mostly the original logic stripped of MIDI and instrumented so we can see WTF is going on. And placed in the context of an it compiles and runs sketch.

Code:
// https://wokwi.com/projects/426319240866310145

void setup() {
  Serial.begin(115200);
  Serial.println("\nWake Up!\n");

  noteOn(42, "test", 0);
  noteOff(777, "also test", 0);
}

void loop() {
  delay(433);

  wtf();
}

int SLIDE1;
int SLIDE1_OLD = -1;
int lastNote = -1;

# define TRANS_KEY  10

void wtf()
{
  int theReading = analogRead(A0);
//  Serial.print(theReading); Serial.print(" <A0   ");

  int SLIDE1 = map(theReading, 50, 975, 1, 14);

  if (analogRead(0) < 50) {
    SLIDE1 = 0;
  }
  if (analogRead(0) > 975) {
    SLIDE1 = 15;
  }

  if  (SLIDE1 != SLIDE1_OLD) {

    Serial.print("SLIDE1> "); Serial.print(SLIDE1);
    Serial.println("");

    if  ((SLIDE1 > 0) && (SLIDE1 < 15))          {
      noteOff(lastNote, " 1..14 ", 1);
      noteOn((SLIDE1 + 59 + TRANS_KEY), " 1..14 ", 1);
      lastNote = SLIDE1 + 59 + TRANS_KEY;
    }
    if  (SLIDE1 == 0)           {
      noteOff(lastNote, " ZERO ", 1);
    }
    if  (SLIDE1 == 15)         {
      noteOff(lastNote, " FIFTEEN ", 1);
    }

    SLD_SCN (SLIDE1);
    SLIDE1_OLD = SLIDE1;
  }
}

void noteOn(int aNote, char *msg, int)
{
  Serial.print("              note on : "); Serial.print(msg); Serial.println(aNote);
}

void noteOff(int aNote, char *msg, int)
{
  Serial.print("             note off : "); Serial.print(msg); Serial.println(aNote);
}

void SLD_SCN(int wtf)
{
  Serial.print("     SLD_SCN : "); Serial.println(wtf); 
}

a7

Am sorry i forget mention
SLD_SCN (SLIDE1);
funtion used to display current reading of "SLIDE1" from below

         void SLD_SCN (int NUMBER_KEY)   {
  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.print(NUMBER_KEY);
  display.display();
    }

Now i tried code from --- blh64

         int val = analogRead(0);
                 SLIDE1 = map(val,50,975,1,14);
               if (val<50) {  SLIDE1=0;  }
               if (val>975) {  SLIDE1=15;  }
               
              if  (SLIDE1 !=SLIDE1_OLD) {
                 if  ((SLIDE1>0) && (SLIDE1<15))          { 
                  Serial.println(SLIDE1);
                     MIDI.sendNoteOff(Last_Note,0,1); 
                     int New_Note = SLIDE1+59+TRANS_KEY;
                    MIDI.sendNoteOn(New_Note,127,1);
                    Last_Note = New_Note; 
                  }
                    else  {  MIDI.sendNoteOff(Last_Note,0,1); } 
           SLD_SCN (SLIDE1);
                SLIDE1_OLD = SLIDE1;
              }

after uploading above code
when i start moving slider i am getting start sound from New_Note what is (SLIDE1+59+TRANS_KEY) ,,,,,also getting increment with 1 semitone according to Slider position. .... that is good what i want .

but when i fully move slider to SLIDE1 = 15 , Note is holded by (New_Note-3) where (SLIDE1 = 1)
and when i fully move slider back to SLIDE1 = 0 , Note is also holded by (New_Note-3) where (SLIDE1 = 1)
i think command
else { MIDI.sendNoteOff(Last_Note,0,1); }
not working
according to me , when "SLIDE1" is being 0 after 1 , it should switch off Note what is called On by SLIDE1 =1 after 2.
and when "SLIDE1" is being 15 after 14 , it should switch off Note what is called On by SLIDE1 =14 after 13.
SLIDE1 as 0 and 15 Should call Note_off event for 1 and 14 respectively.

I am surprised that why (New_Note-3) is sounding holded at SLIDE1 as 0 or 15 where (New_Note = (1+59+TRANS_KEY) or you can consider a Note number =(59+TRANS_KEY)-2 is hold at fully moved slider left or right.

Take a look at the simulation I linked in #4 above and gather some ideas about how to use serial printing so you will know, rather than think, what is happening with key variables and how they inform flow through your code.

I ignored

 SLD_SCN (SLIDE1);

good thing it has nothing to do with turning on or off any notes.

My simulation works perfectly, aside from the turning off notes that are already. Off.

I applied to a copy of that simulation an easy fix, but again ask if that is some kind of problem. Most times, turning off something that is already off is harmless.

There is no sign in any logic you posted of a reason that more than one note would be on at a time. I have added to my simulation (again, in a copy for now) printing that would say a note got turned on when there was already a note playing.

Perhaps it is time for you to place your own logic in the smallest program possible. At the moment, I would have to say the problem is in the code you aren't posting.

a7

Thanks Alto777 for response.
I could not understand ypur post#4 .
but after many efforts i reached to solution by this code:

    int val = analogRead(0);
                 SLIDE1 = map(val,50,975,1,14);
               if (val<50) {  SLIDE1=0;  }
               if (val>975) {  SLIDE1=15;  }
               
              if  (SLIDE1 !=SLIDE1_OLD) {
                 if  ((SLIDE1>0) && (SLIDE1<15))          { 
                  Serial.println(SLIDE1);
                  for (int ch=0; ch<16; ch++)  {  MIDI.sendNoteOff(Last_Note,0,ch); } 
                     int New_Note = SLIDE1+59 +TRANS_KEY;  // 
                  for (int ch=0; ch<16; ch++)  {  MIDI.sendNoteOn(New_Note,127,ch);  }
                    Last_Note = New_Note; 
                  }
                    else 
                
                    {
                    for (int ch=0; ch<16; ch++) {   MIDI.sendControlChange(120,0,ch);}
                    for (int ch=0; ch<16; ch++)   { MIDI.sendControlChange(123,0,ch); }
                      }  
           SLD_SCN (SLIDE1);
                SLIDE1_OLD = SLIDE1;
              }

thank tried it from yours Ideas. this is working right what i want .when slider is being moved , Note sequence is running . that is good . but here i faced new problem. after every note on progChange(10) event is occuring.
after every changes in "SLIDE1" send ProgChange to 10 that is 11 on my keyboard. it is auto event what i did not wrote in code .
you can see my full sketch


#include <RH_ASK.h>     // ok working RadioHead 1.83
#include <MIDI.h>
#include <Keypad.h>                   
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include<EEPROM.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example  -96,-97,-98,-99

#define KEY_UP    -115
#define KEY_DOWN  -113
#define KEY_ZERO  -114
#define KEY_FIND_SCN  -110
#define A    -106
#define B    -107
#define C    -109
#define D    -108
#define AA    -96
#define BB    -97
#define CC    -98
#define DD    -99
#define PATCH_UP    -126
#define PATCH_DOWN  -127
#define MONO        -125
#define POLY        -124
#define DRUM        -116
#define KEY_128     -128
#define ORANGE_A    -123
#define ORANGE_B    -122
#define BLUE_A      -121
#define BLUE_B      -120
#define Bank_WriteA      -119
#define Bank_WriteB      -118
#define Key_Mode_Sw    -117
#define KEY_128    -128

char *data;    //  RF

const byte ROWS = 16; 
const byte COLS = 10; 
char keys[ROWS][COLS] = {
{1,2,3,4,5,6,7,8,9,10},
{11,12,13,14,15,16,17,18,19,20},
{21,22,23,24,25,26,27,28,29,30},
{31,32,33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48,49,50},
{51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70},
{71,72,73,74,75,76,77,78,79,80},
{81,82,83,84,85,86,87,88,89,90},
{91,92,93,94,95,96,97,98,99,100},
{101,102,103,104,105,106,107,108,109,110},
{111,112,113,114,115,116,117,118,119,120},
{121,122,123,124,125,126,127,128,129,130},
{131,132,133,134,135,136,137,138,139,140},
{141,142,143,144,145,146,147,148,149,150},
{151,152,153,154,155,156,157,158,159,160}
};
byte rowPins[ROWS] = {4,5,6,7,8,9,10,11,12,14,15,16,17,22,23,24}; 
byte colPins[COLS] = {32,33,34,35,36,37,38,39,40,41}; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String msg;
MIDI_CREATE_DEFAULT_INSTANCE();
       byte sysexArray[8] = {0xF0,0x7F,0x7F,0x04,0x04,0x00,50,0xF7}; // 0x28         // byte sysexArray[8] = {0xF0, 0x7F, 0x7F, 0x04, 0x04, 0x0, 0x50, 0xF7};   //   MIDI.sendSysEx(8, sysexArray, true); 
  byte User[8] = {0xB0,0x00,0x55,0x90,0x20,0x00,0xC0,0x00}; 
  RH_ASK driver(2000, 2, A1, 5); // ESP8266 or ESP32: do not use pin 11  rx_2 ,tx_A1

   int UP = 0;
   int DOWN = 0;
  int Bank = 61;
  String  Key_Mode;
  int PATCH = 0;
 int SHIFT1 = 0;
 long COUNT = 0;
 int  TRANS_KEY = 0;
 int  TRANS_KEY_Old = 0;
 int KEY_SHIFT_MODE = 0;
 int NOTE_NUMBER = 0;
 int RAG_NOTE[15] = {49,51,52,54,56,57,59,61,63,64,66,68,69,71,73 };
 int Bells_DRBR[22] = {61,63,64,66,68,69,71,73,75,76,78,80,81,83,85,87,88,90,92,93,95,97 }; // 22 notes

 int SLIDE1 = 0;
 int SLIDE1_OLD = 0;
 int Last_Note = 0;
 int Pitch_Value = 0;
 byte C_Status = 0;
 byte D_Status = 0;
 byte Last_Gear = 0; 

   
  int A001=0,A002=0,A003=0,A004=0,A005=0,A006=0,A007=0,A008=0,A009=0,A010=0;
  int A011=0,A012=0,A013=0,A014=0,A015=0,A016=0,A017=0,A018=0,A019=0,A020=0;
  int A21=0,A22=0,A23=0,A24=0,A25=0,A26=0,A27=0,A28=0,A29=0,A30=0;
  int A31=0,A32=0,A33=0,A34=0,A35=0,A36=0,A37=0,A38=0,A39=0,A40=0;
  int A41=0,A42=0,A43=0,A44=0,A45=0,A46=0,A47=0,A48=0,A49=0,A50=0;
  int A51=0,A52=0,A53=0,A54=0,A55=0,A56=0,A57=0,A58=0,A59=0,A60=0;
  
 int A61=0,A62=0,A63=0,A64=0,A65=0,A66=0,A67=0,A68=0,A69=0,A70=0;
 int A71=0,A72=0,A73=0,A74=0,A75=0,A76=0,A77=0,A78=0,A79=0,A80=0;
 int A81=0,A82=0,A83=0,A84=0,A85=0,A86=0,A87=0,A88=0,A89=0,A90=0;
 int A91=0,A92=0,A93=0,A94=0,A95=0,A96=0,A97=0,A98=0,A99=0,A100=0;
 int A101=0,A102=0,A103=0,A104=0,A105=0,A106=0,A107=0,A108=0,A109=0,A110=0;
 int A111=0,A112=0,A113=0,A114=0,A115=0,A116=0,A117=0,A118=0,A119=0,A120=0;
 int A121=0,A122=0,A123=0,A124=0,A125=0,A126=0,A127=0,A128=0;
 // -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------

void setup() {
     Serial.begin(9600);    // Debugging only
int data = 1;
         if (!driver.init())  // RF
         Serial.println("init failed");  // RF
         

    pinMode(LED_BUILTIN, OUTPUT);
     kpd.setHoldTime(1000); 
    MIDI.begin(4);
     MIDI.turnThruOff();
     digitalWrite(13,LOW);
    if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
 display.display();
 display.clearDisplay();

    msg = "";
    Key_Mode="Bank";
  WEL_COME_SCN ();

  A001 = EEPROM.read(1);  A002 = EEPROM.read(2); A003 = EEPROM.read(3); A004 = EEPROM.read(4); A005 = EEPROM.read(5); 
     A006 = EEPROM.read(6);  A007 = EEPROM.read(7); A008 = EEPROM.read(8); A009 = EEPROM.read(9); A010 = EEPROM.read(10); //-------------
          A011 = EEPROM.read(11);  A012 = EEPROM.read(12); A013 = EEPROM.read(13); A014 = EEPROM.read(14); A015 = EEPROM.read(15); 
     A016 = EEPROM.read(16);  A017 = EEPROM.read(17); A018 = EEPROM.read(18); A019 = EEPROM.read(19); A020 = EEPROM.read(20); //-------------
     A21 = EEPROM.read(21);  A22 = EEPROM.read(22); A23 = EEPROM.read(23); A24 = EEPROM.read(24); A25 = EEPROM.read(25); 
     A26 = EEPROM.read(26);  A27 = EEPROM.read(27); A28 = EEPROM.read(28); A29 = EEPROM.read(29); A30 = EEPROM.read(30); //-------------
          A31 = EEPROM.read(31);  A32 = EEPROM.read(32); A33 = EEPROM.read(33); A34 = EEPROM.read(34); A35 = EEPROM.read(35); 
     A36 = EEPROM.read(36);  A37 = EEPROM.read(37); A38 = EEPROM.read(38); A39 = EEPROM.read(39); A40 = EEPROM.read(40); //-------------
     A41 = EEPROM.read(41);  A42 = EEPROM.read(42); A43 = EEPROM.read(43); A44 = EEPROM.read(44); A45 = EEPROM.read(45); 
     A46 = EEPROM.read(46);  A47 = EEPROM.read(47); A48 = EEPROM.read(48); A49 = EEPROM.read(49); A50 = EEPROM.read(50); //-------------
          A51 = EEPROM.read(51);  A52 = EEPROM.read(52); A53 = EEPROM.read(53); A54 = EEPROM.read(54); A55 = EEPROM.read(55); 
     A56 = EEPROM.read(56);  A57 = EEPROM.read(57); A58 = EEPROM.read(58); A59 = EEPROM.read(59); A60 = EEPROM.read(60); //-------------
 // -----------------------------------------------------------------------------------------------------------------------------   
     A61 = EEPROM.read(61);  A62 = EEPROM.read(62); A63 = EEPROM.read(63); A64 = EEPROM.read(64); A65 = EEPROM.read(65); 
     A66 = EEPROM.read(66);  A67 = EEPROM.read(67); A68 = EEPROM.read(68); A69 = EEPROM.read(69); A70 = EEPROM.read(70); //-------------
          A71 = EEPROM.read(71);  A72 = EEPROM.read(72); A73 = EEPROM.read(73); A74 = EEPROM.read(74); A75 = EEPROM.read(75); 
     A76 = EEPROM.read(76);  A77 = EEPROM.read(77); A78 = EEPROM.read(78); A79 = EEPROM.read(79); A80 = EEPROM.read(80); //------------- 
          A81 = EEPROM.read(81);  A82 = EEPROM.read(82); A83 = EEPROM.read(83); A84 = EEPROM.read(84); A85 = EEPROM.read(85); 
     A86 = EEPROM.read(86);  A87 = EEPROM.read(87); A88 = EEPROM.read(88); A89 = EEPROM.read(89); A90 = EEPROM.read(90); //-------------
          A91 = EEPROM.read(91);  A92 = EEPROM.read(92); A93 = EEPROM.read(93); A94 = EEPROM.read(94); A95 = EEPROM.read(95); 
     A96 = EEPROM.read(96);  A97 = EEPROM.read(97); A98 = EEPROM.read(98); A99 = EEPROM.read(99); A100 = EEPROM.read(100); //-------------
          A101 = EEPROM.read(101);  A102 = EEPROM.read(102); A103 = EEPROM.read(103); A104 = EEPROM.read(104); A105 = EEPROM.read(105); 
     A106 = EEPROM.read(106);  A107 = EEPROM.read(107); A108 = EEPROM.read(108); A109 = EEPROM.read(109); A110 = EEPROM.read(110); //-------------
          A111 = EEPROM.read(111);  A112 = EEPROM.read(112); A113 = EEPROM.read(113); A114 = EEPROM.read(114); A115 = EEPROM.read(115); 
     A116 = EEPROM.read(116);  A117 = EEPROM.read(117); A118 = EEPROM.read(118); A119 = EEPROM.read(119); A120 = EEPROM.read(120); //-------------
      A121 = EEPROM.read(121);  A122 = EEPROM.read(122);  A123 = EEPROM.read(123);  A124 = EEPROM.read(124);  A125 = EEPROM.read(125);  
      A126 = EEPROM.read(126);  A127 = EEPROM.read(127);  A128 = EEPROM.read(128);  
}

void SCN (int gg) {
  display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(gg);
  display.setCursor(50,0);             // Start at top-left corner
  display.println(msg);
  display.setCursor(0,16);             // Start at top-left corner
  display.println(analogRead(A0));
  display.display();
  
}

void loop() {       // @ blh64
               int val = analogRead(0);
                 SLIDE1 = map(val,50,975,1,14);
               if (val<50) {  SLIDE1=0;  }
               if (val>975) {  SLIDE1=15;  }
               
              if  (SLIDE1 !=SLIDE1_OLD) {
                 if  ((SLIDE1>0) && (SLIDE1<15))          { 
                  Serial.println(SLIDE1);
                  for (int ch=0; ch<16; ch++)  {  MIDI.sendNoteOff(Last_Note,0,ch); } 
                     int New_Note = SLIDE1+59 ;  // TRANS_KEY
                  for (int ch=0; ch<16; ch++)  {  MIDI.sendNoteOn(New_Note,127,ch);  }
                    Last_Note = New_Note; 
                  }
                    else 
                
                    {
                    for (int ch=0; ch<16; ch++) {   MIDI.sendControlChange(120,0,ch);}
                    for (int ch=0; ch<16; ch++)   { MIDI.sendControlChange(123,0,ch); }
                    //  MIDI.sendNoteOff(n,0,1); 
                      }  
           SLD_SCN (SLIDE1);
                SLIDE1_OLD = SLIDE1;
              }
          
    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   
        {      int mykey = kpd.key[i].kchar;
            if (( kpd.key[i].stateChanged ) && (mykey < 128 )  &&  (mykey > 0 ) && (mykey != 13))
            {
                switch (kpd.key[i].kstate) {  
                    case PRESSED:
         if     (Key_Mode == "Perform")        {      Perform_Mode (mykey);  MAIN_SCN ();    }   
        else if (Key_Mode == "Bank")        {   Bank_Mode (mykey); }      MAIN_SCN ();
                break;
                    case RELEASED:   
                 //   if (Key_Mode == "Bank")        {   Bank_Mode_Released (mykey); }      MAIN_SCN ();
                break;
                }
            }
            // ----------------------------------------------------------------------------------------------
                  if (( kpd.key[i].stateChanged ) && (mykey == KEY_128)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:  
                     if     (Key_Mode == "Perform")  { PATCH = 127;     MIDI.sendProgramChange(PATCH, 1);       MAIN_SCN (); }
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
               if (( kpd.key[i].stateChanged ) && (mykey == PATCH_UP)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                    PATCH++;    MIDI.sendProgramChange(PATCH, 1);   MAIN_SCN ();
                    if (DOWN==1) {PATCH = PATCH+10;    MIDI.sendProgramChange(PATCH, 1);   MAIN_SCN (); }       UP = 1; 
                break;
                 case HOLD:               break;
                  case RELEASED:     UP = 0;              break;
                }  }
            // ----------------------------------------------------------------------------------------------
                if (( kpd.key[i].stateChanged ) && (mykey == PATCH_DOWN)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                    PATCH--;    MIDI.sendProgramChange(PATCH, 1);   MAIN_SCN ();
                     if (UP==1) {PATCH = PATCH-10;     MIDI.sendProgramChange(PATCH, 1);   MAIN_SCN (); }   DOWN = 1; 
                break;
                  case HOLD:                break;
                  case RELEASED:     DOWN = 0;              break;
                }  }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == KEY_FIND_SCN)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED: 
                       MIDI.sendProgramChange(10, 1);  
                    FIND_SCN ();
                    KEY_SHIFT_MODE = 1;
                break;
                    case RELEASED:
                     KEY_SHIFT_MODE = 0;
                     All_Note_Off ();                  MIDI.sendProgramChange(PATCH, 1);     MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
              if (( kpd.key[i].stateChanged ) && (mykey == KEY_UP)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                    TRANS_KEY++;  MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
                if (( kpd.key[i].stateChanged ) && (mykey == KEY_DOWN)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                    TRANS_KEY--;   MAIN_SCN ();      data = 31;   
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
                    if (( kpd.key[i].stateChanged ) && ((mykey == KEY_ZERO) || (mykey == -112) || (mykey == -111))) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                    TRANS_KEY = 0;
                    MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
              if (( kpd.key[i].stateChanged ) && (mykey == A)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED: 
                     data = 11;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();       
                break;
                 case HOLD:   
                data = 13;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                 case RELEASED:   
                data = 12;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
              if (( kpd.key[i].stateChanged ) && (mykey == B)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
               data = 21;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                 case HOLD:   
               data = 23;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED: 
               data = 22;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
                if (( kpd.key[i].stateChanged ) && (mykey == C)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
              if (D_Status==0)  {  data = 31;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); Last_Gear = 31;}
              if (D_Status==1)  {  data = 10;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); } // Speeed  full
                  C_Status = 1;
                break;
                 case HOLD:   
          //     data = 33;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   
                if (D_Status==0)  {  data = 32;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();   }
              if (D_Status==1)  {  data = 5;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); } // Speed  LOW
                C_Status = 0;
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
                   if (( kpd.key[i].stateChanged ) && (mykey == D)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:    //41
              if (C_Status==0)  {  data = 41;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); Last_Gear = 41;}
              if (C_Status==1)  {  data = 10;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); } // Speeed  full 
               D_Status = 1;
                break;
                 case HOLD:   
            //   data = 43;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   // 42
                  if (C_Status==0)  {  data = 42;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();   }
              if (C_Status==1)  {  data = 5;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); } // Speed  LOW
                 D_Status = 0;
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
               if (( kpd.key[i].stateChanged ) && (mykey == AA)) // --------------------------------------       P
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
              data = 11;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                 case HOLD:   
               data = 13;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   
                data = 12;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == BB)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
               data = 21;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                 case HOLD:   
               data = 23;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   
                 data = 22;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == CC)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                 data = 31;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); Last_Gear = 31;
                break;
                 case HOLD:   
               data = 33;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   
            data = 32;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
              if (( kpd.key[i].stateChanged ) && (mykey == DD)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
              data = 41;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  Last_Gear = 41;
                break;
                 case HOLD:   
               data = 43;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent();  
                break;
                 case RELEASED:   
                data = 42;   driver.send((uint8_t *)&data, sizeof(data));    driver.waitPacketSent(); 
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
               if (( kpd.key[i].stateChanged ) && (mykey == Key_Mode_Sw)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                  if ( Key_Mode=="Perform") { Key_Mode="Bank";  }
                  else  if ( Key_Mode=="Bank") { Key_Mode="Perform";  }   
                  MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
              if (( kpd.key[i].stateChanged ) && (mykey == DRUM)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:   
                  if ( Key_Mode=="Perform") { Key_Mode="Bank";  }
                  else  if ( Key_Mode=="Bank") { Key_Mode="Perform";  }   
                  MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == Bank_WriteA)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:         Write_Bank ();    Key_Mode="Bank";     MAIN_SCN ();       break;
                }   }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == Bank_WriteB)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:         Write_Bank ();   Key_Mode="Bank";     MAIN_SCN ();    break;
                }   }
            // ----------------------------------------------------------------------------------------------
            if (( kpd.key[i].stateChanged ) && (mykey == MONO)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:     for (int i=1; i<11; i++) { MIDI.sendControlChange(126,0,i); } // (cc#,value,channel)           
                    break;     // CC# 126
                }   }
            // ----------------------------------------------------------------------------------------------
            if (( kpd.key[i].stateChanged ) && (mykey == POLY)) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:      for (int i=1; i<11; i++) {  MIDI.sendControlChange(127,0,i); } // (cc#,value,channel)           
                    break;  //  CC#127
                }   }
            // ----------------------------------------------------------------------------------------------
             if (( kpd.key[i].stateChanged ) && (mykey == ORANGE_A )) // --------------------------------------
            {  switch (kpd.key[i].kstate) {  
                    case PRESSED:  
                     MIDI.sendProgramChange(111, 1); 
                 for (int i=21; i>(-1); i--) { 
                  MIDI.sendNoteOn((Bells_DRBR[i]+TRANS_KEY),127,1); 
                 delay(100); 
                 MIDI.sendNoteOff((Bells_DRBR[i]+TRANS_KEY),0,1); }  // (note,velocity,channel)
                        MIDI.sendProgramChange(PATCH, 1);
                         MAIN_SCN ();
                break;
                }  }
            // ----------------------------------------------------------------------------------------------

        }
    }   

        if (MIDI.read())                // Is there a MIDI message incoming ?
    {        COUNT++;
                         //  digitalWrite(13,HIGH);  
                           byte A_TYPE = MIDI.getType();
                           byte A_CHANNEL = MIDI.getChannel();  // channel 
                           byte A_NOTE =  MIDI.getData1();  // note number
                           byte A_VOL = MIDI.getData2(); // velocity
                           
        if ((A_TYPE==144) && (A_CHANNEL == 4)&& (KEY_SHIFT_MODE==1) ) { //                           A_TYPE=144 noteOn    128 NoteOff    ///     
         TRANS_KEY = (A_NOTE-73) + TRANS_KEY_Old;                                  //  ......52 = -12........64 = 0........76 = +12.......COARSE TUNE
                                  
 display.clearDisplay();
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);       display.println(A_TYPE);          display.setCursor(50,0);       display.println(A_CHANNEL);      display.setCursor(90,0);      display.println( A_NOTE);  
  display.setCursor(0,16);       display.println(A_VOL);          display.setCursor(50,16);            display.print( "Turn-");  display.print( COUNT);
  display.display();  
    }   
 /*
   if (A_TYPE==240)
   { display.clearDisplay();
  display.setTextSize(1);           
  display.setTextColor(SSD1306_WHITE);       
  display.setCursor(0,0);       display.print(A_TYPE);   display.print( " -" );          display.print(A_CHANNEL);  display.print( " -" );           display.print( A_NOTE); 
  display.setCursor(0,16);  display.print( "Turn-");  display.print( COUNT);
  display.display();     }     
    */
    
    }
        

   if ( TRANS_KEY != TRANS_KEY_Old )  {

         byte sysexArray[8] = {0xF0,0x7F,0x7F,0x04,0x04,0x00,(TRANS_KEY+64),0xF7};  
             MIDI.sendSysEx(8,sysexArray,true);
             MAIN_SCN ();
    TRANS_KEY_Old = TRANS_KEY;
   }



         ////   if (UP == 1) {PATCH =PATCH+10;    MIDI.sendProgramChange(PATCH, 1); delay  }
          //  if (DOWN == 1) {PATCH--;    MIDI.sendProgramChange(PATCH, 1);  }
            if (PATCH < 0) { PATCH = 127; }
            if (PATCH > 127) { PATCH = 0; }
     
   
}  // End loop      ####################################################################################################################################################
    void  All_Note_Off ()
{
        MIDI.sendControlChange(0,123,0);
         MIDI.sendControlChange(1,123,0);
          MIDI.sendControlChange(2,123,0);
           MIDI.sendControlChange(3,123,0);
            MIDI.sendControlChange(4,123,0);
             MIDI.sendControlChange(5,123,0);
              MIDI.sendControlChange(6,123,0);
               MIDI.sendControlChange(7,123,0);
                MIDI.sendControlChange(8,123,0);
                 MIDI.sendControlChange(9,123,0);
                  MIDI.sendControlChange(10,123,0);
                   MIDI.sendControlChange(11,123,0);
                    MIDI.sendControlChange(12,123,0);
                     MIDI.sendControlChange(13,123,0);
                      MIDI.sendControlChange(14,123,0);
                       MIDI.sendControlChange(15,123,0);
}
  void FIND_SCN () 
  {  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println("Key ??");
  display.display();
  }
    void SCN_SLIDE ()   {
  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(SLIDE1);
  display.display();
    }
      void MAIN_SCN ()   {
  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  if   (TRANS_KEY > 0) { display.print("+");     }
  display.print(TRANS_KEY);
  display.setTextSize(1);
    display.setCursor(60,0);     
  display.print(Key_Mode);
   display.print("__");
    display.print(Bank);
   display.setCursor(70,16); 
   display.print("P- ");    
    display.print(PATCH+1);
  display.display();
    }
          void SLD_SCN (int NUMBER_KEY)   {
  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.print(NUMBER_KEY);
  display.display();
    }
    void function () {

      MIDI.sendControlChange(94,127,1);  // (cc#,value,channel)
    }
     void WEL_COME_SCN ()   {
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println("Roland CH2");
  display.display();  }

  
  void Bank_Mode (int Bank_Key)  {

      if (Bank_Key==1) {  MIDI.sendProgramChange(A001, 1);  Bank = 1;  PATCH = A001; }
    if (Bank_Key==2) {  MIDI.sendProgramChange(A002, 1);  Bank = 2;  PATCH = A002;}
    if (Bank_Key==3) {  MIDI.sendProgramChange(A003, 1);  Bank = 3;  PATCH = A003;}
    if (Bank_Key==4) {  MIDI.sendProgramChange(A004, 1);  Bank = 4;  PATCH = A004;}
    if (Bank_Key==5) {  MIDI.sendProgramChange(A005, 1);  Bank = 5;  PATCH = A005;}
    if (Bank_Key==6) {  MIDI.sendProgramChange(A006, 1);  Bank = 6;  PATCH = A006;}
    if (Bank_Key==7) {  MIDI.sendProgramChange(A007, 1);  Bank = 7;  PATCH = A007;}
    if (Bank_Key==8) {  MIDI.sendProgramChange(A008, 1);  Bank = 8;  PATCH = A008;}
    if (Bank_Key==9) {  MIDI.sendProgramChange(A009, 1);  Bank = 9;  PATCH = A009;}
    if (Bank_Key==10) {  MIDI.sendProgramChange(A010, 1);  Bank = 10;  PATCH = A010;}
    if (Bank_Key==11) {  MIDI.sendProgramChange(A011, 1);  Bank = 11;  PATCH = A011;}
    if (Bank_Key==12) {  MIDI.sendProgramChange(A012, 1);  Bank = 12;  PATCH = A012;}
    if (Bank_Key==13) {  MIDI.sendProgramChange(A013, 1);  Bank = 13;  PATCH = A013;}
    if (Bank_Key==14) {  MIDI.sendProgramChange(A014, 1);  Bank = 14;  PATCH = A014;}
    if (Bank_Key==15) {  MIDI.sendProgramChange(A015, 1);  Bank = 15;  PATCH = A015;}
    if (Bank_Key==16) {  MIDI.sendProgramChange(A016, 1);  Bank = 16;  PATCH = A016;}
    if (Bank_Key==17) {  MIDI.sendProgramChange(A017, 1);  Bank = 17;  PATCH = A017;}
    if (Bank_Key==18) {  MIDI.sendProgramChange(A018, 1);  Bank = 18;  PATCH = A018;}
    if (Bank_Key==19) {  MIDI.sendProgramChange(A019, 1);  Bank = 19;  PATCH = A019;}
    if (Bank_Key==20) {  MIDI.sendProgramChange(A020, 1);  Bank = 20; PATCH = A020;}
    if (Bank_Key==21) {  MIDI.sendProgramChange(A21, 1);  Bank = 21; PATCH = A21;}
    if (Bank_Key==22) {  MIDI.sendProgramChange(A22, 1);  Bank = 22; PATCH = A22;}
    if (Bank_Key==23) {  MIDI.sendProgramChange(A23, 1);  Bank = 23; PATCH = A23;}
    if (Bank_Key==24) {  MIDI.sendProgramChange(A24, 1);  Bank = 24; PATCH = A24;}
    if (Bank_Key==25) {  MIDI.sendProgramChange(A25, 1);  Bank = 25; PATCH = A25;}
    if (Bank_Key==26) {  MIDI.sendProgramChange(A26, 1);  Bank = 26; PATCH = A26;}
    if (Bank_Key==27) {  MIDI.sendProgramChange(A27, 1);  Bank = 27; PATCH = A27;}
    if (Bank_Key==28) {  MIDI.sendProgramChange(A28, 1);  Bank = 28; PATCH = A28;}
    if (Bank_Key==29) {  MIDI.sendProgramChange(A29, 1);  Bank = 29; PATCH = A29;}
    if (Bank_Key==30) {  MIDI.sendProgramChange(A30, 1);  Bank = 30; PATCH = A30;}
    if (Bank_Key==31) {  MIDI.sendProgramChange(A31, 1);  Bank = 31; PATCH = A31;}
    if (Bank_Key==32) {  MIDI.sendProgramChange(A32, 1);  Bank = 32; PATCH = A32;}
    if (Bank_Key==33) {  MIDI.sendProgramChange(A33, 1);  Bank = 33; PATCH = A33;}
    if (Bank_Key==34) {  MIDI.sendProgramChange(A34, 1);  Bank = 34; PATCH = A34;}
    if (Bank_Key==35) {  MIDI.sendProgramChange(A35, 1);  Bank = 35; PATCH = A35;}
    if (Bank_Key==36) {  MIDI.sendProgramChange(A36, 1);  Bank = 36; PATCH = A36;}
    if (Bank_Key==37) {  MIDI.sendProgramChange(A37, 1);  Bank = 37; PATCH = A37;}
    if (Bank_Key==38) {  MIDI.sendProgramChange(A38, 1);  Bank = 38; PATCH = A38;}
    if (Bank_Key==39) {  MIDI.sendProgramChange(A39, 1);  Bank = 39; PATCH = A39;}
    if (Bank_Key==40) {  MIDI.sendProgramChange(A40, 1);  Bank = 40; PATCH = A40;}
    if (Bank_Key==41) {  MIDI.sendProgramChange(A41, 1);  Bank = 41; PATCH = A41;}
    if (Bank_Key==42) {  MIDI.sendProgramChange(A42, 1);  Bank = 42; PATCH = A42 ;}
    if (Bank_Key==43) {  MIDI.sendProgramChange(A43, 1);  Bank = 43; PATCH = A43 ;}
    if (Bank_Key==44) {  MIDI.sendProgramChange(A44, 1);  Bank = 44; PATCH = A44 ;}
    if (Bank_Key==45) {  MIDI.sendProgramChange(A45, 1);  Bank = 45; PATCH = A45 ;}
    if (Bank_Key==46) {  MIDI.sendProgramChange(A46, 1);  Bank = 46; PATCH = A46 ;}
    if (Bank_Key==47) {  MIDI.sendProgramChange(A47, 1);  Bank = 47; PATCH = A47 ;}
    if (Bank_Key==48) {  MIDI.sendProgramChange(A48, 1);  Bank = 48; PATCH = A48 ;}
    if (Bank_Key==49) {  MIDI.sendProgramChange(A49, 1);  Bank = 49; PATCH = A49 ;}
    if (Bank_Key==50) {  MIDI.sendProgramChange(A50, 1);  Bank = 50; PATCH = A50 ;}
    if (Bank_Key==51) {  MIDI.sendProgramChange(A51, 1);  Bank = 51; PATCH = A51 ;}
    if (Bank_Key==52) {  MIDI.sendProgramChange(A52, 1);  Bank = 52; PATCH = A51 ;}
    if (Bank_Key==53) {  MIDI.sendProgramChange(A53, 1);  Bank = 53; PATCH = A53 ;}
    if (Bank_Key==54) {  MIDI.sendProgramChange(A54, 1);  Bank = 54; PATCH = A54 ;}
    if (Bank_Key==55) {  MIDI.sendProgramChange(A55, 1);  Bank = 55; PATCH = A55 ;}
    if (Bank_Key==56) {  MIDI.sendProgramChange(A56, 1);  Bank = 56; PATCH = A56 ;}
    if (Bank_Key==57) {  MIDI.sendProgramChange(A57, 1);  Bank = 57; PATCH = A57 ;}
    if (Bank_Key==58) {  MIDI.sendProgramChange(A58, 1);  Bank = 58; PATCH = A58 ;}
    if (Bank_Key==59) {  MIDI.sendProgramChange(A59, 1);  Bank = 59; PATCH = A59 ;}
    if (Bank_Key==60) {  MIDI.sendProgramChange(A60, 1);  Bank = 60; PATCH = A60 ;}
    
    if (Bank_Key==61) {  MIDI.sendProgramChange(A61, 1);  Bank = 61; PATCH = A61;}
     if (Bank_Key==62) {  MIDI.sendProgramChange(A62, 1);  Bank = 62; PATCH = A62;}
      if (Bank_Key==63) {  MIDI.sendProgramChange(A63, 1);  Bank = 63; PATCH = A63;}
       if (Bank_Key==64) {  MIDI.sendProgramChange(A64, 1);  Bank = 64; PATCH = A64;}
        if (Bank_Key==65) {  MIDI.sendProgramChange(A65, 1);  Bank = 65; PATCH = A65;}
         if (Bank_Key==66) {  MIDI.sendProgramChange(A66, 1);  Bank = 66; PATCH = A66; }
          if (Bank_Key==67) {  MIDI.sendProgramChange(A67, 1);  Bank = 67; PATCH = A67;}
           if (Bank_Key==68) {  MIDI.sendProgramChange(A68, 1);  Bank = 68; PATCH = A68;}
            if (Bank_Key==69) {  MIDI.sendProgramChange(A69, 1);  Bank = 69; PATCH = A69;}
             if (Bank_Key==70) {  MIDI.sendProgramChange(A70, 1);  Bank = 70; PATCH = A70;}
              if (Bank_Key==71) {  MIDI.sendProgramChange(A71, 1);  Bank = 71; PATCH = A71;}
               if (Bank_Key==72) {  MIDI.sendProgramChange(A72, 1);  Bank = 72; PATCH = A72;}
                if (Bank_Key==73) {  MIDI.sendProgramChange(A73, 1);  Bank = 73; PATCH = A73;}
                 if (Bank_Key==74) {  MIDI.sendProgramChange(A74, 1);  Bank = 74; PATCH = A74;}
                  if (Bank_Key==75) {  MIDI.sendProgramChange(A75, 1);  Bank = 75; PATCH = A75;}
                   if (Bank_Key==76) {  MIDI.sendProgramChange(A76, 1);  Bank = 76; PATCH = A76;}
                    if (Bank_Key==77) {  MIDI.sendProgramChange(A77, 1);  Bank = 77; PATCH = A77;}
                     if (Bank_Key==78) {  MIDI.sendProgramChange(A78, 1);  Bank = 78; PATCH = A78;}
                      if (Bank_Key==79) {  MIDI.sendProgramChange(A79, 1);  Bank = 79; PATCH = A79;}
                       if (Bank_Key==80) {  MIDI.sendProgramChange(A80, 1);  Bank = 80; PATCH = A80;}
                        if (Bank_Key==81) {  MIDI.sendProgramChange(A81, 1);  Bank = 81; PATCH = A81;}
                         if (Bank_Key==82) {  MIDI.sendProgramChange(A82, 1);  Bank = 82; PATCH = A82;}
                          if (Bank_Key==83) {  MIDI.sendProgramChange(A83, 1);  Bank = 83; PATCH = A83;}  //////////////
                           if (Bank_Key==84) {  MIDI.sendProgramChange(A84, 1);  Bank = 84; PATCH = A84;}
                            if (Bank_Key==85) {  MIDI.sendProgramChange(A85, 1);  Bank = 85; PATCH = A85;}
                             if (Bank_Key==86) {  MIDI.sendProgramChange(A86, 1);  Bank = 86; PATCH = A86;}
                              if (Bank_Key==87) {  MIDI.sendProgramChange(A87, 1);  Bank = 87; PATCH = A87;}
                               if (Bank_Key==88) {  MIDI.sendProgramChange(A88, 1);  Bank = 88; PATCH = A88;}
                                if (Bank_Key==89) {  MIDI.sendProgramChange(A89, 1);  Bank = 89; PATCH = A89;}
                                 if (Bank_Key==90) {  MIDI.sendProgramChange(A90, 1);  Bank = 90; PATCH = A90;}
                                  if (Bank_Key==91) {  MIDI.sendProgramChange(A91, 1);  Bank = 91; PATCH = A91;}
                                   if (Bank_Key==92) {  MIDI.sendProgramChange(A92, 1);  Bank = 92; PATCH = A92;}
                                    if (Bank_Key==93) {  MIDI.sendProgramChange(A93, 1);  Bank = 93; PATCH = A93;}
                                     if (Bank_Key==94) {  MIDI.sendProgramChange(A94, 1);  Bank = 94; PATCH = A94;}
                                      if (Bank_Key==95) {  MIDI.sendProgramChange(A95, 1);  Bank = 95; PATCH = A95;}
                                       if (Bank_Key==96) {  MIDI.sendProgramChange(A96, 1);  Bank = 96; PATCH = A96;}
                                        if (Bank_Key==97) {  MIDI.sendProgramChange(A97, 1);  Bank = 97; PATCH = A97;}
                                         if (Bank_Key==98) {  MIDI.sendProgramChange(A98, 1);  Bank = 98; PATCH = A98;}
                                          if (Bank_Key==99) {  MIDI.sendProgramChange(A99, 1);  Bank = 99; PATCH = A99;}
                                           if (Bank_Key==100) {  MIDI.sendProgramChange(A100, 1);  Bank = 100; PATCH = A100;}
                                            if (Bank_Key==101) {  MIDI.sendProgramChange(A101, 1);  Bank = 101; PATCH = A101;}
                                             if (Bank_Key==102) {  MIDI.sendProgramChange(A102, 1);  Bank = 102; PATCH = A102;}
                                              if (Bank_Key==103) {  MIDI.sendProgramChange(A103, 1);  Bank = 103; PATCH = A103;}
                                               if (Bank_Key==104) {  MIDI.sendProgramChange(A104, 1);  Bank = 104; PATCH = A104;}
                                                if (Bank_Key==105) {  MIDI.sendProgramChange(A105, 1);  Bank = 105; PATCH = A105; }
                                                 if (Bank_Key==106) {  MIDI.sendProgramChange(A106, 1);  Bank = 106; PATCH = A106; }
                                                  if (Bank_Key==107) {  MIDI.sendProgramChange(A107, 1);  Bank = 107; PATCH = A107; }
                                                   if (Bank_Key==108) {  MIDI.sendProgramChange(A108, 1);  Bank = 108; PATCH = A108;}
                                                    if (Bank_Key==109) {  MIDI.sendProgramChange(A109, 1);  Bank = 109; PATCH = A109;}
                                                     if (Bank_Key==110) {  MIDI.sendProgramChange(A110, 1);  Bank = 110; PATCH = A110;}
                                                      if (Bank_Key==111) {  MIDI.sendProgramChange(A111, 1);  Bank = 111; PATCH = A111;}
                                                       if (Bank_Key==112) {  MIDI.sendProgramChange(A112, 1);  Bank = 112; PATCH = A112;}
                                                        if (Bank_Key==113) {  MIDI.sendProgramChange(A113, 1);  Bank = 113; PATCH = A113;}
                                                         if (Bank_Key==114) {  MIDI.sendProgramChange(A114, 1);  Bank = 114; PATCH = A114;}
                                                          if (Bank_Key==115) {  MIDI.sendProgramChange(A115, 1);  Bank = 115; PATCH = A115;}
                                                           if (Bank_Key==116) {  MIDI.sendProgramChange(A116, 1);  Bank = 116; PATCH = A116;}
                                                            if (Bank_Key==117) {  MIDI.sendProgramChange(A117, 1);  Bank = 117; PATCH = A117;}
                                                             if (Bank_Key==118) {  MIDI.sendProgramChange(A118, 1);  Bank = 118; PATCH = A118;}
                                                                if (Bank_Key==119) {  MIDI.sendProgramChange(A119, 1);  Bank = 119; PATCH = A119;}
                                                                  if (Bank_Key ==120) {  MIDI.sendProgramChange(A120, 1);  Bank = 120; PATCH = A120;}
                                                                  if (Bank_Key ==121) {  MIDI.sendProgramChange(A121, 1);  Bank = 121; PATCH = A121;}
                                                                  if (Bank_Key ==122) {  MIDI.sendProgramChange(A122, 1);  Bank = 122; PATCH = A122;}
                                                                  if (Bank_Key ==123) {  MIDI.sendProgramChange(A123, 1);  Bank = 123; PATCH = A123;}
                                                                  if (Bank_Key ==124) {  MIDI.sendProgramChange(A124, 1);  Bank = 124; PATCH = A124;}
                                                                  if (Bank_Key ==125) {  MIDI.sendProgramChange(A125, 1);  Bank = 125; PATCH = A125;}
                                                                  if (Bank_Key ==126) {  MIDI.sendProgramChange(A126, 1);  Bank = 126; PATCH = A126;}
                                                                  if (Bank_Key ==127) {  MIDI.sendProgramChange(A127, 1);  Bank = 127; PATCH = A127;}
                                                                  if (Bank_Key ==128) {  MIDI.sendProgramChange(A128, 1);  Bank = 128; PATCH = A128;}               
                                                                                         }
         void Perform_Mode (int mykey)
         {PATCH = mykey-1;  MIDI.sendProgramChange(PATCH, 1);}                                                                                 
         
          void  Write_Bank () {
                 if (Bank==1) {   EEPROM.write(1, PATCH);  A001 = PATCH;  }
                 if (Bank==2) {   EEPROM.write(2, PATCH);  A002 = PATCH;  }
                 if (Bank==3) {   EEPROM.write(3, PATCH);  A003 = PATCH;  }
                 if (Bank==4) {   EEPROM.write(4, PATCH);  A004 = PATCH;  }
                 if (Bank==5) {   EEPROM.write(5, PATCH);  A005 = PATCH;  }
                 if (Bank==6) {   EEPROM.write(6, PATCH);  A006 = PATCH;  }
                 if (Bank==7) {   EEPROM.write(7, PATCH);  A007 = PATCH;  }
                 if (Bank==8) {   EEPROM.write(8, PATCH);  A008 = PATCH;  }
                 if (Bank==9) {   EEPROM.write(9, PATCH);  A009 = PATCH;  }
                 if (Bank==10) {   EEPROM.write(10, PATCH);  A010 = PATCH;  } // -----
                 if (Bank==11) {   EEPROM.write(11, PATCH);  A011 = PATCH;  }
                 if (Bank==12) {   EEPROM.write(12, PATCH);  A012 = PATCH;  }
                 if (Bank==13) {   EEPROM.write(13, PATCH);  A013 = PATCH;  }
                 if (Bank==14) {   EEPROM.write(14, PATCH);  A014 = PATCH;  }
                 if (Bank==15) {   EEPROM.write(15, PATCH);  A015 = PATCH;  }
                 if (Bank==16) {   EEPROM.write(16, PATCH);  A016 = PATCH;  }
                 if (Bank==17) {   EEPROM.write(17, PATCH);  A017 = PATCH;  }
                 if (Bank==18) {   EEPROM.write(18, PATCH);  A018 = PATCH;  }
                 if (Bank==19) {   EEPROM.write(19, PATCH);  A019 = PATCH;  }
                 if (Bank==20) {   EEPROM.write(20, PATCH);  A020 = PATCH;  }
                 if (Bank==21) {   EEPROM.write(21, PATCH);  A21 = PATCH;  }
                 if (Bank==22) {   EEPROM.write(22, PATCH);  A22 = PATCH;  }
                 if (Bank==23) {   EEPROM.write(23, PATCH);  A23 = PATCH;  }
                 if (Bank==24) {   EEPROM.write(24, PATCH);  A24 = PATCH;  }
                 if (Bank==25) {   EEPROM.write(25, PATCH);  A25 = PATCH;  }
                 if (Bank==26) {   EEPROM.write(26, PATCH);  A26 = PATCH;  }
                 if (Bank==27) {   EEPROM.write(27, PATCH);  A27 = PATCH;  }
                 if (Bank==28) {   EEPROM.write(28, PATCH);  A28 = PATCH;  }
                 if (Bank==29) {   EEPROM.write(29, PATCH);  A29 = PATCH;  }
                 if (Bank==30) {   EEPROM.write(30, PATCH);  A30 = PATCH;  }
                 if (Bank==31) {   EEPROM.write(31, PATCH);  A31 = PATCH;  }
                 if (Bank==32) {   EEPROM.write(32, PATCH);  A32 = PATCH;  }
                 if (Bank==33) {   EEPROM.write(33, PATCH);  A33 = PATCH;  }
                 if (Bank==34) {   EEPROM.write(34, PATCH);  A34 = PATCH;  }
                 if (Bank==35) {   EEPROM.write(35, PATCH);  A35 = PATCH;  }
                 if (Bank==36) {   EEPROM.write(36, PATCH);  A36 = PATCH;  }
                 if (Bank==37) {   EEPROM.write(37, PATCH);  A37 = PATCH;  }
                 if (Bank==38) {   EEPROM.write(38, PATCH);  A38 = PATCH;  }
                 if (Bank==39) {   EEPROM.write(39, PATCH);  A39 = PATCH;  }
                 if (Bank==40) {   EEPROM.write(40, PATCH);  A40 = PATCH;  }
                 if (Bank==41) {   EEPROM.write(41, PATCH);  A41 = PATCH;  }
                 if (Bank==42) {   EEPROM.write(42, PATCH);  A42 = PATCH;  }
                 if (Bank==43) {   EEPROM.write(43, PATCH);  A43 = PATCH;  }
                 if (Bank==44) {   EEPROM.write(44, PATCH);  A44 = PATCH;  }
                 if (Bank==45) {   EEPROM.write(45, PATCH);  A45 = PATCH;  }
                 if (Bank==46) {   EEPROM.write(46, PATCH);  A46 = PATCH;  }
                 if (Bank==47) {   EEPROM.write(47, PATCH);  A47 = PATCH;  }
                 if (Bank==48) {   EEPROM.write(48, PATCH);  A48 = PATCH;  }
                 if (Bank==49) {   EEPROM.write(49, PATCH);  A49 = PATCH;  }
                 if (Bank==50) {   EEPROM.write(50, PATCH);  A50 = PATCH;  }
                 if (Bank==51) {   EEPROM.write(51, PATCH);  A51 = PATCH;  }
                 if (Bank==52) {   EEPROM.write(52, PATCH);  A52 = PATCH;  }
                 if (Bank==53) {   EEPROM.write(53, PATCH);  A53 = PATCH;  }
                 if (Bank==54) {   EEPROM.write(54, PATCH);  A54 = PATCH;  }
                 if (Bank==55) {   EEPROM.write(55, PATCH);  A55 = PATCH;  }
                 if (Bank==56) {   EEPROM.write(56, PATCH);  A56 = PATCH;  }
                 if (Bank==57) {   EEPROM.write(57, PATCH);  A57 = PATCH;  }
                 if (Bank==58) {   EEPROM.write(58, PATCH);  A58 = PATCH;  }
                 if (Bank==59) {   EEPROM.write(59, PATCH);  A59 = PATCH;  }
                 if (Bank==60) {   EEPROM.write(60, PATCH);  A60 = PATCH;  }
             
             if (Bank==61) {   EEPROM.write(61, PATCH);  A61 = PATCH;  }  //write A61 address PATCH_Number  
               if (Bank==62) {   EEPROM.write(62, PATCH);  A62 = PATCH;  }
                 if (Bank==63) {   EEPROM.write(63, PATCH);  A63 = PATCH;  }
                   if (Bank==64) {   EEPROM.write(64, PATCH);  A64 = PATCH;  }
                     if (Bank==65) {   EEPROM.write(65, PATCH);  A65 = PATCH ; }
                       if (Bank==66) {   EEPROM.write(66, PATCH);  A66 = PATCH;  }
                         if (Bank==67) {   EEPROM.write(67, PATCH);  A67 = PATCH;  }
                           if (Bank==68) {   EEPROM.write(68, PATCH);  A68 = PATCH;  }
                             if (Bank==69) {   EEPROM.write(69, PATCH);  A69 = PATCH;  }
                               if (Bank==70) {   EEPROM.write(70, PATCH);  A70 = PATCH;  }
                               if (Bank==71) {   EEPROM.write(71, PATCH);  A71 = PATCH;  }
                               if (Bank==72) {   EEPROM.write(72, PATCH);  A72 = PATCH;  }
                               if (Bank==73) {   EEPROM.write(73, PATCH);  A73 = PATCH;  }
                               if (Bank==74) {   EEPROM.write(74, PATCH);  A74 = PATCH;  }
                               if (Bank==75) {   EEPROM.write(75, PATCH);  A75 = PATCH;  }
                               if (Bank==76) {   EEPROM.write(76, PATCH);  A76 = PATCH;  }
                               if (Bank==77) {   EEPROM.write(77, PATCH);  A77 = PATCH;  }
                               if (Bank==78) {   EEPROM.write(78, PATCH);  A78 = PATCH;  }
                               if (Bank==79) {   EEPROM.write(79, PATCH);  A79 = PATCH;  }
                               if (Bank==80) {   EEPROM.write(80, PATCH);  A80 = PATCH;  }
                               if (Bank==81) {   EEPROM.write(81, PATCH);  A81 = PATCH;  }
                               if (Bank==82) {   EEPROM.write(82, PATCH);  A82 = PATCH;  }
                               if (Bank==83) {   EEPROM.write(83, PATCH);  A83 = PATCH;  }
                               if (Bank==84) {   EEPROM.write(84, PATCH);  A84 = PATCH;  }
                               if (Bank==85) {   EEPROM.write(85, PATCH);  A85 = PATCH;  }
                               if (Bank==86) {   EEPROM.write(86, PATCH);  A86 = PATCH;  }
                               if (Bank==87) {   EEPROM.write(87, PATCH);  A87 = PATCH;  }
                               if (Bank==88) {   EEPROM.write(88, PATCH);  A88 = PATCH;  }
                               if (Bank==89) {   EEPROM.write(89, PATCH);  A89 = PATCH;  }
                               if (Bank==90) {   EEPROM.write(90, PATCH);  A90 = PATCH;  }
                               if (Bank==91) {   EEPROM.write(91, PATCH);  A91 = PATCH;  }
                               if (Bank==92) {   EEPROM.write(92, PATCH);  A92 = PATCH;  }
                               if (Bank==93) {   EEPROM.write(93, PATCH);  A93 = PATCH;  }
                               if (Bank==94) {   EEPROM.write(94, PATCH);  A94 = PATCH;  }
                               if (Bank==95) {   EEPROM.write(95, PATCH);  A95 = PATCH;  }
                               if (Bank==96) {   EEPROM.write(96, PATCH);  A96 = PATCH;  }
                               if (Bank==97) {   EEPROM.write(97, PATCH);  A97 = PATCH;  }
                               if (Bank==98) {   EEPROM.write(98, PATCH);  A98 = PATCH;  }
                               if (Bank==99) {   EEPROM.write(99, PATCH);  A99 = PATCH;  }
                               if (Bank==100) {   EEPROM.write(100, PATCH);  A100 = PATCH;  }
                               if (Bank==101) {   EEPROM.write(101, PATCH);  A101 = PATCH;  }
                               if (Bank==102) {   EEPROM.write(102, PATCH);  A102 = PATCH;  }
                               if (Bank==103) {   EEPROM.write(103, PATCH);  A103 = PATCH;  }
                               if (Bank==104) {   EEPROM.write(104, PATCH);  A104 = PATCH;  }
                               if (Bank==105) {   EEPROM.write(105, PATCH);  A105 = PATCH;  }
                               if (Bank==106) {   EEPROM.write(106, PATCH);  A106 = PATCH;  }
                               if (Bank==107) {   EEPROM.write(107, PATCH);  A107 = PATCH;  }
                               if (Bank==108) {   EEPROM.write(108, PATCH);  A108 = PATCH;  }
                               if (Bank==109) {   EEPROM.write(109, PATCH);  A109 = PATCH;  }
                               if (Bank==110) {   EEPROM.write(110, PATCH);  A110 = PATCH;  }
                                if (Bank==111) {   EEPROM.write(111, PATCH);  A111 = PATCH;  } 
                                if (Bank==112) {   EEPROM.write(112, PATCH);  A112 = PATCH;  } 
                                if (Bank==113) {   EEPROM.write(113, PATCH);  A113 = PATCH;  } 
                                if (Bank==114) {   EEPROM.write(114, PATCH);  A114 = PATCH;  } 
                                if (Bank==115) {   EEPROM.write(115, PATCH);  A115 = PATCH;  } 
                                if (Bank==116) {   EEPROM.write(116, PATCH);  A116 = PATCH;  } 
                                if (Bank==117) {   EEPROM.write(117, PATCH);  A117 = PATCH;  } 
                                if (Bank==118) {   EEPROM.write(118, PATCH);  A118 = PATCH;  } 
                                if (Bank==119) {   EEPROM.write(119, PATCH);  A119 = PATCH;  } 
                                if (Bank==120) {   EEPROM.write(120, PATCH);  A120 = PATCH;  }  
                                Write_Bank_SCN (); 
                                delay(1000);   MAIN_SCN ();                                                                         
           }
           void  Bank_Mode_Released (int Bank_key)   {          
            
           }
            void  Write_Bank_SCN ()  {
   display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
    display.print("Write Done");
  display.display();
              
            }
             

please check why progChange(10) is occur.

Looks like you need to learn more about MIDI.

And what's with all the loops over 16 channels? Are you just throwing spaghetti at this to see what sticks?

a7

May be I don't know what occurs ?? when same midi message is sent over 16 channels.
Is there a rule in midi?? That if note on message is sent over 16 channels, prog Change will be automatically generate??
Please clear it.
I called it over 16 channels because of I will play it from my keyboard in perform mode. And I want to play notes run at same patch what is already selected.
Please reply if you seeing something wrong in my code

I strongly recommend you to read more about arrays...
Your programming style has a lot of repetition.
The code us difficult to follow, difficult to change and small errors may be overlooked easily.