Problem trying to send data from Max/MSP to Arduino with Serial.read

chewiesmissus's icon

Hi there,

I am trying to send 3 pieces of information from Max/MSP using the serial object into Arduino using Serial.read. At the moment I am sending from Max just the numbers 1, 2, and 3 in a single message.

The 3 bytes appear to send from Max (pop-up says write 3) but then serial.read (of which I have 3) seems to be returning -1 for 2 out of every 3 readings as follows.

Reading 1 = 1
Reading 2 = -1
Reading 3 = -1
Reading 1 = 2
Reading 2 = -1
Reading 3 = -1
Reading 1 = 3
Reading 2 = -1
Reading 3 = -1

Here is a copy of the Arduino code

int settings[3];

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

void loop() {
if (Serial.available()) {
// put your main code here, to run repeatedly:

settings [0] = Serial.read();
settings [1] = Serial.read();
settings [2] = Serial.read();

Serial.print("Reading 1 = ");
Serial.println(settings [0]);
Serial.print("Reading 2 = ");
Serial.println(settings [1]);
Serial.print("Reading 3 = ");
Serial.println(settings [2]);

}
}

And of the Max patch

{
 "boxes" : [ {
 "box" : {
 "maxclass" : "message",
 "text" : "1 2 3",
 "numoutlets" : 1,
 "patching_rect" : [ 194.0, 78.0, 37.0, 22.0 ],
 "id" : "obj-26",
 "outlettype" : [ "" ],
 "style" : "",
 "numinlets" : 2
 }
 }
, {
 "box" : {
 "maxclass" : "message",
 "text" : "print",
 "numoutlets" : 1,
 "patching_rect" : [ 115.0, 115.0, 34.0, 22.0 ],
 "id" : "obj-10",
 "outlettype" : [ "" ],
 "style" : "",
 "numinlets" : 2
 }
 }
, {
 "box" : {
 "maxclass" : "newobj",
 "text" : "print status @popup 1",
 "numoutlets" : 0,
 "patching_rect" : [ 254.0, 218.0, 138.0, 23.0 ],
 "id" : "obj-28",
 "fontname" : "Arial",
 "style" : "",
 "fontsize" : 13.0,
 "numinlets" : 1
 }
 }
, {
 "box" : {
 "maxclass" : "newobj",
 "text" : "serial d 9600",
 "numoutlets" : 2,
 "patching_rect" : [ 194.0, 144.0, 79.0, 22.0 ],
 "id" : "obj-1",
 "outlettype" : [ "int", "" ],
 "style" : "",
 "numinlets" : 1
 }
 }
 ],
 "lines" : [ {
 "patchline" : {
 "source" : [ "obj-26", 0 ],
 "destination" : [ "obj-1", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }
 }
, {
 "patchline" : {
 "source" : [ "obj-10", 0 ],
 "destination" : [ "obj-1", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }
 }
, {
 "patchline" : {
 "source" : [ "obj-1", 1 ],
 "destination" : [ "obj-28", 0 ],
 "hidden" : 0,
 "disabled" : 0
 }
 }
 ],
 "appversion" : {
 "major" : 7,
 "minor" : 3,
 "revision" : 3,
 "architecture" : "x86",
 "modernui" : 1
 }
}


Can anybody help me to get the readings in order?

Thanks in advance!

Asha

Source Audio's icon

You must get the data coming from max as a string or as array

in order to read them as a group. So read the Arduino tutorials,

which cover string reading, arrays etc

chewiesmissus's icon

You must get the data coming from max as a string or as array
in order to read them as a group. So read the Arduino tutorials,
which cover string reading, arrays etc

Thanks Source Audio. I didn't realise max was actually sending the data as 3 bytes each time and the serial read was reading each byte as one read hence the extra readings. I have since modified the arduino code slightly as below and it seems to do as I need.

byte settings[3] = {0, 0, 0};

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

void loop() {
  if (Serial.available() >= 3) 
  {
  // put your main code here, to run repeatedly:

settings [0] = Serial.read() ;
settings [1] = Serial.read() ;
settings [2] = Serial.read() ;

  Serial.print("Reading 1 = ");
  Serial.println(settings [0]);
  Serial.print("Reading 2 = ");
  Serial.println(settings [1]);
  Serial.print("Reading 3 = ");
  Serial.println(settings [2]);     

  }
}

Source Audio's icon

Max is sending 3 bytes because You decided to do so, without delimiter

or some other kind of routing which would help You to read and assign

received values to certain destinations in Arduino.

Grouping values in array of defined length is ok, but sometimes

it could be easier to prepend something like A for destination 1, B for destination 2

and so on. Then You just need to send Values which changed and need updating.

Otherwise You must send the whole array of values, even if only one member of the whole string

changed.

chewiesmissus's icon

Thanks for your reply! I am working on learning these things. Cheers