Pressure Resistor MIDI note Problem

Hi guys,

I’m trying to make my own midi keyboard.
For triggering the keys i want to use pressure sensetive resistors.

The pressure part is working. But the problem that i have is that when the pressure resistor is pushed it repeats itself (I tried to remove the delay time but the key is then repeating itself in a speed that the DAW freezes…)

Another problem is that when i push the pressure resistor en then release it. The key in my DAW doesnt stop. But stays on the note that was pressed.

I hope the explanation is clear. And someone can help me out :slight_smile:

The code i use:

#include <MIDI.h>

byte noteON = 144;//note on command
byte noteOFF = 128;
int pressPin = A0;
int potPin = A1;

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  Serial.begin(115200);
}

void loop() {
  buttonState = analogRead(pressPin);
  
  byte velocity = map(buttonState, 0, 1023, 0, 127);
  byte note = 60;
  
   if (buttonState != lastButtonState) {
  
    if (buttonState) {
      // if the current state is HIGH then the button went from off to on:
    MIDImessage(noteON, note, velocity);//turn note on
   delay(600);
    
    } 
    else {
      // if the current state is LOW then the button went from on to off:
   MIDImessage(noteOFF, note, velocity);  
   delay(600);
  }
 }
}

//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

Here’s another thing that i tried.

The problem with this one is that it is continually looping that the note is of.
The note off command only needs to be triggered ones. then when the pressure resistor is triggered again te note goes on one time. And goes off 1 time.

You can see the problem in the images i attached

#include <MIDI.h>

byte noteON = 144;//note on command
byte noteOFF = 128;
int pressPin = A0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  
  int pressVal = analogRead(pressPin);//read data from potentiometer
  
  //we have to scale the potentiometer data to fit between 0 and 127 (this is the range of MIDI notes)
  byte velocity = map(pressVal, 0, 1023, 0, 127);
  byte note = 60;

  if (pressVal > 1 ) {
  MIDImessage(noteON, note, velocity);//turn note on
  delay(300);//hold note for 300ms
  }
  else {
  MIDImessage(noteOFF, note, 0);//turn note off (note on with velocity 0)
  delay(500);//wait 200ms until triggering next note
  }

  
}

//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

What "pressure resistor" are you using and how is it wired? Why do you include MIDI.h when you never use it?

It looks like you intend that each note will last a fixed amount of time. Is that right?

Steve

@slipstick i'm using the Taiwan Alpha pressure sensor*. I'm pretty new with Arduino, saw the MIDI.h library in other tutorials so thought i needed to ad it.

I want 1 note to be played as long as there is pressure on the sensor. just like a normal electric piano key would work.

The wiring; 1 leg is going to a resistor also that leg is going to A0, the other end of the resistor is going to the ground. the other leg of the sensor is going to 5v

Well the best thing you can do is to replace
if (pressVal > 1 ) {
with something like
if (pressVal > 100 ) {
Or something more realistic.

You need to remove the delay call completely.

Then you want a boolean variable to track if the note is on or not. Only turn off the note if the pressed value is less than a specific threshold and the note is playing. Likewise only turn the note on when the note is off and the pressed value is greater than a specific threshold.

Vincentw96:
@slipstick i'm using the Taiwan Alpha pressure sensor*. I'm pretty new with Arduino, saw the MIDI.h library in other tutorials so thought i needed to ad it.

I want 1 note to be played as long as there is pressure on the sensor. just like a normal electric piano key would work.

Wiring sounds o.k.

What your first code is currently doing is to follow the variations of pressure as the "key" is held down and send new MIDI NoteOns with the new velocity as it changes (though the delays make it not very responsive). If that's not what you want you need a way to decide which of the many different pressures you may see will be used for the note velocity. Your second code just assumes that the first non-zero pressure it sees is what is needed. I don't really know if that's o.k. or not or if you will need MIDI Aftertouch etc (I'm more used to velocity-sensitive keyboards rather than pressure-sensitive).

The simple way to fix the multiple NoteOffs is to have a noteActive flag you set TRUE when a NoteOn is sent. Only if the flag is set do you send the NoteOff then immediately clear the flag (set it to FALSE).

Presumably in a real instrument you'll need to keep active flags and velocities by key (note number) probably using arrays. Which could get complex quite fast with a polyphonic instrument with many keys.

Steve

@slipstick Thank you very much! the problem of the multiple noteOffs is fixed!

Do you maybe also have a solution that when the pressure sensor is pressed only 1 note play's, and only that note untill the pressure sensor is released. The situation now is that when the pressure sensor is pressed and the pressure changes a new note is triggerd...

Post your latest code.

But you should easily be able to use the same flag. If the flag says you have already sent a NoteOn don't send another until after the NoteOff has been sent.

It's also worth thinking in advance about how this is going to work when you have more than one key. You willl need to keep track of exactly which NoteOn is sent...middle C (Midi note 60) is a nice note but you can't play much music with just that one.

Steve

@slipstick Yes thats also a good point but at this moment i have no clue on how to do that… It would be awesome if you could give me an insight of how that would work :smiley:

this is my latest code. The key stops when it is not pressed:

edit: i actually found out that the NoteActive is doing nothing in this code, but the change from buttonstate > 1 to > 100 made the difference in that the note stops when it is not pressed…
no idea how to use those flags

#include <MIDI.h>

byte noteON = 144;//note on command
byte noteOFF = 128;
int pressPin = A0;

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

boolean NoteActive = 0;


void setup() {
  Serial.begin(115200);
}

void loop() {
  buttonState = analogRead(pressPin);
  
  byte velocity = map(buttonState, 0, 1023, 0, 127);
  byte note = 60;
  
   if (buttonState != lastButtonState) {
  
    if (buttonState > 100) {
      // if the current state is HIGH then the button went from off to on:
    MIDImessage(noteON, note, velocity);//turn note on
     NoteActive == true;
//   delay(300);
    
    } 
    else {
      // if the current state is LOW then the button went from on to off:
   MIDImessage(noteOFF, note, velocity);  
     NoteActive == false;
//   delay(300);
  }
 }
}

//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

Its simple, if the note is active, and the button state is > 100, send an aftertouch message, not a noteon
message, because you've already sent a noteon message when you set the note to be active.

Learn about state machines, its a very simple and powerful insight to have.

I think you're building up a lot of trouble for yourself when you try to extend this to more notes. But first you need to really understand what your current code and your circuit layout are doing and I'm not convinced you do.

Until then there's no real point talking about making a more complex version.

Steve