Send midi pitch bend with a joystick

hi everybody,

I try to send midi pitch bend with a joystick it moves but always down and i don't understand why. At middle no moves it's okay.

This is my code:

#include <MIDI.h>


MIDI_CREATE_DEFAULT_INSTANCE();

int lastAnalogValue;
void setup()
{
   Serial.begin(31250);
    MIDI.begin (MIDI_CHANNEL_OMNI);           
}

void loop()
{


  int analogValue = analogRead(A0);
  

  if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) 
  
  {

    if (analogValue != lastAnalogValue && analogValue > 516) {

      MIDI.sendPitchBend(analogValue*(-8), 1);
      lastAnalogValue = analogValue;
    }
  

    if (analogValue != lastAnalogValue && analogValue < 508) {

      MIDI.sendPitchBend(analogValue* (8) , 1);
      lastAnalogValue = analogValue;
    }
     }
     
      }

Verify first that your joystick does give you a full range between 0 and 1023 and 512 in the middle, when idle

If so then try this code

void loop() {
  int analogValue = analogRead(A0) - 512; // 0 at mid point
  if ( abs(analogValue-lastAnalogValue) > 1)  { // have we moved enough to avoid analog jitter?
    if ( abs(analogValue) > 4) { // are we out of the central dead zone?
      MIDI.sendPitchBend(8*analogValue, 1); // or -8 depending which way you want to go up and down 
      lastAnalogValue = analogValue;
    }
  }
}

Note that you can pass something between -8192 (maximum downwards bend) and 8191 (max upwards bend) and center value is 0. As we have a analogValue Between -512 and +511, multiplying by 8 just gives you half the range. You could multiply by 16 if you want

(Typed here from my mobile so totally untested)

1 Like

It works perfect and thank you the advice (multiply)

Good ~ have fun !!

Hi all, Thanks for this useful post.

I was attempting to build something similar following the instruction in this blog post

However the results were not convincing.

If i'd like to print the raw values of the analog read and the resulting midi messagge on the serial monitor how should i do it?

I tried with this code but it does not works:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

// Onboard led
const int ledPin =  LED_BUILTIN;// the number of the LED pin
const int mididelay = 2;
int lastAnalogValue;
void setup()
{
   Serial.begin(9600);
    MIDI.begin (MIDI_CHANNEL_OMNI);           
}

void loop() {
  int analogValue = analogRead(A0) - 512; // 0 at mid point
  if ( abs(analogValue-lastAnalogValue) > 1)  { // have we moved enough to avoid analog jitter?
    if ( abs(analogValue) > 4) { // are we out of the central dead zone?
      MIDI.sendPitchBend(16*analogValue, 1); // or -8 depending which way you want to go up and down 
      lastAnalogValue = analogValue;
      digitalWrite(ledPin, HIGH);
      delay(mididelay);
      digitalWrite(ledPin, LOW);
    }
  }

  Serial.print("vx = ");
  Serial.print(lastAnalogValue);
  Serial.print("\t bend = ");
  Serial.print(16*analogValue);

}

I tried by changing the baud rate in both the monitor and the sketch but i keep getting unreadable characters.

Sorry if my request is trivial, but I don't know how to code in C and how use IO from arduino.

Many thanks

I suspect MIDI is using the hardware serial port and setting it to 31250 baud.
If you want to log to a second serial port you'll need either an Arduino with
several serial ports, or to use one of the software serial librarys.

MIDI works by using 31250 baud on a serial port. Perhaps you meant to set it up
on a different port?

Sorry i was not clear. Indeed 31250 baud rate is necessary to work with midi,from what i read around.
What i meant to ask is having a way of reading the midi messages in the serial console, without necessarily having an active communication with a midi device.

So the idea would be to use a different baud rate just to read some out values while customizing the code.

The 31250 baud rate is a problem for reading serail monitor.
Also serial print function causes troubles with midi messages don't use it.
I use sometimes an LCD screen to read values

Then get a serial program on the host that allows a 31250 baud rate to be configured. MIDI is binary, not ASCII, note.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.