Arduino 101 and Serial Object compatibility in Max7

Pauline Gloss's icon

Hi All, I'm wondering about compatibility between Arduino 101 and the Serial object in max.

Basically, I programmed my arduino to, when receiving a button press, to print a random value to the serial port. This works perfectly fine and I can read this action in the serial monitor on the Arduino software. However, I'm having trouble getting the Max serial object to read this value. (Port, baud rate, etc, are all correct. Also using metro for serial object).

FWIW: I also used the serial tutorial built straight into max and I couldn't read serial information there either. I thought I would program something even simpler to see if it would work and it doesn't.

Advice, etc?

Thanks,
Pauline

Scott Fitzgerald's icon

Make sure the Arduino serial monitor is closed when opening the port in Max.

Pauline Gloss's icon

Hey Scott, thanks. Yes. I recognized this pretty quickly. Unfortunately that's not the problem.

Also more info: I built a python work around where python pulls from the arduino serial port, reads the serial information from the 101 and passes it to max via OSC (via udprecieve object). This works fine and I'm able to communicate with max. Still not able to connect via Serial object, unfortunately..

Scott Fitzgerald's icon

What's your patch & Arduino code look like?

Pauline Gloss's icon

for the serial object, i built basically directly from this video: https://www.youtube.com/watch?v=6bT3G4Mep7E

the code has changed a bit now that i've built it up and i'm using it via python, but the basics stay the same. i was printing random values instead of specific values before and I only had one button:

//constants
const int buttonPin1 = 4;
const int buttonPin2 = 7;
const int ledPin = 13; //led push button, i think
//variables

int buttonPin1State = 0; //current state of button
int buttonPin2State = 0;

int lastButtonPin1State = 0; //previous button state
int lastButtonPin2State = 0;

void setup() {
// put your setup code here, to run once:

//initialize button on pin 4
pinMode(buttonPin1, INPUT);
//initialize button on pin 7
pinMode(buttonPin2, INPUT);
//initialize the LED
pinMode(ledPin, OUTPUT);
//initialize the serial communication
Serial.begin(9600);

}//end set up

void loop() {

//something I don't understand yet
while (!Serial) ;

//read button1 state
buttonPin1State = digitalRead(buttonPin1);
//read button 2 state
buttonPin2State = digitalRead(buttonPin2);

//compare buttonPin1 state against previous
if (buttonPin1State != lastButtonPin1State) {

//if the state has changed nothing
if (buttonPin1State == HIGH) {

} else {
//if current state is low, send value

Serial.println(1);
}
//in case of bounce
delay(50);
}

//save current state as last state for next loop
lastButtonPin1State = buttonPin1State;

//In parallel
//compare buttonPin2state against previous
if (buttonPin2State != lastButtonPin2State) {

//if the state has changed nothing
if (buttonPin2State == HIGH) {

} else {
//if current state is low, send value

Serial.println(2);
}
//in case of bounce
delay(50);
}

//save current state as last state for next loop
lastButtonPin2State = buttonPin2 State;

}

