MIDI e-drum error

I create a project about 16-channels electric drum, use Arduino Mega instead of Arduino UNO. I'm using 1M resistors and 5.1V Diode Zener to connect each piezo to pin on Mega board. But got some problem when open LoopMIDI and Hairless-MIDI, some notes of pads were sending continuously, I set the baudrate to 38400/115200, what is the problem?

please draw a full schematic (not fritzing) and share it with us.

please share the code with us within </> code-tags (no link)

you set the baudrate where ?

Here is the schematic.

Code:

//Piezo defines
#define NUM_PIEZOS 16
#define KICK_THRESHOLD 100
#define SNAREH_THRESHOLD 100
#define SNARER_THRESHOLD 100
#define HHC_THRESHOLD 100
#define HHO_THRESHOLD 100  
#define HHPEDAL_THRESHOLD 100 
#define RB_THRESHOLD 100
#define R_THRESHOLD 100
#define RC_THRESHOLD 100
#define CRASH_THRESHOLD 100
#define TOM1_THRESHOLD 100
#define TOM2_THRESHOLD 100
#define TOM3_THRESHOLD 100
#define TOM4_THRESHOLD 100
#define CC1_THRESHOLD 100   
#define CC2_THRESHOLD 100
#define SPLASH1_THRESHOLD 100
#define SPLASH2_THRESHOLD 100

#define START_SLOT 0     //0 START ANALOG INPUTS

//MIDI note defines for each trigger
#define KICK_NOTE 36  
#define SNAREH_NOTE 38  
#define SNARER_NOTE 40    
//#define SNAREC_NOTE 42  
#define HHC_NOTE 49  
#define HHO_NOTE 54  
#define HHPEDAL_NOTE 48  
#define RB_NOTE 61 
#define R_NOTE 60 
#define RC_NOTE 63  
#define CRASH_NOTE 77
#define TOM1_NOTE 71 
#define TOM2_NOTE 69  
#define TOM3_NOTE 67 
#define TOM4_NOTE 65 
#define CC1_NOTE 79 
#define CC2_NOTE 81 
#define SPLASH1_NOTE 89 
#define SPLASH2_NOTE 91  

//MIDI defines
#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define MAX_MIDI_VELOCITY 127

#define SERIAL_RATE 115200

#define SIGNAL_BUFFER_SIZE 50
#define PEAK_BUFFER_SIZE 10
#define MAX_TIME_BETWEEN_PEAKS 10
#define MIN_TIME_BETWEEN_NOTES 10

unsigned short slotMap[NUM_PIEZOS];
unsigned short noteMap[NUM_PIEZOS];
unsigned short thresholdMap[NUM_PIEZOS];

short currentSignalIndex[NUM_PIEZOS];
short currentPeakIndex[NUM_PIEZOS];
unsigned short signalBuffer[NUM_PIEZOS][SIGNAL_BUFFER_SIZE];
unsigned short peakBuffer[NUM_PIEZOS][PEAK_BUFFER_SIZE];

boolean noteReady[NUM_PIEZOS];
unsigned short noteReadyVelocity[NUM_PIEZOS];
boolean isLastPeakZeroed[NUM_PIEZOS];

unsigned long lastPeakTime[NUM_PIEZOS];
unsigned long lastNoteTime[NUM_PIEZOS];

byte hihat_switch = 0;
unsigned long switch_pressed = 0;


void setup()
{
  Serial.begin(SERIAL_RATE);
 
  //initialize globals
  for(short i=0; i<NUM_PIEZOS; ++i)
  {
    currentSignalIndex[i] = 0;
    currentPeakIndex[i] = 0;
    memset(signalBuffer[i],0,sizeof(signalBuffer[i]));
    memset(peakBuffer[i],0,sizeof(peakBuffer[i]));
    noteReady[i] = false;
    noteReadyVelocity[i] = 0;
    isLastPeakZeroed[i] = true;
    lastPeakTime[i] = 0;
    lastNoteTime[i] = 0;   
    slotMap[i] = START_SLOT + i;
  }
 
  thresholdMap[0] = KICK_THRESHOLD;
  thresholdMap[1] = SNAREH_THRESHOLD;
  thresholdMap[2] = SNARER_THRESHOLD;
  thresholdMap[3] = HHC_THRESHOLD;
  thresholdMap[4] = HHO_THRESHOLD;
  thresholdMap[5] = HHPEDAL_THRESHOLD;
  thresholdMap[6] = RB_THRESHOLD;
  thresholdMap[7] = R_THRESHOLD;
  thresholdMap[8] = RC_THRESHOLD;
  thresholdMap[9] = CRASH_THRESHOLD;
  thresholdMap[10] = TOM1_THRESHOLD;
  thresholdMap[11] = TOM2_THRESHOLD;
  thresholdMap[12] = TOM3_THRESHOLD;
  thresholdMap[13] = TOM4_THRESHOLD;
  thresholdMap[14] = CC1_THRESHOLD;
  thresholdMap[15] = CC2_THRESHOLD;
  thresholdMap[16] = SPLASH1_THRESHOLD;
  thresholdMap[17] = SPLASH2_THRESHOLD;
 
  noteMap[0] = KICK_NOTE;
  noteMap[1] = SNAREH_NOTE;
  noteMap[2] = SNARER_NOTE;
  noteMap[3] = HHC_NOTE;
  noteMap[4] = HHO_NOTE;
  noteMap[5] = HHPEDAL_NOTE;
  noteMap[6] = RB_NOTE;
  noteMap[7] = R_NOTE;
  noteMap[8] = RC_NOTE;
  noteMap[9] = CRASH_NOTE;
  noteMap[10] = TOM1_NOTE;
  noteMap[11] = TOM2_NOTE;
  noteMap[12] = TOM3_NOTE;
  noteMap[13] = TOM4_NOTE;
  noteMap[14] = CC1_NOTE;
  noteMap[15] = CC2_NOTE;   
  noteMap[16] = SPLASH1_THRESHOLD;
  noteMap[17] = SPLASH2_THRESHOLD;
}

