Interpreting serial messages in Max

beatmelody's icon

I'm attempting to control Max via some code running on an Arduino UNO, which is capable of detecting 5 states, and upon a state change sends a corresponding integer from 0-4. I'm aware that the serial messages should be in ASCII, but I'm unable to figure out what to do with the numbers I'm getting from the serial object; 0 is interpreted as 58 248, 1 as 157 254, 2 also as 58 248, 3 as 17 254, and 4 as 59 248. Tutorials have suggested using the 13 10 carriage return message to format the messages as lists to be converted by itoa, but I'm not getting any 13 10 messages. Can anyone make sense of these outputs? Thanks.

Source Audio's icon

upload the arduino code.
that is easiest way to tell what you should do in max.
otherwise simply insert itoa and print object at serial output
to check what comes in.

In arduino using Serial.write or Serial.print sends only data,
Serial.println also newline
which then needs to get detected in max usind that sel 10 13 etc

beatmelody's icon

Thanks Source Audio. Here is the code.

void setup() {
pinMode(2,INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(7,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
Serial.begin(9600);
}

int buttonStatuses[] = {0, 0, 0, 0};
int state = 0;

void loop() {
int pinValues[] = {digitalRead(2), digitalRead(4), digitalRead(7), digitalRead(8)};
delay(10);
bool statusChange = false;
for (int i = 0; i < 4; i++) {
if (buttonStatuses[i] != pinValues[i]) {
buttonStatuses[i] = pinValues[i];
statusChange = true;
}
}

if (statusChange == true) {
state = 0;
for (int i = 0; i < 4; i++) {
if (buttonStatuses[i] == 0) {
state = i + 1;
}
}
Serial.println(state);
}
}

Source Audio's icon

You are sending 1 number at a time.
0 if no button pressed, 1 2 3 4 for the 4 pins when pressed.
you use Serial.println, are so send line feed (ascii 10)
after the number, so in max you need this :

P.S. in this case zl group is not really needed, because you send only 1 value.
But in case of several values between linefeeds, you would need
it to collect incoming numbers, and bang the list out, when linefeed gets received.

beatmelody's icon

Thanks! Will try it out.