Multiple Velocity Sensitive Piezos!

Hello! Im currently near finishing my MIDI controller! :slight_smile: However… I have only managed to have 4 MIDI drum pads working if they ARENT velocity sensitive. I have the code to make one piezo velocity sensitive, but Im not very good at arrays and am struggling to implement one. Everytime I change something it says something else isnt declared - when it worked quite happily before the array!

This is the original code:

#include <MIDI.h>

const int piezo = A9;

int threshold = 50; //anything over fifty means we’ve hit the piezo

void setup(){

MIDI.begin(1);

}

void loop(){

int sensorReading = analogRead(piezo);
if (sensorReading >= threshold){
int maxPiezoVal = getMaxVal(sensorReading);
byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
MIDI.sendNoteOn(50, velocity, 1);
delay(10);
MIDI.sendNoteOff(50, 0, 1);
delay(100);
}
}

int getMaxVal(int lastVal){
int currentVal = analogRead(piezo);
while (currentVal>lastVal){
lastVal = currentVal;
currentVal = analogRead(piezo);
}

return lastVal;
}

And this is what I have done, which I know is wrong but I gave it a go :

#include <MIDI.h>

int piezo[4] = {A9, A10, A11, A12};
#define inputs 4
int note[4] = {50, 51, 52, 53};
int threshold = 50; //anything over fifty means we’ve hit the piezo

void setup(){

MIDI.begin(1);

}

void loop(){
for(int i=0; i<inputs; i++) {
int sensorReading = analogRead(i);
if (sensorReading >= threshold){
int maxPiezoVal = getMaxVal(sensorReading);
byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
MIDI.sendNoteOn(note*, velocity, 1);*

  • delay(10);*
    _ MIDI.sendNoteOff(note*, 0, 1);_
    _
    delay(100);_
    _
    }_
    _
    }*_

int getMaxVal(int lastVal) {
* int currentVal = analogRead(i);*
* while (currentVal>lastVal){*
* lastVal = currentVal;*
* currentVal = analogRead(i);*
* }*
return lastVal;
}
}
[/quote]
Would someone be able to tell me where Im going wrong please? I really need this finished by tomorrow night so in a bit of a panic :astonished: I just need four velocity sensitive drum pads!
I appreciate any help that anyone can give me. Thanks!

#include <MIDI.h>

const int InputCount = 4;
int piezo[InputCount] = {A9, A10, A11, A12};
int note[InputCount] = {50, 51, 52, 53};

const int threshold = 50; //anything over fifty means we've hit the piezo

void setup() {
  MIDI.begin(1);
}

void loop() {
  int sensorReading[InputCount];
  for(int i=0; i<InputCount; i++) {
    sensorReading[i]= analogRead(i);
    if (sensorReading[i] >= threshold) {
      int maxPiezoVal = getMaxVal(i, sensorReading[i]);
      byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
      MIDI.sendNoteOn(note[i], velocity, 1);
      delay(10);
      MIDI.sendNoteOff(note[i], 0, 1);
      delay(100);
    }
  }
}

int getMaxVal(int pin, int lastVal) {
  int currentVal = analogRead(pin);
  while (currentVal>lastVal){
    lastVal = currentVal;
    currentVal = analogRead(pin);
  }
  return lastVal;
}

Thank you for the quick reply! Thats gone through fine onto the board etc, but its sending out constant arpeggiated notes!

Not sure why its happening, changing the threshold or delay doesnt seem to make a difference. Any ideas?

Basically getting this!

ScreenFlow.mp4 (399 KB)

Oops‚Ķ I forget to use the ‚Äėpiezo‚Äô array for the pin numbers. It was reading A0, A1, A2, and A3. Try this version:

#include <MIDI.h>

const int InputCount = 4;
int piezo[InputCount] = {A9, A10, A11, A12};
int note[InputCount] = {50, 51, 52, 53};

const int threshold = 50; //anything over fifty means we've hit the piezo

void setup() {
  MIDI.begin(1);
}

void loop() {
  for(int i=0; i<InputCount; i++) {
    int sensorReading = analogRead(piezo[i]);
    if (sensorReading >= threshold) {
      int maxPiezoVal = getMaxVal(piezo[i], sensorReading);
      byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
      MIDI.sendNoteOn(note[i], velocity, 1);
      delay(10);
      MIDI.sendNoteOff(note[i], 0, 1);
      delay(100);
    }
  }
}

int getMaxVal(int pin, int lastVal) {
  int currentVal = analogRead(pin);
  while (currentVal>lastVal){
    lastVal = currentVal;
    currentVal = analogRead(pin);
  }
  return lastVal;
}

Its just doing the same thing :frowning: I dont understand why! It worked fine with just one, but four seems to be a problem!

Would there be a way of doing it without an array? Everything I do spits out constant messages, so think this is out of my league :blush:

