read string trouble

sending data from one xbee to another movement data works fine but when i try sending the servo data it serial prints on both sides but just doesnt move the servos on the rx side
tx

#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 12); // RX, TX
int data = 0;
int potpin = A0;  // analog pin used to connect the potentiometer
int newval, oldval;   // variable to read the value from the analog pin
int potpinb = A1;
int newval1, oldval1;
const int SW1 = 8; //forward
const int SW2 = 9; //backward
const int SW3 = 3; //left
const int SW4 = 2; //right
void setup()
{
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  //pinMode(Light, INPUT);
  Serial.begin(9600);
mySerial.begin(9600);
}
void loop(void)
{
  
   newval = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  newval = map(newval, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  if (newval < (oldval-2) || newval > (oldval+2))
  { 
    mySerial.write(newval); //print the new value for testing 
    mySerial.write("a,");
    Serial.print(newval);
    Serial.print("a,");
    oldval=newval; //set the current old value
  } 
  newval1 = analogRead(potpinb);            // reads the value of the potentiometer (value between 0 and 1023) 
  newval1 = map(newval1, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2))
  { 
    mySerial.write(newval1); //print the new value for testing 
    mySerial.write("b,");
    Serial.print(newval1);
    Serial.print("b,");
    oldval1=newval1; //set the current old value
  } 
  int data = 0;
  if (digitalRead(SW1) == HIGH)
  {
    data += 1;
  }
  if (digitalRead(SW2) == HIGH)
  {
    data += 2;
  }
  if (digitalRead(SW3) == HIGH)
  {
    data += 4;
  }
  if (digitalRead(SW4) == HIGH)
  {
    data += 8;
  }
  if(data > 0)
  {
    delay(50);
  mySerial.write(data);
 Serial.print(data);
  }

}

rx

String readString;
#include <MotorDriver.h>
#include <Servo.h>
Servo myservo;
Servo myservo2;
int cammove = 0;
int cammove2 = 0;
int data = 0;
void setup()
{
  motordriver.init();
  motordriver.setSpeed(100, MOTORB);
  motordriver.setSpeed(250, MOTORA); // steering motor
myservo.attach(A0);
myservo2.attach(A1);
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available())
 data = Serial.read();
 //movement control
 if(data < 11)
 {
  if (data == 1)
  {
    motordriver.goForward();
  }
  else if (data == 2)
  {
    motordriver.goBackward();
  }
  else if (data == 4)
  {
    motordriver.goLeft();
  }
  else if (data == 8)
  {
    motordriver.goRight();
  }
  else if (data == 5)
  {
    motordriver.goForward();
    motordriver.goLeft();
  }
  else if (data == 9)
  {
    motordriver.goForward();
    motordriver.goRight();
  }
  else if (data == 6)
  {
    motordriver.goBackward();
    motordriver.goLeft();
  }
  else if (data == 10)
  {
    motordriver.goBackward();
    motordriver.goRight();
  }
  else
  {
    motordriver.stop();
  }
 }
  // cam movement
  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 > 0)
        {
          Serial.print("writing: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservo.write(n);
          if(readString.indexOf('b') >0) myservo2.write(n);
         }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  delay(50);
    Serial.print("data=");
    Serial.println(data, DEC);
  
  data -= data;
}
  int data = 0;
  if (digitalRead(SW1) == HIGH)
  {
    data += 1;
  }

Why do you have a global and a local variable of the same name?

What does the Serial output look like? How is the servo powered?

both powered by the mega 5v

rx serial

[code] data=0 data=53 data=44 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿa2 data=97 data=47 data=44 data=97 data=41 data=44 aa& data=97 data=35 data=44 a data=97 data=29 data=44 a data=97 data=23 data=44 a data=97 data=17 data=44 a data=97 data=11 data=44 a data=97 data=5 data=44 a data=97 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=5 data=44 ÿÿÿÿÿÿÿa data=97 data=12 data=44 a data=97 data=18 data=44 a data=97 data=24 data=44 ae data=97 data=30 data=44 a! data=97 data=36 data=44 a' data=97 data=42 data=44 a- data=97 data=48 data=44 a3 data=97 data=54 data=44 a9 data=97 data=60 data=44 a? data=97 data=66 data=44 aE data=97 data=72 data=44 aK data=97 data=78 data=44 aQ data=97 data=84 data=44 aW data=97 data=90 data=44 a] data=97 data=96 data=44 ac data=97 data=102 data=44 ai data=97 data=108 data=44 ao data=97 data=108 data=44 ai data=97 data=102 data=44 ac data=97 data=96 data=44 a] data=97 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=0 data=93 data=44 ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿb` data=98 data=99 data=44 bf data=98 data=105 data=44 bl data=98 data=111 data=44 br data=98 data=117 data=44 bx data=98 data=123 data=44 b~ data=98 data=129 data=44 b„ data=98 data=135 data=44 bŠ data=98 data=141 data=44 b� data=98 data=147 data=44 b– data=98 data=153 data=44 bœ data=98 data=159 data=44 b¢ data=98 data=165 data=44 b¨ data=98 data=171 data=44 b® data=98 data=171 data=44 b® data=98 data=171 data=44 b® data=98 data=171 data=44 b® data=98 data=171 data=44 b® data=98 data=177 data=44 b´ data=98 [/code]

tx serial

110a,107a,104a,101a,104a,107a,110a,113a,78b,81b,84b,87b,90b,93b,90b,87b,84b,81b,78b,

i also got rid of the extra data variable thanks

both powered by the mega 5v

I KNOW for a fact that you have been told that that is the WRONG answer. DO NOT START ANOTHER DAMNED THREAD ABOUT YOUR SERVO ISSUES UNTIL YOU PROVIDE A REAL POWER SOURCE FOR THE SERVOS.

