Trigger MIDI notes with FSR

Hi, everyone!

First and foremost, please forgive my ignorant and noob-ish question. I have tried to resolve my problem myself but all of the internet searches and reverse-engineering of publicly available code has left me possibly more confused than I was before.

All that I am trying to do is write a piece of code that will allow me to trigger 1 MIDI note by pressing an FSR. The harder you press the FSR, the louder the MIDI note will sound.

Bizarrely, I was able to write code from scratch that enabled my board to play a repeating 45-second melody via the MIDI serial. It's when I tried to add the FSR to the mix that I became hopelessly confused.

I am embarrassed because I know that this will turn out to be a relatively simple few lines of code, but at this point I am willing to swallow my pride and throw myself on the mercy of this board.

Any help that anyone can provide would be greatly appreciated, and I promise to pay it forward to the next noob once I know enough about the arduino to offer advice.

you're describing what is often called channel aftertouch in MIDI parlance.

instead of simply triggering a note (or series of notes) upon any change on the force sensor, read the value of the force sensor in a loop and use it to insert MIDI Volume commands on the output channel until there is no force detected on the force sensor.

if you program it correctly, you should get a "soft" (low volume) note if you lightly tap the sensor, and a louder note if you rap the sensor sharply.

from a musician standpoint, it might be more interesting to use the initial reading to send the "note on" command, and a reading of no force (once the sensor is let go) to send the "note off" between those times, translate the force on the sensor to send MIDI Expression values on that channel (in a loop). when the "no force" event is detected, remember to reset the MIDI Expression value to '0' on that channel along with sending the "note off" command. -- this will act more like a regular MIDI controller with "channel aftertouch" ...

Thank you for your reply. I will use your advice gratefully.

Ok, I have finally hacked together a tiny kernel of a sketch that plays a midi note (middle C), and gets louder the harder I press, like so:

//with apologies to roguescience

int fsrC = 0;                  //FSR is on pin 0
int fsrVal = 0;                //variable for reading FSR value
int mappedFsrVal = 0;          //variable for holding remapped FSR value
int prevFsrVal = 0;               //variable for storing our prev FSR value
int thresh1 = 0;            //threshold amount

void setup() {
  Serial.begin(31250);            //MIDI communicates at 31250 baud

void loop(){

   fsrVal = analogRead(fsrC);         //read input from FSR
   mappedFsrVal = map(fsrVal, 0, 127, 0, 127);  //map value to 0-127
   if(abs(mappedFsrVal - prevFsrVal) >= thresh1){
     midiOUT(0xB0, 7, mappedFsrVal); //CC Volume message
     midiOUT(0x90, 60, mappedFsrVal); //MIDI noteOn message/Play Middle C
     prevFsrVal = mappedFsrVal;

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);


My problem now is that it triggers so fast that I get a constant, rapid-fire series of notes every time I press my FSR. I have tried introducing a delay, but all that has done is to regulate the amount of time that exists between the false-triggers, and as I increase the delay, it incrementally decreases the speed at which I can trigger individual midi messages by pressing the FSR.

I think I need to make these rapid-fire false triggers dependent on the state of the prevFsrVal but the mechanism through which I would do this eludes me.

It is for that reason that I ask, is there a way of "debouncing" the serial information coming from the FSR?

I need the FSR to trigger once when I press it, and yet still be sensitive enough that I can press it quickly (ie 100ms between presses) and still send discrete midi messages.

I hope the above makes sense.

I think I figured this out while walking my dogs on my lunch break.

I need to have the arduino check the prevFsrVal in order to determine whether or not to send the Midi noteOn message.

If the prevFsrVal is == zero, then read the FSR and send the appropriate midi messages.

If the prevFsrVal is > zero, do not send noteOn message.

In other words, only play a note if the FSR has been released before being pressed again, and it doesn't matter how long ago that was.

Now how to say that in arduino-ese is something that is a jumbled mess of confusion in my tiny little brain, but I think I am on the right track.

Comments? Suggestions? Refutations of the above hypothesis?

Okay, that previous post was predicated on the idea that I can have the arduino "remember" what the state of the analog input was before the current state of the analog input.

Now my ridiculously clueless question to anyone and everyone is, is there in fact a way to do that?

I keep trying to write a piece of code that will do that and keep thinking that I am doing it but then it doesn't work and I realize that I am just restating what the current state of the analog pin is.

How do you say "was" in the arduino programming language?*

*with regards to analog/variable inputs. I have seen some pieces of code like this for digital inputs and am assuming that there is a difference with regards to analog inputs.