Help me adapt this code for MIDI?

Using this code with the encoder library from teensy (Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals) I’ve gotten really good results by connecting pins A and B of the encoders directly to interrupt pins on ArduinoMega. (By good results I mean the serial monitor can detect very little changes in the movement of the encoder). I know need to adapt this code to print MIDI CC messages as the two encoders turn. Could anyone help me with this? I can’t find any resources on the web talking about encoders and MIDI.

/* Encoder Library - TwoKnobs Example

#include <Encoder.h>

// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder knobLeft(5, 6);
Encoder knobRight(7, 8);
// avoid using pins with LEDs attached

void setup() {
Serial.begin(9600);
Serial.println(“TwoKnobs Encoder Test:”);
}

long positionLeft = -999;
long positionRight = -999;

void loop() {
long newLeft, newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = “);
Serial.print(newLeft);
Serial.print(”, Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println(“Reset both knobs to zero”);
knobLeft.write(0);
knobRight.write(0);
}
}

Some suggestions:

Learn about posting code using code tags (the # button above your message).

Learn about encoders. Learn about MIDI. Learn about Arduino and C. Put the knowledge together.

PS. Sorry, but I don't know the Encoder library, and I don't know about MIDI.

Prove it. Post your IDE .ino file as an attachment using the "Additional Options" and post some screenshots or some other documentation that proves your getting good results. (that means a text terminal capture file from a program like REALTERM). In order to help you we need to see what you have to work with, hence above instructions. Let's go over the ground rules here on the forum. You present your project You post the reference documentation (if you want to talk about MIDI that's fine but we don't particularly care about MIDI so we may or may not know what you are talking about. That's fine. We're professionals. When we know something we research on Google, like you should have and post links to sites that list the MIDI CC messages. That's your job, not ours. There's probably a hundred hits on MIDI CC messages but I'm not going to post any of them because you should have already done that: ie: " I have this project that involves mapping encoder rotation to MIDI CC messages based on the following rules: 1 rule 1 2 rule 2 blah blah blah, "Here is the necessary reference documents you should need. If you need any other information , please let me know and I'll get it." Thank you, Joe Newbie

Furthermore, this:

I know need to adapt this code to print MIDI CC messages as the two encoders turn.

is not going to cut it as a Design Criteria. You're going to have to sit down, pour yourself a tall cup of coffee , print out the MIDI list and make a a truth table or some kind of documentation or reference that defines the criteria for these messages. Look at what you have given us ? Are we going to write a program that has code like this ?

IF SHAFT == TURNING 
{PRINT SOME CC MESSAGES]

We don't want to have to play 20 questions every single day. Please provide ALL the information necessary to complete the project, as the line is out the door. Come back when you have the above and we can move on.

I can't find any resources on the web talking about encoders and MIDI.

Of course not because there is no connection. One doesn't care about the other. The SHAFT is not MIDI's problem, it's Mr. uC's problem. as is MIDI. It's Mr. uC job to interface with Mr. Shaft, and Mr. Midi. "Hello Mr. Shaft, how are you turning today ? "Oh hello Mr. uC, I'm rotating CW just fine, thank you very much. would you care for some encoder counts? They're fresh baked .." "Oh , thank you so much Mr. Shaft, excuse me, I just remembered I need to pick up some MIDI CC messages for my wife.., good day !"

Well let me be a bit more specific then :stuck_out_tongue:
Let’s look at one encoder for now. I’ve scrapped the idea of CC messages as it is easier to send movement as note on’s and note off’s. (One respective note for each direction of turn) For example,
what should happen, is if the encoder is turned right, send a note on message (doesn’t matter what note in particular). If it stops turning right, send a note off message for that note we just turned on.
If the encoder turns left send a different note on message, and if it stops turning left, send a note off message for that note we just turned on.

The library I posted (Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals) gives an example project, I’ve (attempted to) modifiy it in order to do what I want it to. This is my first go, so it’s not great! - Arduino is just spewing out values.

#include <Encoder.h>
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>


// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(18, 19);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("Basic MIDI Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();   //"myEnc.read" will read the position of the encoder (can be negative or positive) 
  if (newPosition > oldPosition) {    //if the new position is greater than the old position (which means clockwise turn) send a note on
    oldPosition = newPosition;
    MIDI.sendNoteOn(100,127,1);   //synax is: note 100, 127 velocity(meaning note on), channel 1
  }
else {
  MIDI.sendNoteOff(100,0,1);      //if the turning has stopped send a note off for that same note
if(newPosition < oldPosition) {    //if the new position is less than the old position (CW turn) 
     oldPosition = newPosition;
    MIDI.sendNoteOn(99,127,1);      //send a different note on
    }
   else {
     MIDI.sendNoteOff(99,0,1);         //if not, turn that note off
   }
}




//end
}

If by "spewing out values" you mean that there are a lot of Note Off commands, well, that is what your logic says to do.

It would help to see this if you would use the IDE to reformat the code. The compiler doesn't care about the way that you indent, but you and I care.

I use Eclipse, not the Arduino IDE, but I think the command is Ctrl-Alt-T in the Arduino IDE.

I believe that your logic is such that every time through loop (which may be 100's of thousands of times per second), you command a Note Off for BOTH notes if the encoder has not moved. You probably only want to do this once, and only on one note.

We like to provide answers. What was your question?

Yeah the arduino is constantly sending messages, where as I want it to send a message only when the encoder is moving, or has just stopped moving. What is the best way to set this out? I'm trying a few different methods but none act exactly how I want them to. I will do the indent thing as well!

Maybe an interrupt would solve that problem.

if you don't want messages to be sent the whole time, you just need to check if the encoder has a NEW value before sending it. something like this:

// read the encoder and save value to valiable
// compare variable of NEW value with LAST value
// if the value if DIFFERENT, send the value
// if it is the same, do nothing
// make LAST value = to NEW value

What are you trying to build? I have built a couple of MIDI controllers using an Arduino and it has been great fun. I didn't use any encoders though. I have used buttons, leds and some pots (normal knobs). The way i have done it was like this: - analog read the pot value (it will give a value between 0 - 1023) - midi will take values from 0 - 127, so i map the pot value to the midi range - then a midi message needs several pieces of information, so you need to decide which information you want your pot to send.

On my last project i decided to send cmd, pitch and velocity for each element (either a button, a pot,...). I have created a function that would send this data via serial:

void noteOn (int cmd, int pitch, int vel){
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(vel);
} // end void noteOn

then when i would read the value (let's say from a pot) i would compare it to the last value (so it send a message only if there is a new value) and send something like this:

noteOn(0xB0, 20, mappedPotVal)

here is a great article about MIDI, so you can understand how MIDI works, which kind of info is sent on a MIDI message, .... It helped me a lot: http://www.music-software-development.com/midi-tutorial.html

but there was still another thing. On the computer side, the computer can't use the serial messages as MIDI, so i still had to convert them. But that wasn't so much trouble because there are some converters available which do all the work for you. I use the Spikenzielabs Serial-MIDI converter, and it has always worked great. You can get it here: http://www.spikenzielabs.com/SpikenzieLabs/Serial_MIDI.html

So, i guess it all depends on what you are building, what kind of MIDI message you need to send. But the idea is always kind of the same... ;)

Good luck! =)