reading pieozo velocity information

hey.
im trying to build some kind of drum controller. the first prototype should have a 4x4 pad matrix with an led in every pad.
my problem at the moment is how to read the velocity of pad hits as fast as possible. i experimented a while and came up with this:

#include "multiplexer.h"

int led = 13;
int count = 0;
int monitor;
long t;
int L = 0;
boolean block = false;
multiplexer MP = multiplexer(5,4,3,6,1);

void setup(){
  digitalWrite(led, HIGH);
  digitalWrite(6, HIGH);
  Serial.begin(9600);
}

void loop () {
  int level = analogRead(0);
  if( level > 50 ) {
    if(!block) {
      L = 0;
      t = millis();
    }
    while(level > 50) {
      L++;
      level = analogRead(0);
    }
    monitor = (int)((float)L/400.0*7.0);
    block = true;
  }
  for (count=0; count<=monitor; count++) {
    MP.select( count );
  }
  if(t+50<millis()) {
    if( block ) {
      Serial.println( L );
      block = false;
    }
    monitor = 0;
  }
}

(i connected a piezo to pin 0 of the arduino decimila with a 1mega-ohm resistor parallel, the mutliplexer lights up 8 leds according to the measured velocity)

works fine but the problem is the 50 ms delay. if i remove it i get multiple values from just one hit, for example 80/300/20.
now i think i have to understand how the piezo behaves when it gets hit. i read somewhere its a goo method to mesure the time untill the voltage is under a certain treshold again. but is there some other method?

thanks alot! josh.

If you introduce a lowpassfilter, or an averagefilter, you'd probably get much better triggerpoints without the jitter.

xxx <- I was going to post a link to wikipedia here, but I'm not yet trusted with links :frowning:

Better perhaps, is to create an average-filter.

The following playground tutorial shows how to impliment a digital low pass filter: Arduino Playground - Smooth

My solution would be to keep on reading the same input until you see the level peak and start to drop.
Then you trigger the MIDI note on. Then keep reading the input until it drops below a certain threshold send a IDI note off, and then it is allowed to trigger a note on a voltage rise again.

You could do this for all pads simultaneously (well on after the other using a state machine) or just track the one hit pad up, until you trigger.

thanks for the answers, waiting until there is a peak and until it drops down again doesnt work because when it dropped it seems to raise again and generates another peak..
the filter seems to be a good idea! ill try it! thanks again!

when it dropped it seems to raise again and generates another peak

That's contact bounce, it should be over by the time you send three bytes of MIDI data. The first peak should be the largest.

Hey, i tried out some smoothing but still the problem is timing. it seems to take up to 10+ ms until the piezo returns to its normal condition. this is a problem because perhaps i want to hit the same drum several times in a second...
i tried removing the 1 mega ohm resistor i connected parallel and found that it kind of reacts to pressure which is nice because i wanted to have a mode where the pads behave like pushbuttons. if i decrease the resistance, the signal is less sensitive and you got to hit harder to cross the treshold.
now i came up with another idea. maybe it would work to connect a transistor that connects the piezo to ground if i want it to... so it arduino detects a hit, i tigger the trasistor and the piezo is connected to ground for like 3 ms.. this should "discharge" it..
what do you guys think of this?

i'm very bad when it comes to electronics.. i just recognized that if there is a very low resistor connected parallel to the piezo it seems to return to its normal condition much faster but its also not that sensitive.. so the transistor method would combine the positive things of both situations.. with a high resistor and with a lower one...

Try putting a capacitor across the transducer, that will hold the charge (voltage) so you can read it. Then discharge the capacitor by switching the input pin to an output and putting a low on it.

Sorry to post two in a row but:-

it seems to take up to 10+ ms until the piezo returns to its normal condition. this is a problem because perhaps i want to hit the same drum several times in a second...

Wow :o you want to drum at 100 drum strokes per second. Isn't that some sort of a record?

ok, it worked out pretty good for one pad now adjusting the sensitivity and blocking further reading for 50ms after a hit. other methods like connecting a capacitor always had other disadvantages..
but now i got a similar problem with 2 pads connected to a 4051 multiplexer. when i hit a pad hard, the other one also generates a hit. even stranger that when i connect the pads to two pins on the 4051 that i never read in my code, a hit on one pad generates a nearly full velocity hit on both pads.. perhaps i should connect all other pins to ground.. ill try that..

thanks for any help!

Edit: i tested connecting the other pins to ground but nothng changed. it seems like if the voltage the piezo produces gets to high, some current "leaks" to the other pin... so i think it wound work to limit the incoming current from the piezo, but how do i do that? a resistor in series doesnt work..
thanks again!

It's not current you need to limit, it's voltage. Piezo transducers produce a frighteningly high voltage when you hit them hard. You know those gas lighters that have no flint or battery, but spark when you squeeze them? They work on the piezoelectric principle!

Are you frightened yet?

Stick a 5.1v zener across the piezo wires to bleed off the nasty spikes. There's very little current behind it, so you can use the smallest 5.1v zeners you can find.

If you don't have a zener then a signal diode from the input to +5V should catch the high voltages, remember to put a resistor in series so the voltage can be dropped across something. Also add some decoupling capacitors. That is 0.1uF across the power supply AT THE POWER PINS of the IC, leads as short as possible.