Pages: [1]   Go Down
Author Topic: "Invalid Conversion" or "Cannot Convert" messages  (Read 673 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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;
}

Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can you post the exact error, I hate guessing
Logged


Offline Offline
Full Member
***
Karma: 2
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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?
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the SplitString code:

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

How can I fix the problems?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: