Using potentiometer for sending MIDI notes

Hello Community!
I am trying to make my DIY midi control surface to send a specific midi note whenever one of the potentiometers is moved. Rephrasing: when a potentiometer is moved it sends a fixed MIDI note, alongside the value of the analog input.
Thanks a lot!

Read the pot.
Compare any movement to a sensitivity margin.
If the movement is greater than noise,
Send the MIDI note, send the pot value.

I assume you want values between 0 and 255 for midi.
Try this.
Leo..

int rawValue, oldValue;
byte potValue;

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

void loop() {
  rawValue = analogRead(A0); // read
  if (abs(rawValue - oldValue) >= 4) { // hysteresis
    oldValue = rawValue; // remember the change
    potValue = oldValue >> 2; // 10-bit to 8-bit
    Serial.println(potValue); // print changes
  }
}

Thanks for the quick reply guys!
I thought about this issue and I think it needs a further expansion:
ideally the MIDI note would be played whenever digital and analog inputs are used.
There are buttons, encoders and potentiometers on my control surface. It would be great that, whenever I use an encoder, a pot or a button, a specific MIDI note is played, alongside the function associated to those inputs. I could be like associating a midi note to the RX led.
Let me know your thought.
Much appreciated

No for MIDI you need a range of 0 to 127.

I think you need to explain it better.

What does this mean in context of a MIDI system?

What do you mean by used? The reading from a pot will always be changing at lease +/- one significant bit. So you have to do like @Wawa said and look for a significant change.

To do that you simply call the code that sets the LED after the MIDI NoteOn message, and off again after the MIDI NoteOff message.

But please note you have no control of the RX LED on an Arduino.

Thanks for your quick reply Mike.
Let me try to explain better: pots are assigned to pitch bend, encoders send CC and buttons send notes.
The aim: when I move the potentiometers a midi note is sent alongside the pitch bend value; when I rotate the encoder, the same midi note is sent; Same behaviour for the buttons: when they are pushed the same midi note is sent alongside its original note.
Now, I was thinking, rather then rewrite the entire sketch, would it be possible to trigger that specific midi note whenever an activity is detected on the digital & analog inputs?
Cheers

Digital is easy, although you might want to do some de-bouncing on the switch contacts.

However as I said the value read out of pots is changing all the time, that is just the nature of analogue to digital conversion. So to prevent this noise from spamming your system with unwanted messages you have to do something like we said about waiting until a significant movement has occurred. Note you need to do this for every pot you want to monitor. That is why it would be useful to learn how to use arrays.

Also depending on if your rotary encoder is implemented correctly, that is by using a state machine. If not you will pick up four transitions for every click, and hence get four notes sent.

Don't forget about sending note off messages to prevent the notes from hanging.

Been 40 years since I played with MIDI :slight_smile:

  if (abs(rawValue - oldValue) >= 8) { // hysteresis
    oldValue = rawValue; // remember the change
    potValue = oldValue >> 3; // 10-bit to 7-bit

These changes should do it.
Leo..