Send integer on Serial to Arduino.

danielaaroe's icon

hey. Need some help here.
Doing a project where were going to control a standard rc car using a iPhone 3g, 1 arduino (possably 2), and some RF modules.
we are going to read the accelerometer data on the iphone and turning them in to throttle/turn commands.

I've managed to get the accelerometer data in to my max-patch(8 bit) so now I have 2 8-bit integers in number boxes which i don't know how to send out on the serial port and to the arduino.

I need help to make the rest of the patch, so that i can send the two (continuously changing) 8-bit integers to the arduino.

Just imagine a blank patch with only two number boxes on it. the number in the boxes change continously(whole numbers, no decimals) and i want to send them to the arduino...
How do i do that ?
since this is real-time control, it needs do update at least 10 times a second.

Anyone have an idea of what to do ?
I think this is a pretty basic thing to do, but i have almost no experience with the max 5 program!

thanks for reading, and thanks again for all help.

Daniel Aaroe

seejayjames's icon

Try the [serial] object, and run the integers through [itoa] before they go in. or try [spell]. Some combination should get the Arduino to recognize them. You'll have to send them as two-element lists, each with an index number that the Arduino code listens for (and then sends to the right place), or send them as a pair, with a start code for the Arduino to see. It'll take a bit of fiddling but it should be fine. The key is the formatting for [serial].

danielaaroe's icon

Thanks, I've looked at it, but due to my lack of skills in the program, i can't really make sense of it. It looks like the [serial] makes the "integer" to "binary". But I dont really know anything for sure.

I'll just upload the program, and whoever wants to, can take a look. I have not tried it yet. Don't know if it does what it should. Left a few short comments there. And one long one.

pelang's icon

hi,

maybe this can halp you. the arduino code is not tested..

-----

char id; //max pak input 1( pak 0 1)
int data = 0; //max pak input data (pak 0 number)

void setup() {

Serial.begin(9600);

}

void loop()

{
while ((Serial.available() > 1)){

id = Serial.read();
data = Serial.read();

if (id == 2){

    if (data != 0){
     digitalWrite(12, HIGH);
    }
    else{
     digitalWrite(12, LOW);
    }

}
if (id == 3){

    if (data != 0){
     digitalWrite(13, HIGH);
    }
    else{
     digitalWrite(13, LOW);
    }
}

}

}

}

------

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


danielaaroe's icon

Thanks. I shall look at it more closely tonight. Did'nt quite figure out where to input the two integers. Even though it might look like it was at the [pak 2.0].

I see you made som arduino code as well. That was not necessary but I think it will help a lot when i start to write the arduino code. thanks again

pelang's icon

this is should be more clear:

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

danielaaroe's icon

yes it was. will try this. thanks man

danielaaroe's icon

Do i put both the data inputs in the same [pak].

The id number, is that there to seperate the two integers from each other ?(if so, i thought i should just mux/demux the ints to send them on the same serial without identifiers)...

sorry if the questions are stupid. I'm new at this.

I tried that, and this is how it looks like. Think this could work?
(have not used the id number box. have not muxed them either!)

thanks.

pelang's icon

hi

use for each data stream one [pak] object and give as first argument another id number.

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