void loop()
{
  unsigned long currentTime = millis();
 
 if(currentTime > (switch_pressed + 2))
 {
 hihat_switch = (((hihat_switch << 1) | digitalRead(3)) & 3);
 if(hihat_switch == 2){
   noteFire(49, 127);  
  switch_pressed = currentTime; 
   }
 if(hihat_switch == 1){
   noteFire(54, 127);  
  switch_pressed = currentTime; 
   }
 }
 
  for(short i=0; i<NUM_PIEZOS; ++i)
  {
    //get a new signal from analog read
    unsigned short newSignal = analogRead(slotMap[i]);
    signalBuffer[i][currentSignalIndex[i]] = newSignal;
   
    //if new signal is 0
    if(newSignal < thresholdMap[i])
    {
      if(!isLastPeakZeroed[i] && (currentTime - lastPeakTime[i]) > MAX_TIME_BETWEEN_PEAKS)
      {
        recordNewPeak(i,0);
      }
      else
      {
        //get previous signal
        short prevSignalIndex = currentSignalIndex[i]-1;
        if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;       
        unsigned short prevSignal = signalBuffer[i][prevSignalIndex];
       
        unsigned short newPeak = 0;
       
        //find the wave peak if previous signal was not 0 by going
        //through previous signal values until another 0 is reached
        while(prevSignal >= thresholdMap[i])
        {
          if(signalBuffer[i][prevSignalIndex] > newPeak)
          {
            newPeak = signalBuffer[i][prevSignalIndex];       
          }
         
          //decrement previous signal index, and get previous signal
          prevSignalIndex--;
          if(prevSignalIndex < 0) prevSignalIndex = SIGNAL_BUFFER_SIZE-1;
          prevSignal = signalBuffer[i][prevSignalIndex];
        }
       
        if(newPeak > 0)
        {
          recordNewPeak(i, newPeak);
        }
      }
 
    }
       
    currentSignalIndex[i]++;
    if(currentSignalIndex[i] == SIGNAL_BUFFER_SIZE) currentSignalIndex[i] = 0;
  }
}

void recordNewPeak(short slot, short newPeak)
{
  isLastPeakZeroed[slot] = (newPeak == 0);
 
  unsigned long currentTime = millis();
  lastPeakTime[slot] = currentTime;
 
  //new peak recorded (newPeak)
  peakBuffer[slot][currentPeakIndex[slot]] = newPeak;
 
  //1 of 3 cases can happen:
  // 1) note ready - if new peak >= previous peak
  // 2) note fire - if new peak < previous peak and previous peak was a note ready
  // 3) no note - if new peak < previous peak and previous peak was NOT note ready
 
  //get previous peak
  short prevPeakIndex = currentPeakIndex[slot]-1;
  if(prevPeakIndex < 0) prevPeakIndex = PEAK_BUFFER_SIZE-1;       
  unsigned short prevPeak = peakBuffer[slot][prevPeakIndex];
  
  if(newPeak > prevPeak && (currentTime - lastNoteTime[slot])>MIN_TIME_BETWEEN_NOTES)
  {
    noteReady[slot] = true;
    if(newPeak > noteReadyVelocity[slot])
      noteReadyVelocity[slot] = newPeak;
  }
  else if(newPeak < prevPeak && noteReady[slot])
  {
    noteFire(noteMap[slot], noteReadyVelocity[slot]);
    noteReady[slot] = false;
    noteReadyVelocity[slot] = 0;
    lastNoteTime[slot] = currentTime;
  }
 
  currentPeakIndex[slot]++;
  if(currentPeakIndex[slot] == PEAK_BUFFER_SIZE) currentPeakIndex[slot] = 0; 
}

void noteFire(unsigned short note, unsigned short velocity)
{
  if(velocity > MAX_MIDI_VELOCITY)
    velocity = MAX_MIDI_VELOCITY;
 
  if((note == 54) && (hihat_switch == 0)) //if pad 4 is hit (note 17) and switch is pressed change note to 22
{
note = 49;
 }
 
  midiNoteOn(note, velocity);
  midiNoteOff(note, velocity);
}

void midiNoteOn(byte note, byte midiVelocity)
{
  Serial.write(NOTE_ON_CMD);
  Serial.write(note);
  Serial.write(midiVelocity);
}

void midiNoteOff(byte note, byte midiVelocity)
{
  Serial.write(NOTE_OFF_CMD);
  Serial.write(note);
  Serial.write(midiVelocity);
}

that is not a schematic !

where did you get the code ?

I'm resolved. I reversed the negative and positive on the piezo.

That would only leave the problem that you have the Zener diodes the wrong way round.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.