How to Map midi

Hi guys,

could anyone help out to map this code to MIDI ?

int potPin1 = A0;
int potValue1 = 0;

int potPin2 = A1;
int potValue2 = 0;

int potPin3 = A2;
int potValue3 = 0;

int potPin4 = A3; 
int potValue4 = 0;

//Joystick/thumbstic Pins

const int SW_pin = 8; // digital pin connected to switch output
const int X_pin = A4; // analog pin connected to X output
const int Y_pin = A5; // analog pin connected to Y output

const int pushButton1 = 7;

const int pushButton2 = 5; 

const int pushButton3 = 6;

const int Toggle = 9;

int toggleState = 0; 

int LED1 = 10;

void setup() {
 pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
 pinMode(pushButton1, INPUT);
  digitalWrite(pushButton1, HIGH);
 pinMode(pushButton2, INPUT);
  digitalWrite(pushButton1, HIGH);
 pinMode(pushButton3, INPUT);
  digitalWrite(pushButton1, HIGH);
 pinMode(Toggle, INPUT);
  digitalWrite(pushButton1, HIGH);
 pinMode(LED1, OUTPUT);
void loop() {

  Serial.print("Switch:  ");
  Serial.print("X-axis: ");
  Serial.print("Y-axis: ");


  int button1State = digitalRead(pushButton1);
 // print out the state of the button
 Serial.print("button1 =");
  int button2State = digitalRead(pushButton2);
 // print out the state of the button
 Serial.print("button2 =");

  int button3State = digitalRead(pushButton3);
 // print out the state of the button
 Serial.print("button3 =");

  int button4State = digitalRead(Toggle);
 // print out the state of the button
 Serial.print("toggle =");

 potValue1 = analogRead(potPin1);  
 Serial.print("potentiometer1 = " );    

 potValue2 = analogRead(potPin2);  
 Serial.print("potentiometer2 = " );     
 potValue3 = analogRead(potPin3);  
 Serial.print("potentiometer3 = " );     

 potValue4 = analogRead(potPin4);  

 Serial.print("potentiometer4 = " );    

 //Toggle turning on LED

toggleState = digitalRead(Toggle);   //Toggle

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (toggleState == HIGH) {
    // turn LED on:
    digitalWrite(LED1, HIGH);
  } else {
    // turn LED off:
    digitalWrite(LED1, LOW);

...I've actually never used MIDI.

Basically what you've got is a "input hardware test" program. If that's working, great.

Do you have a "traditional" serial MIDI connection with the round connector, or are you using USB-MIDI?

Next, I'd suggest you figure-out how to send just one or two MIDI commands (with no pots, switches or any input) before you try integrating all of that hardware.

Then if you want to program a switch to send note-on and note-off commands or if you want to [u]map()[/u] the default 0-1023 pot reading to a 0-127 MIDI velocity, etc., that should be easy enough. But, I'd recommend that you work on one control/message at a time/


I am using USB-MIDI (for MAX MSP routing), I can send one button at the time by using this code

/* send midi note on/off when button is pressed on Pin 2 */

#include "MIDIUSB.h"  
void setup()
    pinMode(2, INPUT_PULLUP);

void loop()
    if( digitalRead(2) == false ) 
        // note on 
        midiEventPacket_t noteOn = {0x09, 0x90, 60, 100}; 

        // note off 
        midiEventPacket_t noteOff = {0x09, 0x90, 0, 0}; 

(same with one analog in) but I cant figure out how to send multiple ones and how to define them

If you just want MIDI output, you can use my MIDI Controller Library. Have a look at this example.

If you want more features, such as MIDI input, you can check out my Control Surface Library. It's the successor of the MIDI Controller library, but I haven't finished all examples and documentation yet.

The advantage of using one of these libraries is that they handle many things for you, like debouncing of the buttons, and filtering and mapping of the analog inputs. They also support multiplexers and other hardware expanders if you ever need more inputs.


Thank you so much, this is perfect ! knobs, buttons and joystick works very well, however having issues with rotary encoder: it only goes two ways, either to 1 or 127, what could be a problem?

hdxn: however having issues with rotary encoder: it only goes two ways, either to 1 or 127, what could be a problem?

That's normal, rotary encoders only send out relative position changes, rather than an absolute position. This is actually an advantage for MIDI controllers, since the initial value in the DAW software doesn't have to match the physical position of the knob. Your DAW should have a setting for relative MIDI control. What software are you using?

PieterP: Your DAW should have a setting for relative MIDI control. What software are you using?

I am using Max Msp

I have no experience with Max Msp, but this seems to be the answer:

In relative mode, you just sum (accumulate) all values you receive. There's a catch, though: all values are encoded as 7-bit two's complement. So if a value is greater than or equal to 64 (0x40), you have to subtract 128 (0x80).

0 → 0 1 → +1 2 → +2 ... 63 → +63 64 → -64 ... 126 → -2 127 → -1

Alternatively, you can use the BINARY_OFFSET mode of the MIDI Controller library: It always adds 64 to the value, which might be easier to deal with in Max Msp: 0 → -64 1→ -63 ... 63 → -1 64 → 0 65 → +1 ... 126 → +62 127 → +63