control angle of servo motor

hye..I have two Xbees and two Arduinos coded to control angle of servo motor. How could I code both of them . I think we need a sender and receiver code. On the receiver ,I'm already have the receiver code, but I have no idea for sender code. I'm a total beginner. Thanks!

Here is my receiver code.

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup()
{
Serial.begin(9600);
myservo.attach(9);
}

void loop()
{

while (Serial.available() > 0) {

pos = Serial.parseInt();

}
myservo.write(pos);
delay (100);

}

 delay (100);

In 1/10th of a second, your input serial buffer could easily overflow, and you'd never know.

I want to create a wireless servo controller to control angle of servo motor . Now the problem is I don't know how to write the code:(

The above code that I posted is "receiver code".but could you please tell me how to write the (transmitter/sender) code for control the angle of servo?

actually I have just tried by myself to write the transmitter/sender code,but it doesn't work(something wrong with the code)...any help I will appreciate it.

int pos = 0;
void setup()
{
Serial.begin(9600);
}

void loop()
{
while (Serial.available() > 0) {
pos = Serial.parseInt();
}
Serial.print(pos);
delay(10);
}

At the moment your problem seems to be the communication, so I suggest you ignore the servo for now and just concentrate on getting communication working between two XBees. I suggest that Google is your friend here.

I have just tried by myself to write the transmitter/sender code,but it doesn't work(

...so you
posted the receiver code, again.

void loop()
{
  while (Serial.available() > 0) {
    pos = Serial.parseInt();
   }
  Serial.print(pos);
  delay(10);
}

Don't you want to write the position to the servo, rather than the serial port? The sender knows what it sent. That dumb delay() is still there.

I configured my XBees using X-CTU and they've been working fine.Now i just don't know how to write the code.

here i'm using this simple code for receiver code.How to write the sender code? :frowning:

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup()
{
Serial.begin(9600);
myservo.attach(9);
}

void loop()
{

while (Serial.available() > 0) {

pos = Serial.parseInt();

}
myservo.write(pos);
delay (10);

}

You probably could use the below test code for both sending and receiving. For testing, skip the xbees, and connect the sending arduino tx pin to the receiving arduino rx pin (and connect both arduino grounds together). Send a servo position command to the sending arduio via the serial monitor, and it will echo the command to the receiving arduino via the tx/rx connection.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    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);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

sorry can i know what do you mean by " connect the sending arduino tx pin to the receiving arduino rx pin"?

queenka:
sorry can i know what do you mean by " connect the sending arduino tx pin to the receiving arduino rx pin"?

Which part don't you understand? (I'm not being facetious - presumably you do understand some of the terms.)

here..alike code as above.. but mine is used to debug my servo.. i used PWMServo lib bcoz Servo.h seems like having conflict with SoftwareSerial.h.. i used softwareserial for my GPS module before this

/*
  Servo debugger by Heiswayi Nrird
*/
#include <PWMServo.h> // library Servo.h conflicts with SoftwareSerial.h
PWMServo servo; // SERVO_PIN_A = pin 9 | SERVO_PIN_B = pin 10

char junk = ' ';
int angle;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Enter value of angle (0 - 180) to drive the servo");
  Serial.println();
  Serial.flush();
  servo.attach(SERVO_PIN_A); // pin 9
  servo.write(90);
}

void loop()
{
  while (Serial.available() == 0) ;  // Wait here until input buffer has a character
  {
    angle = Serial.parseInt();        // new command in 1.0 forward
    if (angle < 0 || angle > 180) { Serial.println("From 0 to 180 only!"); }
    else {
      Serial.print("Angle = "); Serial.println(angle); showDir(angle);
      servo.write(angle);

      while (Serial.available() > 0) { junk = Serial.read() ; }      // clear the keyboard buffer
    }
  }
}

void showDir(int a)
{
  if (a < 90) { Serial.print(" (Left)"); }
  else { Serial.print(" (Right)"); }
}

hye PeterH..im already test the coding as above and it work very well..after that, can i using the same testing for xbees??

queenka:
hye PeterH..im already test the coding as above and it work very well..after that, can i using the same testing for xbees??

Don't see why not. You might need to configure / initialise the Xbees, and you'd need to connect the Arduino's serial I/O to the XBee on each side instead of connecting wires between them, but fundamentally the XBee pair gives you a serial channel which is functionally equivalent to the wired connected between Rx/Tx pins.