Can't Route data from Arduino into Max

R_Gol's icon

Hi,

I'm using the following patch:

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


With the following arduino code:

unsigned long sensorA;
unsigned long sensorB;
unsigned long sensorC;
void setup()
{
Serial.begin(9600);
}


void loop()
{
sensorA = 150;
sensorB =50;
sensorC =250;
Serial.print("A");
Serial.print(sensorA);
Serial.print("B");
Serial.print(sensorB);
Serial.print("C");
Serial.print(sensorC);
delay(100);
}

Can't receive the data in Max.
Anyone can spot my issue?


R_Gol's icon

Another question in the same subject:
I try to send multiple messages from MaxMSP to arduino in order to control 4 different relays.

I manage to send data from max into arduino and turn a relay on and off. I can't find a way how can I send 4 different messages from max to Arduino in order to control 4 different relays.
What I have now that is working is that max patch:

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


with the following arduino code:

int relayOne = 2;
void setup()
{
  Serial.begin(9600);
  pinMode(relayOne,OUTPUT);
}
void loop()
{
  dataFromMax();
}
void dataFromMax()
{
  if (Serial.available()) {
    int value1 = Serial.read();
    if(value1 == 1){
      digitalWrite(relayOne, HIGH);
    }
    else if (value1 == 0){
      digitalWrite(relayOne, LOW);
    }
  }
}



how can I expand the code above to control 4 messages of 0 and 1 from max to turn on and off 4 different relays modules? The max patch should look something like that:

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


Source Audio's icon

make as many if statements to match as many needed values.

if xx == 1 then
if xx == 2 then
if xx == 3 then
and so on.

R_Gol's icon

How do I send those values from Max to arduino? using the pad object?

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

and in Arduino?

int relayOne = 2;
int relayTwo = 3;
int relayThree = 4;
void setup()
{
  Serial.begin(9600);
  
  pinMode(relayOne,OUTPUT);
  pinMode(relayTwo,OUTPUT);
  pinMode(RelayThree,OUTPUT);

}
void loop()
{
  dataFromMax();
}
void dataFromMax()
{
  if (Serial.available()) {
    int value1 = Serial.read();
    int value2 = Serial.read();
    int value3 = Serial.read();
    if(value1 == 1){
      digitalWrite(relayOne, HIGH);
    }
    else if (value1 == 0){
      digitalWrite(relayOne, LOW);
    }
     if(value2 == 1){
      digitalWrite(relayTwo, HIGH);
    }
    else if (value2 == 0){
      digitalWrite(relayTwo, LOW);
    }
    if(value3 == 1){
      digitalWrite(relayThree, HIGH);
    }
    else if (value3 == 0){
      digitalWrite(relayThree, LOW);
    }
  }
}
Source Audio's icon

do you have trouble assigning 6 numbers ?
for example :
relay 1 send 1 or 0
relay 2 send 2 or 3
relay 3 send 3 or 4
or any other number.
you don't know how to use some max object to
convert toggle 1 and 0 to other 2 numbers ?

R_Gol's icon

I know that method:

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

The issue I might face is - What happen if both relay sending data at the same time? in that case one of the data won't send? or will be send in delay?


edit: 
and here is the Arduino patch:

int relayOne = 2;
int relayTwo = 3;
int relayThree = 4;
void setup()
{
Serial.begin(9600);

pinMode(relayOne,OUTPUT);
pinMode(relayTwo,OUTPUT);
pinMode(relayThree,OUTPUT);

}
void loop()
{
dataFromMax();
}
void dataFromMax()
{
if (Serial.available()) {
int value1 = Serial.read();
if(value1 == 0){
digitalWrite(relayOne, LOW);
}
else if (value1 == 1){
digitalWrite(relayOne, HIGH);
}
if(value1 == 2){
digitalWrite(relayTwo, LOW);
}
else if (value1 == 3){
digitalWrite(relayTwo, HIGH);
}
if(value1 == 4){
digitalWrite(relayThree, LOW);
}
else if (value1 == 5){
digitalWrite(relayThree, HIGH);
}
}
}



it is indeed working fine, Just wondering if there is smarter/efficent way to do the above without the need to write so many if(value1 == ){} else if and so one

Source Audio's icon

what is wrong with so many if's ?

you dont need else, only 6 if statements

R_Gol's icon

what is wrong with so many if's ?
you dont need else, only 6 if statements

Generally - nothing wrong, is just that I'm thinking what if I would like to send 20 signals from max msp to arduino to control 20 different objects with arduino (LEDs, Relay, etc) it will soon become messy.
This is why I'm asking about more elegant way of doing so.

Source Audio's icon

You will need to learn how to parse serial data.
because you might need more than just 255 numbers
and also more than just on - off state.

But at the end - if you want to control anything individually
in arduino using serial port, you must address it.
either you name it and talk to it,
or you send a list of values, and respond to position in the list.
depends on timing of controls.

R_Gol's icon

Thanks. Do you have any book or reading recommendation on that topic? or even recommendation reading for arduino programming?

Source Audio's icon

I have no idea, it is long ago i started with arduino.
But I am sure you will find a lot of tutorials on the net.

Roman Thilenius's icon


this "if else" mess is somehow funny, because it is one of the few shwocases where maxmsp is simply "better".

if it would be expr, in most other languages you could write it all in one line (while in max you can not).

but to convert 1, 2, 3, 4, 5, 6 to 6 different symbols, in max we would just use [coll] and you are done, (or, if you think that is an unfair comparison, select 1 2 3 4 5 6 and 6 triggers with the symbols). both of which is even better than working with if on array elements, like many languages offer.

miller john's icon

hey, Generally - nothing wrong, is just that I'm thinking what if I would like to send 20 signals from max msp to arduino to control 20 different objects with arduino (LEDs, Relay.etc) it will soon become messy. This is why I'm asking about more elegant way of doing so.

Source Audio's icon

There is no "general" better way.
You would understand that if you want to control something that needs
all parameters to be received at same time, one woud send a string or better say list.
If you control 20 items, but each one reacts to different input in different intervals,
it would be better to address each individually.

But main thing you need to understand is that max sends 8bit numbers
0-255 which in fact represent ascii code.
same the other way arround.
That's why we need atoi to send and itoa to receive serial data
in Max.

for example rgb led 1, I would send id A followed by 3 values.
In arduino if serial read == 'A' then parse 3 ints and control the led RGB.
for led 2 use B and so on.

https://docs.cycling74.com/max8/tutorials/communicationschapter02

Roman Thilenius's icon


(of course it would be "better" to just map one array to another, but i have no idea if you can do that in arduino ide/processing. you can probably do something similar using defined variables, but in that case i highly doubt it will run more effective than if-then-else-... chains.)

R_Gol's icon

But main thing you need to understand is that max sends 8bit numbers
0-255 which in fact represent ascii code.
same the other way arround.
That's why we need atoi to send and itoa to receive serial data
in Max.

so If for some reason I would like to send a number that is larger - 444 for that example - Max send it as a 16bit (2byte) number ?

Source Audio's icon

no, you send 444 to atoi
and atoi outputs 3 x ascii code for digit 4
please at least read stuff at this link

https://docs.cycling74.com/max8/tutorials/communicationschapter02

Source Audio's icon

and @Roman - sure can arduino map incoming array to different destinations.
same as unpack in max.