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