I'm trying to make a tricopter fly with a WiiMote, and to do that, i need to send data that i recieve in Processing to arduino with the serial connection. I read a lot of toppics about this, but there is one problem: the 4 numbers are between 1000 and 2000, and i coudn't find anything on that.
I'm using Glovepie to send data form the Wiimote to Processing with open sound controll, also known as OSC. Then Processing should send this data to the Arduino who is converting this data to PPM singals, used to controll the tricopter.
I would love to hear some idea's! I'm not a genius but i'm willing to learn
If you have Processing send delimiters, too, like "<1500>", you can use this code to receive the data:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Where is says "Process the packet", the "<1500>" that Processing sent will result in "1500" in inData. You can call atoi() to convert that to an int.
If Processing is sending something like "<1500, 2000, 1420, 1200>", inData will contain "1500, 2000, 1420, 1200", and you can use strtok() to get the tokens and atoi() to convert each token.
I tried to get the code working but i don't know how to use the strtok function and atoi =(
Can you please add another piece of code with an example on how to substract the 4 integers serperately?