Go Down

Topic: "Invalid Conversion" or "Cannot Convert" messages (Read 994 times) previous topic - next topic

jeffmorris

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.

Joystick Code
Code: [Select]

#include <SoftwareSerial.h>
String readString;
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
int x_position;
int y_position;
const int xb_rx = 2;
const int xb_tx = 3;
SoftwareSerial Xbee(xb_rx,xb_tx);
void setup()
{
  Serial.begin(9600);
  Xbee.begin(9600);
}
void loop ()
{
  x_position = analogRead(PIN_ANALOG_X);
  y_position = analogRead(PIN_ANALOG_Y);
  map(x_position,0,1024,1000,2000);
  map(y_position,0,1024,1000,2000);
  Serial.print(x_position);
  Serial.print(",");
  Serial.print(y_position);
  Serial.print(",");
  Xbee.print(x_position);
  Xbee.print(",");
  Xbee.print(y_position);
  Xbee.print(",");
}


Servo Code
Code: [Select]

#include <SoftwareSerial.h>
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
const int xb_rx = 2;
const int xb_tx = 3;
SoftwareSerial Xbee(xb_rx,xb_tx);
int x_position;
int y_position;
String x_value;
String y_value;
char val;
String test;
String splitString(String s, char parser,int index);
void setup()
{
  servoLeft.attach(5);       // Left servo to pin 10
  servoRight.attach(6);       // Right servo to pin 9
  Serial.begin(9600);
  Xbee.begin(9600);
  delay(200);
  servoLeft.write(90);
  servoRight.write(90);
}
void loop()
{
  if(Xbee.available())
  {
    val = Xbee.read();
    String x_value = splitString(val,',',0);
    String y_value = splitString(val,',',1);
    x_position = atoi(x_value);
    y_position = atoi(y_value);
    Serial.println(x_position);
    Serial.println(y_position);
  } 
}
String splitString(String s, char parser,int index)
{
  String rs='\0';
  int parserIndex = index;
  int parserCnt=0;
  int rFromIndex=0, rToIndex=-1;
  while(index>=parserCnt)
  {   
    rFromIndex = rToIndex+1;
    rToIndex = s.indexOf(parser,rFromIndex);
    if(index == parserCnt)
    {
      if(rToIndex == 0 || rToIndex == -1)
      {
        return '\0';     
      }
      return s.substring(rFromIndex,rToIndex);
    }
    else
    {
      parserCnt++;
    }
  }
  return rs;
}



Osgeld

can you post the exact error, I hate guessing
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

jeffmorris

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*)'

PaulS

Code: [Select]
    val = Xbee.read();
    String x_value = splitString(val,',',0);

You read ONE character from the radio. Then, you want to split that character at the comma. How it that possibly going to work?

jeffmorris

Here's the SplitString code:

https://github.com/despild/SplitStringExample/blob/master/SplitStringExample.ino

How can I fix the problems?

PaulS

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.

jeffmorris

How can I append each character to a String instance? Is there a better way?

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat

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.

Code: [Select]

//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
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy