Arduino > Max Serial read problems

tristanlouthrobins's icon

Hi folks, I've recently come back to Max after a lengthy break and I'm trying to collect data from a light sensor as a simple stream of values in Max.

I achieved this a couple of years ago, but currently I'm having issues with Max reading the Serial object. Everything is fine in the Arduino Serial Monitor, it's just not making it's way across to Max.

Any thoughts or suggestions would be hugely appreciated! :)

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

Here's the Max patch (with the itoa sub patcher pasted into the main patcher for reference)

And here's the Arduino code:

int lightLevel;

void setup()
{
Serial.begin(38400);
}

void loop()
{
lightLevel = analogRead(A0);
Serial.println(lightLevel, DEC);
delay(200);

}

brendan mccloskey's icon

Hi
assuming you have the latest version of the Arduino IDE, and have selected the correct serial-usb port (dev/tty . . ., or COM3 e.g.), then all you should need is this:

...
Serial.write(lightLevel);
...

If you're using an older board and IDE, then

...
Serial.print(lightLevel, BYTE);
...

does exactly the same. Then in Max just bang the serial object (serial a 38400; serial b 38400, or whatever) for the data.

HTH
Brendan

tristanlouthrobins's icon

Thanks for your reply. The IDE is up to date and the board is pretty new. Where exactly should the additional arduino code go - after Serial.println?

tristanlouthrobins's icon

Additionally, it's going through port /dev/tty.usbmodem1421 and in Max it's set up as serial a 38400 (I can't see the reasoning in defining it as 'b') Cheers, TLR

Medd's icon

Replace println with write and you don't need the 'convert' subpatcher on the other end:

int lightLevel;

void setup()
{
Serial.begin(38400);
}

void loop()
{
lightLevel = analogRead(A0);;
Serial.write(lightLevel);
delay(200);

}

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

Max patch:

tristanlouthrobins's icon

Thanks, Medd! Worked a treat! :D Thanks also, N00b, for the initial help :)

brendan mccloskey's icon

Thanks for filling in the blanks medd - I was in a hurry, hence my concision