Is there a way to modify this code to make it velocity sensitive? This is the only code I found that works with my board (Teensy 3.6). I have a piezo connected to (A0) that triggers a midi note but only at one volume, The value of the piezo varies from 50-150 depending how hard it's hit. Can this be read and sent as a midi velocity message?
Mod edit: code tags
C:
/*
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
}
Last edited by a moderator: