Hi hat controller sketch

I have a pedal that uses a FSR connected to pin (A0) and a drum pad that uses a piezo connected to pin (A1). I want the drum pad to switch between 3 midi notes by varying the value of pin (A0) with the pedal. Is it possible to read 2 analog pins simultaneously?

You can read one analog value and then right after it the other analog value but not both at once. However reading one after the other may be close enough to simultaneous for your purpose. Try it and see.

Peter

Thanks for responding, I’m not experienced with code at all, I was able to get all the drum pads to work just by pasting this block of code over and over and changing the pin and note numbers. I don’t understand why the code works or how it can be somewhat velocity sensitive using the ‘‘while’’ statement but it works unless I can find a better way. I was able to get the FSR pedal to trigger 1 note by changing (val > 600) to (val < 10) but I’m not sure how to get it to trigger the second note which is triggered when the pedal is tapped down lightly and very quickly released mimicking a mechanical hi hat splash.
I’m grateful for any idea how to accomplish this.

{

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
}

If there’s a better way to write this, feel free to make corrections or suggestions.

How it works is fairly easy. It appears that the drum pad is a resistive element of some kind that produces an analog voltage that varies between (likely) 0 and 5 volts which produces a number between 0 and 1023 which is proportional to the voltage on the input pin. The analog read reads the current voltage value (0V in produces 0, 5V in produces 1023). The first if will send the note when the value is above 600 (around 2.5V I expect). The while loop waits (going around and around reading the analog voltage) until the value drops below 600 at which point it cuts the note off. If you reduce that value it will (presumably) take longer for the value to get down below the the lower value making the response slower and the note longer. In order to detect a change across a short interval of time you would need to add a timer so that you get a time value associated with the transition and figure out what analog values correspond to a transition. I’d tend to do something like this by coding it as a state machine, but you are getting somewhat complex. I expect your best bet would be to find a similar project on the net and copy their code.

Peter

Thank you Peter great explanation! The pads all use piezos that create voltage when struck and the FSR in the hi hat pedal creates resistance when compressed. I’m using a Teensy 3.6 board which has 22 analog inputs at 3.3v. I have tried many sketches but this is the only one that worked with this board. I don’t know if it because the others were made for a 5v board or what. I’ve searched and not found any examples of a hi hat controller code. I use this pedal with a commercial unit and it works the same way, the pedal triggers 2 notes and also controls 3 notes on the designated pad. That’s I guess in the firmware of the unit and I have no way to find out how they did it.

While I haven’t used a Teensy board, it is a good choice as it is much more powerful than a standard arduino with a 328. Basically what you need to do is create a stream of data pairs, one of which is the voltage from the analog port and the other is elapsed time from one of the timers. I’d start by printing out on the serial port to the screen the time value and the associated value of the analog input. That should tell you what pattern (what voltage across what time) you need to trigger each note. Then you need to arrange the code (which is where the state machine come in) to recognize the trigger pattern and move the state machine in to the appropriate state to output the correct note and possibly stop the other notes (you have the code now to start and stop midi notes, all you need is the triggers and state machine). I can’t think of an example that there is likely to be a tutorial for that would help you, perhaps someone else can. Finding some code that does almost what you want to copy is likely to be your easiest course.

Peter

Any idea how to make this code velocity sensitive?

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
}

Although it isn’t very clear what velocity means, I assume that it will involve the time between two values in some manner. Thus this tutorial should give you some examples of measuring time intervals:

Peter

I think velocity is the harder/faster you hit it, the louder it gets. Like cheap/old MIDI keyboards are just switches, but good ones can sense the speed you hit it and make the loudness appropriate to that.