max patch was as follows (couldn't get any value to print raw from Serial):

{
    "patcher" :     {
        "fileversion" : 1,
        "appversion" :         {
            "major" : 7,
            "minor" : 3,
            "revision" : 3,
            "architecture" : "x86",
            "modernui" : 1
        }
,
        "rect" : [ 512.0, 79.0, 491.0, 622.0 ],
        "bglocked" : 0,
        "openinpresentation" : 0,
        "default_fontsize" : 12.0,
        "default_fontface" : 0,
        "default_fontname" : "Arial",
        "gridonopen" : 1,
        "gridsize" : [ 15.0, 15.0 ],
        "gridsnaponopen" : 1,
        "objectsnaponopen" : 1,
        "statusbarvisible" : 2,
        "toolbarvisible" : 1,
        "lefttoolbarpinned" : 0,
        "toptoolbarpinned" : 0,
        "righttoolbarpinned" : 0,
        "bottomtoolbarpinned" : 0,
        "toolbars_unpinned_last_save" : 0,
        "tallnewobj" : 0,
        "boxanimatetime" : 200,
        "enablehscroll" : 1,
        "enablevscroll" : 1,
        "devicewidth" : 0.0,
        "description" : "",
        "digest" : "",
        "tags" : "",
        "style" : "",
        "subpatcher_template" : "",
        "boxes" : [             {
                "box" :                 {
                    "id" : "obj-7",
                    "maxclass" : "number",
                    "numinlets" : 1,
                    "numoutlets" : 2,
                    "outlettype" : [ "", "bang" ],
                    "parameter_enable" : 0,
                    "patching_rect" : [ 155.0, 258.0, 50.0, 22.0 ],
                    "style" : ""
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-5",
                    "maxclass" : "newobj",
                    "numinlets" : 1,
                    "numoutlets" : 0,
                    "patching_rect" : [ 95.0, 252.0, 34.0, 22.0 ],
                    "style" : "",
                    "text" : "print"
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-6",
                    "maxclass" : "message",
                    "numinlets" : 2,
                    "numoutlets" : 1,
                    "outlettype" : [ "" ],
                    "patching_rect" : [ 348.0, 108.0, 34.0, 22.0 ],
                    "style" : "",
                    "text" : "print"
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-4",
                    "maxclass" : "newobj",
                    "numinlets" : 1,
                    "numoutlets" : 0,
                    "patching_rect" : [ 255.0, 289.0, 57.0, 22.0 ],
                    "style" : "",
                    "text" : "print raw"
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-3",
                    "maxclass" : "toggle",
                    "numinlets" : 1,
                    "numoutlets" : 1,
                    "outlettype" : [ "int" ],
                    "parameter_enable" : 0,
                    "patching_rect" : [ 171.0, 29.0, 24.0, 24.0 ],
                    "style" : ""
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-2",
                    "maxclass" : "newobj",
                    "numinlets" : 1,
                    "numoutlets" : 2,
                    "outlettype" : [ "int", "" ],
                    "patching_rect" : [ 171.0, 165.0, 79.0, 22.0 ],
                    "style" : "",
                    "text" : "serial c 9600"
                }

            }
,             {
                "box" :                 {
                    "id" : "obj-1",
                    "maxclass" : "newobj",
                    "numinlets" : 2,
                    "numoutlets" : 1,
                    "outlettype" : [ "bang" ],
                    "patching_rect" : [ 171.0, 94.0, 58.0, 22.0 ],
                    "style" : "",
                    "text" : "metro 33"
                }

            }
],
        "lines" : [             {
                "patchline" :                 {
                    "destination" : [ "obj-2", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "source" : [ "obj-1", 0 ]
                }

            }
,             {
                "patchline" :                 {
                    "destination" : [ "obj-4", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "order" : 0,
                    "source" : [ "obj-2", 0 ]
                }

            }
,             {
                "patchline" :                 {
                    "destination" : [ "obj-5", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "order" : 2,
                    "source" : [ "obj-2", 0 ]
                }

            }
,             {
                "patchline" :                 {
                    "destination" : [ "obj-7", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "order" : 1,
                    "source" : [ "obj-2", 0 ]
                }

            }
,             {
                "patchline" :                 {
                    "destination" : [ "obj-1", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "source" : [ "obj-3", 0 ]
                }

            }
,             {
                "patchline" :                 {
                    "destination" : [ "obj-2", 0 ],
                    "disabled" : 0,
                    "hidden" : 0,
                    "source" : [ "obj-6", 0 ]
                }

            }
],
        "dependency_cache" : [ ],
        "autosave" : 0
    }

}

Pauline Gloss's icon

ugh apologies on anyones eyes if the code didn't format correctly for the forum. i used the tags, but it still looks wacky over here

Scott Fitzgerald's icon

A coupel things.

1) Use "Copy Compressed" from the file menu when pasting Max patches
2) delete the while(!Serial) from your Arduino sketch
3) make sure you are communicating with the right port in max. Try the patch and sketch below to test communication

Max Patch
Copy patch and select New From Clipboard in Max.

Arduino code :


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int val = random(127, 255);
  Serial.println(val);
  delay(10);
}

Pauline Gloss's icon

Thanks again Scott. Apologies for the patcher formatting. But yes, unfortunately, I'm having the same problem here. Arduino monitor returns values correctly. I can close that and open max and I can print serial to get a list of ports. I can select the correct one, I can supposedly open the port but I return no values.

Scott_Patcher_Test.png
png
Scott Fitzgerald's icon

Well dang, I just tried it out with my 101, and you're right. The board won't post data to max, even though it is seen as a valid serial port.

However, jasch has a an alternative to the serial object called comport that does work with the 101 board (and I generally find to be a better serial interface than the vanilla c74 object). You can find it as part of the "jasch objects" package in the package manager.

I haven't seen any other programs that the 101 fails to send data to, not sure what is going on in the serial object that it won't show anything.

bt.soundmaker's icon

You may need to look into the objects [itoa] which is ascii conversion and [fromsymbol]

Pauline Gloss's icon

Thanks again, Scott. I'm honestly glad that you are reproducing the same problem. It makes me feel less crazy! (although hopefully there will be a max fix here). Will experiment with the Jasch objects!

Paul Benham's icon

Hi Pauline.
Did you managed to get data from the serial port using the 'comport' object? I'm struggling to get the same data (from a sonar sensor) with a 101 board as I do with an Uno.

Cheers

Paul