help to make tft touchscreen piano

hi, i am trying to make a simple touchscreen MIDI+BLEMIDI piano. I made connection between my RA8875 4.3inch display and ESP32 mini kit. Display working good, examples working. I draw rectangles like piano and try to make touch keys, but when i press touchscreen on any touch key, my devise send a lot of noteOn to my laptop, and not noteOff. I dont have "continious" play of my note but have something like "vibrato". In serial terminal after touching a touchscreen i have a lot of data of x and y points. I dont know how to make my device work like that: press the first key - send noteOn, unpress the first key - send noteOff. I am new in programming. help. thats my code:

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#include <MIDI.h>
#include "BleMidi.h"
BLEMIDI_CREATE_INSTANCE(bm);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
#define RA8875_INT 26
#define RA8875_CS 5 //SPI CS GPIO5
#define RA8875_RESET 25
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;
int CurrentNote = 0;
int PreviousNote = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("RA8875 start");
  //MIDI.begin();
  bm.begin("Blecoder_old");
  bm.onConnected(OnBleMidiConnected);
  bm.onDisconnected(OnBleMidiDisconnected);
  Serial.println(F("looping"));
  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_480x272)) {
    Serial.println("RA8875 Not Found!");
    while (1);
  }

  Serial.println("Found RA8875");
  tft.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);
  pinMode(RA8875_INT, INPUT);
  digitalWrite(RA8875_INT, HIGH);
  tft.touchEnable(true);
  Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
  Serial.println("Waiting for touch events ...");
}

void loop()
{

  Piano();
  float xScale = 1024.0F / tft.width();
  float yScale = 1024.0F / tft.height();

  /* Wait around for touch events */
  if (! digitalRead(RA8875_INT))
  {

    if (tft.touched())  {
      tft.touchRead(&tx, &ty);
      delay(1);

      //code

      if ((tx >= 50) && (tx <= 150) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 50;

      }
      if ((tx >= 160) && (tx <= 250) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 52;
      }
      if ((tx >= 270) && (tx <= 360) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 54;
      }


      bm.sendNoteOn(CurrentNote , 127, 1);
      MIDI.sendNoteOn(CurrentNote, 127, 1);
      PreviousNote = CurrentNote;
    }


    else  {
      bm.sendNoteOff(PreviousNote , 127, 1);
      MIDI.sendNoteOff(PreviousNote, 127, 1);

    }


  }
}
void OnBleMidiConnected() {
  Serial.println(F("Connected"));
}

void OnBleMidiDisconnected() {
  Serial.println(F("Disconnected"));
}

void Piano() {

  tft.fillRect(0, 150, 50, 270, RA8875_WHITE);
  tft.fillRect(55, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(110, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(165, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(220, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(275, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(330, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(385, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(440, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(0, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(75, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(130, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(165, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(240, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(295, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(350, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(385, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(465, 0, 15, 150, RA8875_WHITE);

}
[code]

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Isn't a midi noteOff the same as a noteOn with velocity 0 ?

Deva_Rishi:
Isn't a midi noteOff the same as a noteOn with velocity 0 ?

no, noteOff is different.

Try serializing your issues. Get the "piano" to work first.

what happens when you run this code (open Serial Monitor @115200 bauds):

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#define RA8875_INT 26
#define RA8875_CS 5 //SPI CS GPIO5
#define RA8875_RESET 25
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);

uint16_t tx, ty;
uint8_t CurrentNote = 0;

void drawPiano() {
  tft.fillRect(0, 150, 50, 270, RA8875_WHITE);
  tft.fillRect(55, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(110, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(165, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(220, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(275, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(330, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(385, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(440, 150 , 50 , 270, RA8875_WHITE);
  tft.fillRect(0, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(75, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(130, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(165, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(240, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(295, 0, 10, 150, RA8875_WHITE);
  tft.fillRect(350, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(385, 0, 30, 150, RA8875_WHITE);
  tft.fillRect(465, 0, 15, 150, RA8875_WHITE);
}


void setup()
{
  Serial.begin(115200);
  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_480x272)) {
    Serial.println("RA8875 Not Found!");
    while (1);
  }

  pinMode(RA8875_INT, INPUT_PULLUP);
  tft.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);
  tft.touchEnable(true);

  drawPiano();

  Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
  Serial.println("Waiting for touch events ...");
}

void loop()
{
  if (digitalRead(RA8875_INT) == LOW) {/* Wait around for a new touch event (INT pin goes low) */
    if (tft.touched())  {  /* Make sure this is really a touch event */
      tft.touchRead(&tx, &ty);

      if ((tx >= 50) && (tx <= 150) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 50;
      } else if ((tx >= 160) && (tx <= 250) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 52;
      } else if ((tx >= 270) && (tx <= 360) && (ty >= 120) && (ty <= 450)) {
        CurrentNote = 54;
      } else {
        CurrentNote = 0;
      }
      
      Serial.print(F("Touched "));
      Serial.print(tx);
      Serial.print(F(","));
      Serial.print(ty);
      Serial.print(F("\t"));
      Serial.println(CurrentNote);
    } else  {
      Serial.println(F("RA8875_INT NOT TOUCHED "));
    }
  }
}

(this is basically your code from which I removed all the midi stuff and added a few else and corrected CurrentNote type)

J-M-L:
Try serializing your issues. Get the "piano" to work first.

what happens when you run this code (open Serial Monitor @115200 bauds):

#include <SPI.h>

#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#define RA8875_INT 26
#define RA8875_CS 5 //SPI CS GPIO5
#define RA8875_RESET 25
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);

uint16_t tx, ty;
uint8_t CurrentNote = 0;

void drawPiano() {
 tft.fillRect(0, 150, 50, 270, RA8875_WHITE);
 tft.fillRect(55, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(110, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(165, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(220, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(275, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(330, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(385, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(440, 150 , 50 , 270, RA8875_WHITE);
 tft.fillRect(0, 0, 30, 150, RA8875_WHITE);
 tft.fillRect(75, 0, 10, 150, RA8875_WHITE);
 tft.fillRect(130, 0, 30, 150, RA8875_WHITE);
 tft.fillRect(165, 0, 30, 150, RA8875_WHITE);
 tft.fillRect(240, 0, 10, 150, RA8875_WHITE);
 tft.fillRect(295, 0, 10, 150, RA8875_WHITE);
 tft.fillRect(350, 0, 30, 150, RA8875_WHITE);
 tft.fillRect(385, 0, 30, 150, RA8875_WHITE);
 tft.fillRect(465, 0, 15, 150, RA8875_WHITE);
}

void setup()
{
 Serial.begin(115200);
 /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
 if (!tft.begin(RA8875_480x272)) {
   Serial.println("RA8875 Not Found!");
   while (1);
 }

pinMode(RA8875_INT, INPUT_PULLUP);
 tft.displayOn(true);
 tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
 tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
 tft.PWM1out(255);
 tft.touchEnable(true);

drawPiano();

Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
 Serial.println("Waiting for touch events ...");
}

void loop()
{
 if (digitalRead(RA8875_INT) == LOW) {/* Wait around for a new touch event (INT pin goes low) /
   if (tft.touched())  {  /
Make sure this is really a touch event */
     tft.touchRead(&tx, &ty);

if ((tx >= 50) && (tx <= 150) && (ty >= 120) && (ty <= 450)) {
       CurrentNote = 50;
     } else if ((tx >= 160) && (tx <= 250) && (ty >= 120) && (ty <= 450)) {
       CurrentNote = 52;
     } else if ((tx >= 270) && (tx <= 360) && (ty >= 120) && (ty <= 450)) {
       CurrentNote = 54;
     } else {
       CurrentNote = 0;
     }
     
     Serial.print(F("Touched "));
     Serial.print(tx);
     Serial.print(F(","));
     Serial.print(ty);
     Serial.print(F("\t"));
     Serial.println(CurrentNote);
   } else  {
     Serial.println(F("RA8875_INT NOT TOUCHED "));
   }
 }
}



(this is basically your code from which I removed all the midi stuff and added a few else and corrected CurrentNote type)

I load your code. After power on i push the first key down to about 1-2 sec and release, this is my serial output

Touched 110,256 50
Touched 112,234 50
Touched 106,227 50
Touched 105,231 50
Touched 112,185 50
Touched 105,238 50
Touched 104,239 50
Touched 103,240 50
Touched 106,236 50
Touched 105,230 50
Touched 108,251 50
Touched 111,235 50
Touched 106,232 50
Touched 100,215 50
Touched 105,196 50
Touched 101,235 50
Touched 107,256 50
Touched 106,234 50
Touched 112,235 50
Touched 109,233 50
Touched 107,235 50
Touched 108,250 50
Touched 107,255 50
Touched 113,231 50
Touched 112,229 50
Touched 107,209 50
Touched 106,233 50
Touched 107,234 50
Touched 107,235 50
Touched 121,268 50
Touched 109,229 50
Touched 108,218 50
Touched 105,235 50
Touched 109,234 50
Touched 107,253 50
Touched 112,239 50
Touched 105,237 50
Touched 106,233 50
Touched 104,234 50
Touched 108,232 50
Touched 103,248 50
Touched 111,236 50
Touched 108,234 50
Touched 107,224 50
Touched 106,235 50
Touched 105,239 50
Touched 103,247 50
Touched 111,236 50
Touched 113,233 50
Touched 108,230 50
Touched 102,206 50
Touched 109,229 50
Touched 122,229 50
Touched 108,271 50
Touched 108,234 50
Touched 109,229 50
Touched 103,190 50
Touched 84,230 50
Touched 104,243 50
Touched 108,233 50
Touched 108,184 50
Touched 109,229 50
Touched 104,242 50
Touched 107,245 50
Touched 107,226 50
Touched 107,228 50
Touched 90,167 50
Touched 106,229 50
Touched 110,229 50
Touched 104,229 50
Touched 108,223 50
Touched 101,203 50
Touched 103,242 50
Touched 106,280 50
Touched 113,238 50
Touched 108,203 50
Touched 108,162 50
Touched 109,231 50
Touched 111,228 50
Touched 106,287 50
Touched 106,230 50
Touched 108,220 50
Touched 107,159 50
Touched 104,255 50
Touched 103,256 50
Touched 106,285 50
Touched 108,234 50
Touched 108,231 50
Touched 106,234 50
Touched 106,237 50
Touched 105,235 50
Touched 112,283 50
Touched 108,234 50
Touched 108,234 50
Touched 109,186 50
Touched 104,239 50
Touched 106,237 50
Touched 107,284 50
Touched 106,234 50
Touched 106,234 50
Touched 104,192 50
Touched 113,238 50
Touched 106,234 50
Touched 107,279 50
Touched 107,227 50
Touched 112,229 50
Touched 104,194 50
Touched 111,238 50
Touched 108,234 50
Touched 106,252 50
Touched 106,235 50
Touched 113,226 50
Touched 102,223 50
Touched 107,235 50
Touched 107,233 50
Touched 105,265 50
Touched 109,236 50
Touched 110,227 50
Touched 104,197 50
Touched 107,235 50
Touched 107,234 50
Touched 107,235 50
Touched 109,237 50
Touched 104,229 50
Touched 106,230 50
Touched 101,233 50
Touched 107,235 50
Touched 104,239 50
Touched 113,231 50
Touched 106,231 50
Touched 107,207 50
Touched 108,172 50
Touched 106,241 50
Touched 109,261 50
Touched 113,230 50
Touched 109,234 50
Touched 108,217 50
Touched 101,233 50
Touched 105,230 50
Touched 106,245 50
Touched 112,232 50
Touched 108,235 50
Touched 107,233 50
Touched 105,233 50
Touched 105,231 50
Touched 102,276 50
Touched 100,277 50
Touched 105,233 50
Touched 106,235 50
Touched 104,232 50
Touched 106,240 50
Touched 108,257 50
Touched 112,235 50
Touched 107,250 50
Touched 107,217 50
Touched 106,234 50
Touched 111,231 50
Touched 106,243 50
Touched 107,299 50
Touched 107,234 50
Touched 107,219 50
Touched 103,197 50
Touched 109,231 50
Touched 105,242 50
Touched 109,233 50
Touched 112,237 50
Touched 107,219 50
Touched 106,236 50
Touched 110,233 50
Touched 106,240 50
Touched 107,235 50
Touched 110,239 50
Touched 103,224 50
Touched 106,234 50
Touched 106,233 50
Touched 106,240 50
Touched 110,237 50
Touched 110,236 50
Touched 102,223 50
Touched 106,233 50
Touched 103,233 50
Touched 105,234 50
Touched 105,264 50
Touched 109,230 50
Touched 109,234 50
Touched 106,184 50
Touched 107,232 50
Touched 105,245 50
Touched 106,275 50
Touched 103,230 50
Touched 107,230 50
Touched 103,167 50
Touched 107,233 50
Touched 104,244 50
Touched 107,284 50
Touched 106,235 50
Touched 108,232 50
Touched 106,163 50
Touched 105,231 50
Touched 110,232 50
Touched 110,285 50
Touched 107,233 50
Touched 107,246 50
Touched 109,209 50
Touched 105,239 50
Touched 104,242 50
Touched 106,303 50
Touched 107,237 50
Touched 107,231 50
Touched 111,181 50
Touched 105,239 50
Touched 106,238 50
Touched 106,296 50
Touched 106,236 50
Touched 110,230 50
Touched 106,195 50
Touched 106,239 50
Touched 100,235 50
Touched 106,246 50
Touched 107,235 50
Touched 105,231 50
Touched 104,189 50
Touched 106,238 50
Touched 105,234 50
Touched 106,281 50
Touched 111,240 50
Touched 106,224 50
Touched 103,193 50
Touched 107,234 50
Touched 106,234 50
Touched 105,233 50
Touched 111,238 50
Touched 111,225 50
Touched 102,225 50
Touched 101,235 50
Touched 106,234 50
Touched 105,243 50
Touched 106,285 50
Touched 103,229 50
Touched 107,210 50
Touched 107,234 50
Touched 107,232 50
Touched 105,258 50
Touched 144,237 50
Touched 107,235 50
Touched 107,213 50
Touched 102,235 50
Touched 105,232 50
Touched 110,257 50
Touched 112,231 50
Touched 106,236 50
Touched 106,234 50
Touched 108,233 50
Touched 121,230 50
Touched 107,253 50
Touched 111,235 50
Touched 107,235 50
Touched 108,221 50
Touched 104,230 50
Touched 106,230 50
Touched 108,251 50
Touched 112,249 50
Touched 108,234 50
Touched 107,226 50
Touched 102,191 50
Touched 106,230 50
Touched 108,246 50
Touched 111,236 50
Touched 101,234 50
Touched 107,225 50
Touched 105,232 50
Touched 105,239 50
Touched 116,238 50
Touched 112,234 50

and nothing else.

I made a video of my original code:

So it seems you always get a touch and enter the if (tft.touched())  {