Piezo Velocity for MIDI Drums - Help Needed

Status
Not open for further replies.

loui425

New member
Hi people, ive been working on my MIDI controller, which currently is reading a total of 6 analogue inputs, 4 piezos and 2 potentiometers. ive got the following code below but have absolutely no idea how would go about making the piezos velocity sensitive. I am very new to coding so dont really know where to start, has anyone ever done anything like this before with success or would know anything to help? thanks.

Code:
#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 2

// make arrays for input values and lagged input values
int inputAnalog[analogInputs];

int iAlag[analogInputs];

// make array of cc values
int ccValue[analogInputs];

// index variable for loop
int i;


void setup() {
  // MIDI rate
  Serial.begin(31250);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
 
  // loop trough active inputs for knobs
  for (i=0;i<analogInputs;i++){
   
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
   
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
    
      // calc the CC value based on the raw value
      ccValue[i] = inputAnalog[i]/8;
     
      // send the MIDI
      usbMIDI.sendControlChange(i, ccValue[i], 3);
     
      // set raw reading to lagged array for next comparison
      iAlag[i] = inputAnalog[i];
    }
  delay(5); // limits MIDI messages to reasonable number
  
  
  
  }



  
 
  
}
 
I've done something like this using FSRs for velocity ...I remember though on the forum someone doing quite a sophisticated setup using piezos. I'll look it up for you.... ;)

A couple of things about your code ... the teensy 3.1 has analog pins 14 - 23 on the front. If I read you right you want 6 analog inputs (you don't mean digital??? switches/buttons are digital), plus 2 more for your pots, plus 4 more for your peizos....

I suspect you want 6 digital inputs (hence you have included 'bounce') ... in any case you won't get any joy with your current pin assignments, and 'pullup' is not needed for analog lines, usually. So you will need a pintable to get your assignments right...

Here are six digital pins and six analog pins .... I think you can use aliases A0 - A8, but I tried that in a header file last night and it didn't work ...might be ok in an ino file (can't remember) ... anyways ...

Code:
int apNo=6;
int dpNo=6;
int analogPinTable [] = {14,15,16,17,18,19}; //maybe use aliases?
int digitalPinTable [] = {2,3,4,5,6,7};


void setup()
{

for (int i = 0; i < dpNo; i++)
  {
    delay (1);
    pinMode(digitalPinTable [i], INPUT_PULLUP );
  }

// probably not necessary since the pins should be default input no pullup ...
for (int j = 0; j < apNo; j++)
  {
    pinMode(analogPinTable [j], INPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Some hints on velocity...

Get your piezos connected up and read them ... print out in the serial monitor to get a feel for the values .... hopefully you wont need damping analog circuitry .... then use the map() function to map the values to midi values 0-127.
 
Last edited:
From what I've read, piezoelectric transducers when given a sharp strike produce voltage spikes exceeding the input range of the Teensys. Unless the 'piezos' you mentioned are already in modules that do signal conditioning, you might want to do something to limit voltage range.

Regarding 'new to coding', I suggest you break the problem into the smallest steps you can. Bigger haystack means more time searching for the needle. In your sample code, you've written = when you almost certainly meant ==.
 
Yeah, i suspect that without analog conditioning your going to have problems with piezos for velocity. fsrs are expensive, but hackable, and much better for velocity. Mick Mad even made his own fsrs ... totally impressive project .... and the velocity code is pretty simple ....
 
Status
Not open for further replies.
Back
Top