arduino mega and max msp? ? ?

has anyone got their mega to work with max msp? i have tried all day with no joy. Huh
it get's recognized as a serial device/port. ( port usbserial-A7006R3i )
and when i send information to that port the little led rx flashes.
in the maxuino patch (for decimilia) it shows 13 pwm channels.. but these dont work either!
if anyone has got the digital pins to work? or could anyone tell me what message to send the serial port to switch digital pins on or off?
if anyone wants to send me a max patch with the mega working on it to make sure i didnt get a defective unit i'd be most great full.
thanks peeps

(sorry if this is second time someone reads but i posted in wrong section. my bad )

ok i now have the pwm output's working but still no luck with the digitals. anyone got any ideas? ? ?

I'm stuck on the same problem- if you get the Firmata 2.1 beta firmware it adds support for the Arduino Mega (at least it appears in the firmata.h code). But the maxuino patch doesn't parse it correctly.

This kind of parsing would be much easier in code than in a graphical language like max, I might end up making a max mxj object that does it. I'll share it if that's what I end up doing.

I'm working on figuring out how the protocol works...
It's laid out at V2.3ProtocolDetails - Firmata

I've figured out that you can turn on pin 13 in maxuino by sending the message "digital 15 1" and turn on pin 12 by sending "digital 14 1". However, it's more than just a simple offset, this trick doesn't work for the higher pins.

I made a slider that sent "digital $1 $2" messages into the "p command processing" object in that maxuino patch. I found I was able to turn pins on somewhat unpredictably.

For instance, "digital 15 1" turns on pin 13" and "digital 15 0" turns it off. It's more than an offset issue though, if you give it higher numbers it starts to do weird things like turn on groups of LEDs. I'll double check this when I have more time, I may just be doing something dumb. I would love for this just to be an offset issue, but I think it has to do with the patch not mapping properly to the arduino mega's 8 bit ports.

You can email me at dfine at sonic dot net if you want to try and figure this out together-

I may still write an mxj object for firmata on Arduino Mega. But I need something ASAP so I wrote a quick and dirty I/O protocol for it. Feel free to use and modify.
You can download it and a max example patch at sonic.net/dfine/spurious

Just keep in mind, it's quick and dirty and barely tested!

/*
 /*
  *This code turns an arduino mega into an I/O serial board. This version is quick and dirty, and formatted terribly.
  The first thing
  anyone should do is change the variables and loop values to reflect the number of pins actually used.
  This version is hard coded to use pins 2-39 for input and 40-53 for output (reserving 13 for the arduino test LED.)
  Analog reads could be added pretty easily. 
  Don't forget to pull-down to ground each digital input pin through a 10 to 100K ohm resistor, or readings will be shaky. 
  The following cod sends single byte commands/readings to and from another serial device. 
  The highest two bits of each byte are the command type flags, and the following 6 are payload 
  (an analog value with 64 point resolution or, in the case of digital operations, a pin number 0-63 )
  This protocol supports 64 digital inputs, 64 digital outputs, 2 Analog readings, and 2 PWM outputs (with 64 point resolution).
  10xxxxxx = digital pin set LOW  (do a bitwise 'or' 128)
  11xxxxxx = digital pin set HIGH (bitwise 192)
  00xxxxxx = Analog value device 0  (bitwise 0)
  01xxxxxx = Analog value device 1 (bitwise 64)
  So as you see, a limitation of this single byte encoding is you only get 2 analog devices. 
  *The readPins loop sends readings from digital and analog input pins to another device over serial. It only sends values for pins whos state has changed.
  *The setPins loop takes incoming serial bytes and sets pins accordingly
  You'll see a lot of bit operations doing "value & 63" , since 192 is 00111111 and will clear out the highest two bits, leaving just the payload.
  */
  
int digInputs[40]; // array stores values of digital input pins 2-39. Bigger than it needs to be so i don't have to worry about offsets.
byte count=0;  // used in for loops a bunch
byte readPin;  // digital input pin being read
byte incomingSerialByte; //Takes data from serial.read for immediate processing
byte currentPinValue; //reads current input pin for comparison with 
byte messageByte;
byte ADC0=0; //Stores value of ADC to send to serial, after being mapped to 6 bit.
byte ADC1=0;
byte decodedPin; //stores lower 6 bits, or "payload" of incoming message byte
byte decodedFlag; //Stores higest 2 bits, or flag bits, of incoming message byte


void setup()                    // run once, when the sketch starts
{
  for(count=40;count<=53;count++){
  pinMode(count, OUTPUT);      // sets pins 40 thru 53 as output
  }
  for(count=2;count<=39;count++){ 
  pinMode(count, INPUT);  // sets pins 2 thru 39 as input
  }
  pinMode(13, OUTPUT); // Set the testing LED pin, comment out if you want to use pin 13 for other stuff.
  for(count=0;count<=36;count++){  // initialize array with 0's
    digInputs[count]=0;
  }
}

void loop()                     // run over and over again
{
  Serial.begin(57600);
 // Serial.flush(); //why not?

//This loop reads digital inputs and sends report bytes to serial out
  for(readPin=2;readPin<=39;readPin++)  
    {  
      if(readPin!=13){      //pin 13 is the test LED on the board.

     currentPinValue = digitalRead(readPin);
      if(currentPinValue != digInputs[readPin])//if value has changed from previous reading
      { 
        digInputs[readPin] = currentPinValue;  //update array to track new current value
        switch(currentPinValue){
          case 1:
          messageByte=(192 | readPin);  
          Serial.print(messageByte, BYTE); 
          // The byte will be 11.xxxxxx where last 6 contain number of pin. 11 means "digital high"
          case 0:
          messageByte=(128 | readPin);  // The byte will be 10.xxxxxx , 10 means "digital low"
         Serial.print(messageByte, BYTE); 
          default: 
          break;
        }
      }
       // Serial.print(messageByte, BYTE);

      }
    }
    
    // Move on to reading input from another serial device
   
    for(count=1;count<=8;count++) //This loop reads up to 8 bytes from serial buffer and sets corresponding digital output pins on arduino
    {
    if(Serial.available()==0)  
    {
      break;  //Skip if no serial data has been received
    }
    incomingSerialByte=Serial.read();
    // delay(5); // may not be needed
    decodedFlag=(incomingSerialByte&192);
    decodedPin=(incomingSerialByte&63); //Strips first 2 flag bits, leavig just payload "pin number"

    switch(decodedFlag) //the &192 strips all but first 2 bits of the byte.
    {
      case 128:  //the flag bits were 01, signals to set pin low
      digitalWrite(decodedPin, LOW);
      break;
      
      case 192: //flag bits were 11, so set pin high
      digitalWrite(decodedPin, HIGH);
      break;

    }
    }
}
    
 
//case 64:
/* If you wanted to read in analog values from another device, this is where you'd do it.
You'd have to do an &63 to get just the lower 6 bits.
For PWM output, you'd then map the 0-63 value
to the 0-1023 range. And set a pin to write to, of course.
*/
//case 0:
/*This would be available for a second analog value.
It could be a second PWM. Or, if you want more than 2 PWM outputs,
you could get sneaky and use this value to set a variable which the previous case uses
to determine which PWM to address.
}

So I have been attempting to get maxuino mega ready, but as you said, firmata 2.1 is clearly outputting a very different set of signals for the mega than for the regular arduinos, and I am not sure that is supposed to be the case. the biggest issue is that while running firmata the mega seems to jam up right after starting up and while it can receive, it will not send any info over the serial connection. avidd you seem to be taking the best route for now. I also tried to use my custom code for talking between max and the motor shield with the mega, and while the motor shield library is supposed to be ready for the mega, it will not run on the mega board either. im going to try this protocol just to make sure my boards are working....

hummm well i shall see what i can do. thanks for the help peeps. :smiley: ... if i get anything working i will also post it up :smiley: (i think im a few steps (lol miles) behind you though)
take it easy.

Hi

I am also experiencing a problem with using a new Arduino Mega with Max/ MSP. I have successfully been using Maxuino-005 and standard firmata to read the analog pins from the Duemilanove board but got the Arduino Mega for the extra inputs which I now need.

I have attempted to rewrite the Arduino Max patch to account for the additional analog inputs but the patch is still just reading analog inputs 0-5 and not reading inputs 6-15 at all. There are a number of posts about this and I was hoping that somebody may have come up with a solution to this.

What I want to do is read the analog inputs from 0-15 and also send out data from max to a number of digital i/o on the mega board.

I have also tried the serial object in max/msp and also cannot get this to read any of the inputs from the mega board.

Avidd I was going to try your solution.. what is the file called on your site because there is a whole list and I took a quick look and could not find a max patch for this.

Has anybody got anywhere with this?? Any help/solutions/suggestions??

Best :-?

Hi, Paul here... the guy who's working on hardware abstractions for Firmata.

Here is a link to the alpha test code.

http://www.pjrc.com/tmp/firmata/readme.html

I have tested this on Arduino Mega and both Teensy boards and it's believed to work, but Hans really needs more feedback from users before it'll be merged into the official Firmata.

Please, please, please email me (paul at pjrc dot com) and Hans (hans at at dot or dot at) and let us know you tried it on Arduino Mega (or Teensy, or Sanguino) and what worked and what didn't work.

Who made the Maxuino Patch?? he or she or they should know how or what we need to change in the maxpatch. I've been trying somehow by declaring or calling the rest of the pins in the firmware but it doesnt work. I also tried the code above but maxuino dont read the rest of the pins. Anyone knows???

I made a new subject on this, but do you have any news from it, me i can't even have pwm, how did you get it kielo?
thanks

I am able to communicate my Mega with max 5 using Arduino 2 Max v.4. I adjusted the IDE code to extend the analog input from 0-5 to 0-15 but found after doing this only analog pins 6-17 will communicate to max.

then after some trial and error, realised that the most pins I can get to work is from 0-13, and when I try to add the last 2 pins into the IDE code the first 6 dont work.

why?! sigh.

Im pretty sure its something to do with the arduino code and not the max patch, but I cant be sure.

Does anyone have any idea?