They both work powered by the mega with the sweep demo

merkzilla: They both work powered by the mega with the sweep demo

Just give the servos their own power supply - make life simple.

...R

So, you check is there is a serial character available, and then read one, and do something with the motor, and then you read another one.

This won't work, if there is only one character available.

servos powered by external 6 v power with same results

the rx side is receiving data but its not writing it to the servos

Your receiving code is a mess. You have no check for how many characters are received. You seem to be expecting values below 11 - which are all non-printing characters and later you expect to receive characters. You have no means to know whether a character is one of the first sort, or one of second sort - you are just relying on wishful thinking.

You need to establish a simple protocol for sending and receiving your data. It will probably be easiest if you only use printable characters - it makes the debugging much easier.

My suggestion is to send the data in this format <data, string> where data is a letter of the alphabet that takes the place of the numbers 0 to 10 in your code and string is whatever text you want to send. The < and > are start- and end-markers so the receiver knows when the data starts and ends. For example you could send <b, test> or <b, 123>

The third example in serial input basics is designed to receive that sort of data. The parse example can be modified to put the data into appropriate variables once it has been received.

…R

well for movement of the car im adding 1,2,4,8 1and 2 4 and 8 cannot happen at the same time so the max is10 or backward right but i dont it confused with the servo angels so im seding those as strings…but looks like all data should be sent this way?

Basic rx code that works with the serial monitor for the tx servo command string you posted (below). You might try this code using the serial monitor to sort out your servo issues.

tx command string, copy and ctrl-v to paste in serial monitor.

110a,107a,104a,101a,104a,107a,110a,113a,78b,81b,84b,87b,90b,93b,90b,87b,84b,81b,78b,

rx servo 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 
// Powering a servo from the arduino usually *DOES NOT WORK*.

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 combined like 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
    }
  }
}

merkzilla:
well for movement of the car im adding 1,2,4,8 1and 2 4 and 8 cannot happen at the same time so the max is10 or backward right but i dont it confused with the servo angels so im seding those as strings…but looks like all data should be sent this way?

I would send all the data as chars - it is not the most efficient, but it is simple to debug and I don’t get the impression that a few extra microseconds of transmission time will matter.

You can send ‘1’ ‘2’ ‘4’ and ‘8’ and easily convert them to byte values after they are received. I had not realized you were adding them.

…R

removed all but my code for servo sending and recieing data still but only writes the data within the 50s from the pot data on the tx side so the servos only move ever so slightly
tx

#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(11, 12); // RX, TX
char data = 0;
int potpin = A0;  // analog pin used to connect the potentiometer
int newval, oldval;   // variable to read the value from the analog pin
int potpinb = A1;
int newval1, oldval1;
const int SW1 = 8; //forward
const int SW2 = 9; //backward
const int SW3 = 3; //left
const int SW4 = 2; //right
void setup()
{
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);
  //pinMode(Light, INPUT);
  Serial.begin(9600);
mySerial.begin(9600);
}
void loop(void)
{
  
   newval = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  newval = map(newval, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  if (newval < (oldval-2) || newval > (oldval+2))
  { 
    mySerial.write(newval); //print the new value for testing 
    mySerial.write("a,");
    Serial.print(newval);
    Serial.print("a,");
    oldval=newval; //set the current old value
  } 
  newval1 = analogRead(potpinb);            // reads the value of the potentiometer (value between 0 and 1023) 
  newval1 = map(newval1, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2))
  { 
    mySerial.write(newval1); //print the new value for testing 
    mySerial.write("b,");
    Serial.print(newval1);
    Serial.print("b,");
    oldval1=newval1; //set the current old value
  } 
}

rx

String readString;
#include <MotorDriver.h>
#include <Servo.h>
Servo myservoa;
Servo myservob;
int data = 0;
void setup()
{
  motordriver.init();
  motordriver.setSpeed(100, MOTORB);
  motordriver.setSpeed(250, MOTORA); // steering motor
myservoa.attach(A0);
myservob.attach(A1);
  Serial.begin(9600);
}
void loop()
{

  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);
          }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
      }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

merkzilla: removed all but my code for servo sending and recieing data still but only writes the data

I have suggested a solution which I know works well. I am not going to spend time trying to figure out another way to do the same thing.

Naturally I have no wish to prevent you from doing so.

...R

from what im understanding i would declaire each function as a char

char forward = 'f'; char backward = 'b'; char left = 'l'; char right = 'r'; char forwardleft = "fl"; char forwardright = "fr"; char backwardleft = "bl"; char backwardright = "br";

but if they convert to numbers wouldnt that still confuse servo angle with move or do they not have to convert to numbers?

Send either chars, or single byte numbers. One or the other. Trying to send both is silly.

For your requirements, I'd suggest numbers.

'F' would be single byte and "f" would be chars but from what I seen if it's more than one letter FL it would need to be in "" is that right?

or would it be better to send

tx Side…

char data = 0;

if (digitalRead(SW1) == HIGH)
{
data += 1;
}

String data = “forwardData: 2”;
Serial.println(data);

rx side…

if (Serial.available()) {
char data = Serial.read();
char mostSignificantDigit = data.charAt(10);

if (data == 2)
{
motordrive.forward
}

Serial.println(forwardString);

merkzilla:
but if they convert to numbers wouldnt that still confuse servo angle with move or do they not have to convert to numbers?

How could there possibly be confusion ?
I have no idea what is in your mind

Have you tried the examples in serial input basics ?
Have you experimented with the examples to get them to work with the format <b,123> that I originally suggested or my later suggestion to use <2,123>

You CANNOT have a char with two characters such as “fr”. A char means it is a single character and that is why you use single quotes such as ‘a’

…R