Problem Sending MIDI Data Using a TFT Screen

Hi all,

I’m trying to use an Uno R3 and a TFT screen to send midi data to a computer over USB. The idea is to split the screen into 4 different segments and send a different midi note depending on which section is pushed. The code I have usually works as intended for about 3 or 4 seconds until which no midi signals are sent at all without a push of the reset button. Sometimes it won’t work for more than a second and sometimes it won’t work at all. I’ve been stuck on this issue for a few days now so any help would be greatly appreciated. Code is below:

In case this helps: I’m using a 5" screen with an Adafruit RA8875 driver board. I’m also using the programs loopMIDI and Hairless MIDI to open up the MIDI port.

#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_mfGFX.h"
#include "Adafruit_RA8875.h"


// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 8
#define RA8875_CS 10
#define RA8875_RESET 9
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;

//starting midi parameters
int fret = 0;
int lastFret = 0;
int velocity = 0x50;
int channel = 0x90;
int modeChannel = 0xC0;
int lastString=0;

bool off;
int noteOffCounter;
int noteOnCounter;

void setup() 
{  
  Serial.begin(38400);

  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_800x480)) {
    //RA8875 Not Found
    while (1);
  }
  
  lastString = 0;
  off=true;
  noteOnCounter=0;
  noteOffCounter=0;

  pinMode(RA8875_INT, INPUT);
  digitalWrite(RA8875_INT, HIGH);
  
  tft.touchEnable(true);
  tft.fillScreen(RA8875_BLACK);
  tft.PWM1out(255);
}

void loop() 
{
  if (tft.touched()){
    noteOnCounter++;
  }
  else{
    noteOffCounter++;
  }

  if(noteOnCounter==10 || noteOffCounter==10)
  {
    if((float)(noteOnCounter/noteOffCounter) > 0.5){
      tft.touchRead(&tx,&ty);
      int currentString;

      if (tx <=250){
        currentString = 1;
      } else if(tx <=500){
        currentString = 2;
      } else if(tx<=750){
        currentString = 3;
      } else {
        currentString = 4;
      }

      if (currentString != lastString){
        noteOffInstant();
        pluck(currentString);
      }

      off = false;
    }
    else if (off == false){
      noteOffInstant();
      lastString=0;
      off = true;
    }
    
    noteOnCounter=0;
    noteOffCounter=0;
  }
}

// play a string
void pluck(int string){
  int noteStart;
  
  if (string == 4){
    noteStart = 0x4F;
  } else if (string == 3){
    noteStart = 0x4A;
  } else if (string == 2){
    noteStart = 0x45;
  } else if (string == 1){
    noteStart = 0x40;
  }

  noteOn(channel,noteStart,velocity);
  lastString=string;
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(int cmd, int pit, int vel) {
  Serial.write(cmd);
  Serial.write(pit);
  Serial.write(vel);
}

//turns off all midi notes
void noteOffInstant(){
  noteOn(0xB0,0x7B,0x00);
}
    if((float)(noteOnCounter/noteOffCounter) > 0.5){

After doing the integer division, it is pointless to cast the resulting 0 or 1 to a float.

      int currentString;

I suppose that somewhere below, I'll find more ints named currentFloat, currentDouble, etc...