From processing to arduino

HazardsMind:

sprintf(int, int)

There is a little more needed than that.

As I understand the original question, pedroply is using Processing. Processing does not support sprintf().

It really shouldn't be necessary to create an entire string on the sending side anyway. Just send a delimiter in between the individual values.

And how do I split the values into ints on the arduino?

First you have to think about what you are sending.

If you are sending ASCII strings, then you'll need to use functions like strtok() and atoi(). Hazard's example link above has receiving code similar to what you would need.

If you are sending "byte" values of what the integers in processing represent, then it is quite a bit easier since you don't have to convert from ASCII to decimal.

Well I am sending bytes.
And thanks i had forgotten to tell you.

It is in the receiving code from the link I gave you.

// the data to be split "x,y,z,s" => "111,222,333,444"
        x=atoi(strtok(c,",")); // strtok will look at the split point ",", and will return everything before it, so x = 111
        y=atoi(strtok(NULL,",")); // this will continue off from where the first line ended, and will look again for "," and now y = 222
        z=atoi(strtok(NULL,",")); // same here
        s=atoi(strtok(NULL," ")); // this is looking for a space " " again s = 444

//The atoi() function will convert an array of chars {'1','1','1'} into an actual int 111. Here I just combined both functions into one.

added: just read James C4S's post

I cant get it work:

#include <Servo.h> 
 
Servo myservo; 

 

int pos = 0;


 
void setup() 
{ 
  myservo.attach(9);
  Serial.begin(9600);
  
} 
 
void loop() 
{ 
           
     if (Serial.available() > 0) { 
      pos = atoi(strtok(NULL,",")); //how do i use this??
      myservo.write(pos);
  
  }
 

}

pedroply:
//how do i use this??

RTFM

Still the same I am don't know how to use it in my situation.

pedroply:
Still the same I am don't know how to use it in my situation.

Typically it takes more than 15 minutes to read and understand stuff like this; It's always a good idea to setup some examples and play around with them until you have a better understanding.

Regardless, you haven't said anything about how the string that is sent over the Serial line is formatted.

In processing it is like this:

arduinoPort.write("h");
  arduinoPort.write(",");
  arduinoPort.write(pos);

pedroply:
In processing it is like this:

arduinoPort.write("h");

arduinoPort.write(",");
  arduinoPort.write(pos);

Mixing ASCII and binary is rarely a good idea. Pick an encoding scheme and stick to it.

And how do I do that??
sorry for being very annoying.

pedroply:
And how do I do that??

How do you decide which encoding scheme to use? Coin flip?

I personally prefer ASCII over binary because it is easier to keep in sync.

How you want to send the data, is your choice.

I am sending int like arduinoPort.write(pos); which is an int, how do I combine that with the strtok() command?

pedroply:
I am sending int like arduinoPort.write(pos); which is an int, how do I combine that with the strtok() command?

You don't. You read in the two bytes individually, and pack them into an int. strok() only works if you decide to use an ASCII encoding scheme.

And what is that like?

pedroply:
And what is that like?

Like this

and

This

Thanks, rely enjoyed the second link but when I put in processing "z = (int)(word(pos, posc));" it says the function does not exists

pedroply:
Thanks, rely enjoyed the second link but when I put in "z = (int)(word(pos, posc));" it says the function does not exists

What function doesn't exist? If you're trying to compile some code, and you get an error, not posting the entire code and the entire error is less than unhelpful.