i have a unity -> arduino setup. my unity code :
int left = System.Convert.ToInt32(Nite3.offsetleftarm);
int right = System.Convert.ToInt32(Nite3.offsetrightarm);
_SerialPort.Write(left+"-"+right);
on the sketch-side
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10);
//Serial.begin(9600); // start serial port
Serial.begin(57600); // start serial port
}
// Function to read a string from the serialport and store it in an array
void readSerialString (char *strArray)
{
int i = 0;
if(Serial.available()){
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
serInStringLength=i;
}
int serReadInt()
{
int i, serAva; // i is a counter, serAva hold number of serial available
char inputBytes [7]; // Array hold input bytes
char * inputBytesPtr = &inputBytes[0]; // Pointer to the first element of the array
if (Serial.available()>0) // Check to see if there are any serial input
{
delay(5); // Delay for terminal to finish transmitted
// 5mS work great for 9600 baud (increase this number for slower baud)
serAva = Serial.available(); // Read number of input bytes
for (i=0; i<serAva; i++) // Load input bytes into array
inputBytes[i] = Serial.read();
inputBytes[i] = '\0'; // Put NULL character at the end
return atoi(inputBytesPtr); // Call atoi function and return result
}
else
return -1; // Return -1 if there is no input
}
// Function to find out wether the array is empty or not
boolean isStringEmpty(char *strArray)
{
if (strArray[0] == 0) return true;
else return false;
}
void loop () {
int a = serReadInt();
//a is now the complete string(int)..
myservo.write(a);
// how do i cut to b?
myservo2.write(b);
}
Because of me being total new... i really don't have a an idea how to split the char i got from unity.. the above setup without the b was with one value! worked well!! the setup in sketch also works with int and it should work with char/string