Hi there,
I'm trying to figure out a way of adapting a code so I get sensible velocity readings using piezo transducers.
I'm working on a code by Amanda Ghassaei and whilst it works fine for a single transducer, I can't find a way to make it work for multiple transducers.
The code is as follows...
int noteOn = 144; //turn note on
int noteOff = 128;//turn note off
int piezo1 = A0; // analogue pin 1m ? resistor
int piezo2 = A1; // analogue pin 1m ? resistor
int piezo3 = A2; // analogue pin 1m ? resistor
#define cal 130 //set delay time
int threshold = 20; // sets threshold for trigger
void setup(){
Serial.begin(9600);//baud rate
}
void loop(){
int piezoVal1 = analogRead(piezo1);//assign piezo1
if (piezoVal1>threshold){
int maxPiezoVal = getMaxVal(piezoVal1);
byte velocity = map(maxPiezoVal, 0, 1023, 50, 127);//scale piezo reading to MIDI values
MIDImessage(noteOn, 60, velocity);
delay(cal);
MIDImessage(noteOff, 60, 0);
delay(50);
}
int piezoVal2 = analogRead(piezo2);//assign piezo2
if (piezoVal2>threshold){
int maxPiezoVal = getMaxVal(piezoVal2);
byte velocity = map(maxPiezoVal, 0, 1023, 50, 127);//scale piezo reading to MIDI values
MIDImessage(noteOn, 63, velocity);
delay(cal);
MIDImessage(noteOff, 63, 0);
delay(50);
}
int piezoVal3 = analogRead(piezo3);//assign piezo3
if (piezoVal3>threshold){
int maxPiezoVal = getMaxVal(piezoVal3);
byte velocity = map(maxPiezoVal, 0, 1023, 40, 127);//scale piezo reading to MIDI values
MIDImessage(noteOn, 65, velocity);
delay(cal);
MIDImessage(noteOff, 65, 0);
delay(50);
}
}
// serial data output to hairless MIDI
void MIDImessage(byte command, byte data1, byte data2){
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
//sets scaling for MIDI velocity
int getMaxVal(int lastVal){
int currentVal = analogRead(piezo1);
while (currentVal>lastVal){
lastVal = currentVal;
currentVal = analogRead(piezo1);
}
return lastVal;
}
The part in particular is the last section that deals with getting the last value reading from the piezo and using that for the scaling.
//sets scaling for MIDI velocity
int getMaxVal(int lastVal){
int currentVal = analogRead(piezo1);
while (currentVal>lastVal){
lastVal = currentVal;
currentVal = analogRead(piezo1);
}
return lastVal;
}
How can I make this work for each individual transducer, I have tried a few ways but always end up with error messages as soon as I add the other pins.
Many thanks.