Newbie here - Porting code between two Arduinos with XBees

Hi, I'm new to Arduino/XBee use, and need some programming help.

I wish to control a pan-tilt mechanism (two servos) remotely. Input comes from a joystick on the Coordinator Arduino, and output is movement of two servos on the remote Router Arduino. I have successfully gotten the two XBees to talk with each other; that took a while. Also, I have been able to get the pan-tilt mechanism to work on a single Arduino using a joystick shield attached. So, the pieces are there.

Here is the code I'm using for the single Arduino control:

// Controlling a pan-tilt platform position (two servos) using the Sparkfun joystick shield

#include <Servo.h>
Servo vertservo; // create a vertical servo object to control vertical movement
Servo horzservo; // create a horizontal servo object to control horizontal movement
int potpinvert = 0; // analog pin 0 connects to vertical potentiometer in the joystick
int potpinhorz = 1; // analog pin 1 connects to horizontal potentiometer in the joystick
int val0; // variable to read the value from analog pin 0
int val1; // variable to read the value from analog pin 1

void setup()
{
vertservo.attach(9); // attaches the vertical servo on pin 9 to the vertical servo object
horzservo.attach(10); // attaches the horizontal servo on pin 10 to the horizontal servo object
}

void loop()
{
val0 = analogRead(potpinvert); // reads value of vertical pot (value between 0 and 1023)
val1 = analogRead(potpinhorz); // reads value of horiz pot (value between 0 and 1023)
val0 = map(val0, 0, 1023, 0, 179); // scales vert pot value to the servo movement range
(Value between 0 and 180)
val1 = map(val1, 0, 1023, 0, 179); // scales the horz pot value to the servo movement range
(Value between 0 and 180)
vertservo.write(val0); // sets vertical servo position according to scaled value
horzservo.write(val1); // sets the horiz servo position according to the scaled value
delay(15); // waits for the servos to get to their commanded positions
}

My question is.....How do I get this program to work over the XBee network? From reading around in the Forum I'm suspecting that somehow I have to use a serial send command / serial read. I just don't know what I'm doing.

Any suggestions out there? Your help is GREATLY APPRECIATED! Tim

Your servos are controlled with the numbers in the variables val0 and val1.

You need to send those values to the other Arduino.

I am not familar with XBees but I suspect the 3rd example in serial input basics would be appropriate for receiving the data.

If was using Serial to send the data it would be like this

Serial.print('<');
Serial.print(val0);
Serial.print(',');
Serial.print(val1);
Serial.print('>');

...R

Thanks for your response. I'll give that a try. Makes sense too! Tim

Sorry. I had omitted the link in Reply #1 - I have now added it.

...R

Some servo pot test code made for a low bandwidth setup. You should be able to test it on the arduino with directlu connected servos. If that works, then connect the xbees use it on the tx arduino and use the bottom code on the rx arduino.

tx code

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing, adjust as needed
}

rx 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, myservoe;  // 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 
  myservod.attach(10);  //the pin for the servoe 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);
          if(readString.indexOf('e') >0) myservoe.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);
          if(readString.indexOf('e') >0) myservoe.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Thank-you zoomkat. I'll give it a try. Tim