sending mouse position via serial to arduino?

hey everyone,

I’m now trying to learn about the difference between OF and processing, one of the questions I have is how to use OF to send mouse position to arduino?

in processing we can simply do:
port.write(mouseX+","+mouseY+"\n");

and split the strings in arduino,

Can I do the similar thing in OF too?

Thank you !

yes, but you need to use ofIntToString()

so you will do:

  
port.write(ofToString(mouseX)+","+ofToString(mouseY)+"\n");  
  

or… you can use the string . append fucntions etc…

string in c++ is more powerfull than string in processing!

hey k, thanks for the reply,

however the syntax of writeByte is writeByte(unsigned char * buffer, int length),

how can I do that?

thank you

ok, here is what I have tried so far

  
    valX = ofGetMouseX();  
    valY = ofGetMouseY();  
    string newX = ofToString(valX);  
    string newY = ofToString(valY);  
    string dataPack;  
    dataPack.append(newX);  
    dataPack.append(",");  
    dataPack.append(newY);  
    dataPack.append("\n");  
    printf(&dataPack[0]);  
	serial.writeBytes(&dataPack[0], 10);  

however I got an error “cannot initialize a parameter of type"unsigned char *” with an rvalue of type “char*” "

any idea?

thankyou

do

  
const char *buffer = dataPack.c_str();  
  

and send the char pointer buffer.

in c++ we have many types of strings…

string is basically an array of characters (char)

a pointer in this case is like saying an array… a chunk of memory.

unsigned means that it can hold more data because it utilizes positive numbers thus giving you greater range

I our case you can also do

  
char dataPack[1024];  
  

instead of string.

the reason why you cannot use string is merely because the object you are using was designed to use unsigned char…

it’s actually very easy to overwrite the writebytes function and make it use strings…