Midi ghost notes

Helo all:

im new to arduino and i seem to have reach a dead end.

What im trying to do is build an electronic drum trigger out of a few piezos. For the moment im trying to get the desired behaviour on one single piezo and then ill integrate the rest.

the problem is that when i send a single midi note with a given velocity. arduino sends lots of messages with lower velocities as well.

This is really no good, as i get undesired effects..

here is the code that im using and that i've been trying to fix for over 2 days.

Im using hairless midi. running at 115200 khz.

Any help would be appreciated.

#include <MIDI.h>
int pad = 5;
int note = 55;
bool active = false;  
bool triggered = false; 
int treshold = 120;
int peak = 0;
int in = 0;
int out = 0;
byte status1;

MIDI_CREATE_DEFAULT_INSTANCE();
#define LED 13
void setup() {
  pinMode(pad, INPUT);   
  pinMode(LED, OUTPUT);
  MIDI.begin();
  Serial.begin(115200);                          
}



void loop() {
  in = analogRead(pad);
  if(in > treshold) {
    if(triggered == false) {
      peak = in;
      triggered = true;
    } else {
      if(in > peak) {
        peak = in;
      } else {
        if(active == false) {
          out = peak;
          digitalWrite(LED,HIGH);
          MIDI.sendNoteOn(42,out,1);  // Send a Note (pitch 42, velo 127 on channel 1)
          out = 0;


        }
        active = true;
        peak = peak * .96;  
        if (peak < 150) {
          triggered = false;       
          active = false;
          MIDI.sendNoteOff(42,out,1);

        }
      }
    }
    
  }
  if(in > 10 ) {
  //Serial.print(peak);
  //Serial.print(' ');  
  //Serial.print(in);
  //Serial.print(' ');
  //Serial.print(out);
  //Serial.print(' ');
  //Serial.println(1200);
  }
}

the problem is that when i send a single midi note with a given velocity. arduino sends lots of messages with lower velocities as well.

This is because you are not waiting for the oscillations from the sensor to die down after you trigger the first note so lots of notes will be triggered as the sensor output decays.

After you have sent the MIDI note on then have a loop that waits until the input has dropped below the threshold. Like this:-

while(analogRead(pad) > treshold) { } // do nothing until input has fallen;