arduino mega and midi cc messages

ive hooked up 8 pots to a arduino mega with a midi to usb interface using these 2 tutorials:

http://little-scale.blogspot.com/2008/07/eight-pots-to-midi-cc-data.html

http://shiftmore.blogspot.com/2010/01/quick-and-dirty-arduino-midi-over-usb.html

Great work by both authors, it was very detailed and clear.

I managed to connect it to ableton live and got some midi data from pot 1. The problem is that it doesnt seam to respond to the others. After doing some research I found the probable causes: -the 4051, is there a fault in little scale's code? Maybe I have the wrong ic, it says 4051bp on mine. ive hooked it up exactly as he describes. -the arduino mega, does it work the same as the normal arduino used above? -code, I noticed there is no channel definition in the example above. then i found this: http://little-scale.blogspot.com/2007/11/more-pots-and-switches-to-midi-data.html Uploaded it and used the same 8 pot configuration. Although the midi light on the midi-ush hub flickered ableton was unable to read any signals.

There is another solution though. Since I have a mega i can hook up 16 pots directly to the board. The problem at the moment is I am an absolute beginner to programming and im relying on bits of code from the web. I purposely bought a mega for the extra inputs and find myself multiplexing my pots just so i can use some free available code.

I found the midi library and it sounds really promising. Can someone help me with the code for using 16 pots and optionally 54 switches without multiplexers?

b.t.w.: Is it actually faster to use 8 separate analog pins instead of multiplexing to one?

Ive did some more research and found a bit of code i edited to accept 8 pots on analog pins 0-7 and convert them into midi.

Programming is really hard for me to learn since im more used to metal and paint then code ;).

So I found the mega works with 8 pots and midi. But im still wondering if it's faster to read 8 seperate analog pins or multiplex to one.

The final midi controller is going to use at least 16 analog inputs and probably all 54 digital ones. Will this result in a big pile of latency or is it feasable?

Ill keep you updated on my progress and would appreciate any helpfull comments on this topic.

But im still wondering if it's faster to read 8 seperate analog pins or multiplex to one

AVRs have only one A/D converter, with a multiplexer (multi-way switch) on the input, so the more channels you add either with an external multiplexer, or the internal one, the more the latency.

I have made a synth which currently is using 13 pots on a MEGA with the MIDI library. instead of reading the analog inputs every loop, which is slow (analog read is the slowest function, takes over 100 cycles) it has a counter and a switch case statement. each case only reads 1 Potentiometer. that way the rest of the loop is not slowed down by 13 analog reads. the timing still feels fine because its only a slow human turning the knobs anyways.

as for the 64 buttons without a multiplexer? not likely, and if so, a wiring nightmare. i would look in to the "arduianome" monome clone which happens to have 64 buttons and 64 lights. with only 3 extra chips(i think) maybe you could build that, and adapt it to use the extra analog inputs of the mega.

(there is also software already made to control ableton with it) please see monome forums.

there is a small bug in arduino 17 though which stops the last 8 analog ins on the mega from working. here is the fix.

(http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250970792)

@ AWOL:

thanks, that is really helpfull. Running it with 8 pots has very little latency so i think it will handle my 16 pots fast enough.

@DeFex:

13 pot synth 8-), any pictures or code? Thanks for the fix for the other 8 analog ins, dont need it yet but I was bound to get stuck there down the road.

Im still doing more and more research and Im getting more and more stuck. Did a thorough read on the midi library but just cant make sense of the different commands :’(.

Im now trying to add 8 switches to the following code:
/**

  • midi_cc_test.pde
  • @author Benjamin Eckel
  • This program reads 6 analog inputs on analog pins (0-5).
  • It waits for the user to turn a knob and sends the value
  • as a midi control change out the TX port.
    */

#define inputs 8
#define e 1 // this is the delta needed in
// currentPot to send a message

*int currentPot[8] = {0,0,0,0,0,0,0,0}; *
int pot[8] = {0,0,0,0,0,0,0,0};
int channel[8] = {1,2,3,4,5,6,7,8};
void setup() {

  • Serial.begin(31250); // baud rate = 31250 for midi*
    } // = 38400 for serial debug
    void loop() {
  • for(int i=0; i<inputs; i++) {*
    _ currentPot = analogRead(i) / 8; _
    if(abs(currentPot_-pot) > e) {

    sendMidi(channel, currentPot*);_
    pot _= currentPot;
    }
    }
    }
    void sendMidi(char chan, char val) {
    Serial.print(0xB0, BYTE);
    Serial.print(chan, BYTE);
    Serial.print(val, BYTE);
    }*

    no succes as of yet, I guess ill just have to keep tinkering. If anyone can help me with the code for the pots and switches including the midi library I would appreciate it._

*update!

By means of tedious code searching and studying im starting to see how to read the code and manipulate it.

Ive managed to get the code working with 8 extra pushbuttons:

/**
 * MIDI Tjook
 * 8 analog and 8 digital in to MIDI
 *
 * Using code from:
 * midi_cc_test.pde
 * @author Benjamin Eckel
 */

#define inputs 8
#define e 1        // this is the delta needed in
                   // currentPot[i] to send a message
                   
int currentPot[8] = {0,0,0,0,0,0,0,0};    
int pot[8] = {0,0,0,0,0,0,0,0};
int channel[8] = {1,2,3,4,5,6,7,8}; 
int switchPin1 = 38;
int switchPin2 = 40;
int switchPin3 = 42;
int switchPin4 = 44;
int switchPin5 = 46;
int switchPin6 = 48;
int switchPin7 = 50;
int switchPin8 = 52;

char note1 = 60; //Middle C
char note2 = 61; //D
char note3 = 62;
char note4 = 63;
char note5 = 64;
char note6 = 65;
char note7 = 66;
char note8 = 67;

int switchState1 = LOW;
int switchState2 = LOW;
int switchState3 = LOW;
int switchState4 = LOW;
int switchState5 = LOW;
int switchState6 = LOW;
int switchState7 = LOW;
int switchState8 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
int currentSwitchState3 = LOW;
int currentSwitchState4 = LOW;
int currentSwitchState5 = LOW;
int currentSwitchState6 = LOW;
int currentSwitchState7 = LOW;
int currentSwitchState8 = LOW;

void setup() {
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
 pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
pinMode(switchPin5, INPUT);
pinMode(switchPin6, INPUT);
pinMode(switchPin7, INPUT);
pinMode(switchPin8, INPUT);
  
  Serial.begin(31250);      // baud rate = 31250 for midi
}                           //           = 38400 for serial debug

void loop() {
  for(int i=0; i<inputs; i++) {
    currentPot[i] = analogRead(i) / 8;    
    if(abs(currentPot[i]-pot[i]) > e) {
      sendMidi(channel[i], currentPot[i]);
      pot[i] = currentPot[i];
    }
  }
  { //switchPin1
  currentSwitchState1 = digitalRead(switchPin1);
  if( currentSwitchState1 == HIGH && switchState1 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note1, 0x45);
  if( currentSwitchState1 == LOW && switchState1 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note1, 0x00);
  switchState1 = currentSwitchState1; 
  
  //switchPin2
  currentSwitchState2 = digitalRead(switchPin2);
  if( currentSwitchState2 == HIGH && switchState2 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note2, 0x45);
  if( currentSwitchState2 == LOW && switchState2 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note2, 0x00);
  switchState2 = currentSwitchState2;
  
   //switchPin3
  currentSwitchState3 = digitalRead(switchPin3);
  if( currentSwitchState3 == HIGH && switchState3 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note3, 0x45);
  if( currentSwitchState3 == LOW && switchState3 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note3, 0x00);
  switchState3 = currentSwitchState3;
  
   //switchPin4
  currentSwitchState4 = digitalRead(switchPin4);
  if( currentSwitchState4 == HIGH && switchState4 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note4, 0x45);
  if( currentSwitchState4 == LOW && switchState4 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note4, 0x00);
  switchState4 = currentSwitchState4;
  
   //switchPin5
  currentSwitchState5 = digitalRead(switchPin5);
  if( currentSwitchState5 == HIGH && switchState5 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note5, 0x45);
  if( currentSwitchState5 == LOW && switchState5 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note5, 0x00);
  switchState5 = currentSwitchState5;
  
   //switchPin6
  currentSwitchState6 = digitalRead(switchPin6);
  if( currentSwitchState6 == HIGH && switchState6 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note6, 0x45);
  if( currentSwitchState6 == LOW && switchState6 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note6, 0x00);
  switchState6 = currentSwitchState6;
  
   //switchPin7
  currentSwitchState7 = digitalRead(switchPin7);
  if( currentSwitchState7 == HIGH && switchState7 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note7, 0x45);
  if( currentSwitchState7 == LOW && switchState7 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note7, 0x00);
  switchState7 = currentSwitchState7;
  
   //switchPin8
  currentSwitchState8 = digitalRead(switchPin8);
  if( currentSwitchState8 == HIGH && switchState8 == LOW ) // push
  //Note on channel 5 (0x94), some note value (note), middle velocity (0x45):
    noteOn(0x94, note8, 0x45);
  if( currentSwitchState8 == LOW && switchState8 == HIGH ) // release
  //Note on channel 5 (0x94), some note value (note), silent velocity (0x00):
    noteOn(0x94, note8, 0x00);
  switchState8 = currentSwitchState8;
}

}
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE); 
  Serial.print(data2, BYTE);
}

void sendMidi(char chan, char val) {
  Serial.print(0xB0, BYTE);
  Serial.print(chan, BYTE);
  Serial.print(val, BYTE);
}

As you can probably see, it’s all just hacked together bits of code. I know it’s not very efficient, but so far the latency has been very low and it works fine. Im trying to implement the part where it checks the pots in a similar way with the switches.

I also bought myself a rotary switch to do some experiments with. It’s a 12 way 1 ground version from alpha. To extend the capabilities of the controller I would like to be able to change the midi channel(1 to 12) with the rotary switch so i can have 12 different lay outs with 8 pots and 8 buttons 8-).

Ive found this page where someone uses a rotary switch as a stepped potentiometer:
http://www.tasankokaiku.com/jarse/?tag=arduino

Can I use the presets in the code to change the midi channel number?

Making code for the arduino has been a steep learning curve, but I think im getting there.

Today I modified my existing code with a toggleswitch to change between midichannel 1 and 2. I tested it first with a pushbutton on 2 channels and guess what? It worked, sweet succes of my own programming actually working!
Then I soldered in my toggleswitch and tried using it with the pots, and there it all went wrong again.

the code:

#define inputs 8
#define e 1        // this is the delta needed in
                   // currentPot[i] to send a message
                   
int currentPot[8] = {0,0,0,0,0,0,0,0};    
int pot[8] = {0,0,0,0,0,0,0,0};
int channel[8] = {1,2,3,4,5,6,7,8}; 

int togglepin1 = 2;
int togglestate1 = LOW;
int currenttogglestate1 = LOW;
int midichannel = 0x90;

void setup() {
  pinMode(togglepin1, INPUT);
  
  Serial.begin(31250);      // baud rate = 31250 for midi
}                           //           = 38400 for serial debug

void loop() 
{ //toggleswitch
  currenttogglestate1 = digitalRead(togglepin1);
  if( currenttogglestate1 == HIGH && togglestate1 == LOW ) // up
  //if toggle 1 is high, switch to midichannel 1:
  midichannel = 0x91;
  if( currenttogglestate1 == LOW && togglestate1 == HIGH ) // down
  //if toggle 1 is low, switch to midichannel 2:
  midichannel = 0x90;
  togglestate1 = currenttogglestate1;

}

{//<---------------------------------------------!
  for(int i=0; i<inputs; i++) {
    currentPot[i] = analogRead(i) / 8;    
    if(abs(currentPot[i]-pot[i]) > e) {
      sendMidi(midichannel, channel[i], currentPot[i]);
      pot[i] = currentPot[i];
    }
  }
}

void sendMidi(char midi, char chan, char val) {
  Serial.print(midi, BYTE);
  Serial.print(chan, BYTE);
  Serial.print(val, BYTE);
}

When I compile, I get this:
error: expected unqualified-id before ‘{’ token

the “{” in question is indicated (<----!) in the code.
I doublechecked for typos and mistakes but can’t seem to find anything. any help would be much appreciated.

Then I soldered in my toggleswitch and tried using it with the pots, and there it all went wrong again.

Did you solder the toggleswitch into the code?

You have a bunch of code following the closing curly brace of the loop function. That is not allowed. All code goes inside a function. Either in setup, in loop, or in a new function called from within setup or loop?

doh! :-X