I found code that splits strings into two or more parts on github website (down for maintenance). I tried to create code that sends values from one Arduino UNO board that has Parallax Joystick to other Arduino UNO board that has servos via Xbee communication but I get compile error messages that say "Invalid Conversion" or "Cannot Convert" in the Servo Code. How can I fix the problems? The Joystick Code works.
xbee_Receive.ino: In function 'void loop()':
xbee_Receive:30: error: invalid conversion from 'char' to 'const char*'
xbee_Receive:30: error: initializing argument 1 of 'String::String(const char*)'
xbee_Receive:31: error: invalid conversion from 'char' to 'const char*'
xbee_Receive:31: error: initializing argument 1 of 'String::String(const char*)'
xbee_Receive:32: error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
xbee_Receive:33: error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
That example calls the splitString() function with a String, not a char.
You must create a String (ugh!) to call the function with. That means repeatedly calling client.read(), until the client says "no more!", appending each character to a String instance.
Of course, you will eventually have problems with Strings, so don't way you weren't warned not to use them.
You would be better off IMO to accumulate the incoming string in a char array until you knew it was complete (up to you to figure out when that is) and then use strtok() or similar to break it up into tokens.
Below is some untested code that I made for a joystick/servo setup where the servo command would be received via serial port (or later an internet command). The idea is that the servo command value is followed by a letter designator to identify the particular servo, and the , is a delimiter to mark the end of the servo command. You should be able to test using the serial monitor.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}