I'm building a drum pad using a piezo connected to (A0), I found this code that works well with the T3.6 but is not velocity sensitive. Is there any way to modify it so it reads the value of the piezo hit and sends it as midi velocity?
HTML:
/*
MIDIUSB_test.ino
Created: 4/6/2015 10:47:08 AM
Author: gurbrinder grewal
Modified by Arduino LLC (2015) & Grumpy Mike
*/
#include "MIDIUSB.h"
// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void setup() {
Serial.begin(115200);
}
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
void loop() {
int val;
val = analogRead(A0);
if (val > 600) { // if it is greater than the threshold
noteOn(0, 50, 127); // send a note on message the 68 is the pitch of the note
MidiUSB.flush(); // send the MIDI message
while (analogRead(A0) > 600) { } // wait here until the signal has dropped
noteOff(0, 50, 127); // send the note off message
}
MidiUSB.flush(); // send the MIDI message
}