It plays a note if the analog input is above ‚Äėthreshold‚Äô. Try setting threshold to 1024. If it still produces output then something is horribly wrong. If that stops the output, lower it to 1000. Continue lowering the value until the output can be triggered by hitting the piezo but will not trigger on its own.

You can unroll the loop if you want but it should not have an effect on the output and it will require that many changes be done four times, making it VERY easy to miss a change.

#include <MIDI.h>

const int InputCount = 4;
int piezo[InputCount] = {
  A9, A10, A11, A12};
int note[InputCount] = {
  50, 51, 52, 53};

const int threshold = 50; //anything over fifty means we've hit the piezo

void setup() {
  MIDI.begin(1);
}

void loop() {
  int sensorReading;

  sensorReading = analogRead(piezo[0]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[0], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    MIDI.sendNoteOn(note[0], velocity, 1);
    delay(10);
    MIDI.sendNoteOff(note[0], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[1]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[1], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    MIDI.sendNoteOn(note[1], velocity, 1);
    delay(10);
    MIDI.sendNoteOff(note[1], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[2]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[2], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    MIDI.sendNoteOn(note[2], velocity, 1);
    delay(10);
    MIDI.sendNoteOff(note[2], 0, 1);
    delay(100);
  }

  sensorReading = analogRead(piezo[3]);
  if (sensorReading >= threshold) {
    int maxPiezoVal = getMaxVal(piezo[3], sensorReading);
    byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
    MIDI.sendNoteOn(note[3], velocity, 1);
    delay(10);
    MIDI.sendNoteOff(note[3], 0, 1);
    delay(100);
  }
}

int getMaxVal(int pin, int lastVal) {
  int currentVal = analogRead(pin);
  while (currentVal>lastVal){
    lastVal = currentVal;
    currentVal = analogRead(pin);
  }
  return lastVal;
}

Ive been very silly! After checking your first post I had switched them over to A1,2,3 and 4! So obviously when you sent me the new one it had the same problem.

That has stopped the strange noises, however there is a very long release on the notes if you play a few after eachother. Ive changed the delay time down to 10 which seems to help but its still playing a note for ages!

Sorry to be a pain, Im just not so good on velocity!

That one works great, thank you so much! :slight_smile: Really appreciate your help

johnwasser:
It plays a note if the analog input is above ‚Äėthreshold‚Äô. Try setting threshold to 1024. If it still produces output then something is horribly wrong. If that stops the output, lower it to 1000. Continue lowering the value until the output can be triggered by hitting the piezo but will not trigger on its own.

You can unroll the loop if you want but it should not have an effect on the output and it will require that many changes be done four times, making it VERY easy to miss a change.

#include <MIDI.h>

const int InputCount = 4;
int piezo[InputCount] = {
 A9, A10, A11, A12};
int note[InputCount] = {
 50, 51, 52, 53};

const int threshold = 50; //anything over fifty means we’ve hit the piezo

void setup() {
 MIDI.begin(1);
}

void loop() {
 int sensorReading;

sensorReading = analogRead(piezo[0]);
 if (sensorReading >= threshold) {
   int maxPiezoVal = getMaxVal(piezo[0], sensorReading);
   byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
   MIDI.sendNoteOn(note[0], velocity, 1);
   delay(10);
   MIDI.sendNoteOff(note[0], 0, 1);
   delay(100);
 }

sensorReading = analogRead(piezo[1]);
 if (sensorReading >= threshold) {
   int maxPiezoVal = getMaxVal(piezo[1], sensorReading);
   byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
   MIDI.sendNoteOn(note[1], velocity, 1);
   delay(10);
   MIDI.sendNoteOff(note[1], 0, 1);
   delay(100);
 }

sensorReading = analogRead(piezo[2]);
 if (sensorReading >= threshold) {
   int maxPiezoVal = getMaxVal(piezo[2], sensorReading);
   byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
   MIDI.sendNoteOn(note[2], velocity, 1);
   delay(10);
   MIDI.sendNoteOff(note[2], 0, 1);
   delay(100);
 }

sensorReading = analogRead(piezo[3]);
 if (sensorReading >= threshold) {
   int maxPiezoVal = getMaxVal(piezo[3], sensorReading);
   byte velocity = map(maxPiezoVal, 0, 1023, 0, 127);//velocity between 50 and 127 based on max val from piezo
   MIDI.sendNoteOn(note[3], velocity, 1);
   delay(10);
   MIDI.sendNoteOff(note[3], 0, 1);
   delay(100);
 }
}

int getMaxVal(int pin, int lastVal) {
 int currentVal = analogRead(pin);
 while (currentVal>lastVal){
   lastVal = currentVal;
   currentVal = analogRead(pin);
 }
 return lastVal;
}





Hi John, just wondering if you could be of any assistance, im using the code above with a teensy microcontroller instead of an arduino and i think ive changed the libraries/usb message send accordingly however i would like to add 2 analogue potentiometer inputs to this code which send midi control change data to control effects parameters in a DAW, would you know how i would go about doing this?


many thanks