controlling servo motor+2brushless using xbee-arduino

i am a new arduino user and got some questions in programing i am using the arduino xbee with shield in rx and serial-usb connected xbee to pc how to send a word from the xbee in the serial-usb side to recive on aruino kit(using arduino serial monitor as simulator) thank you very much in advance

i am using the arduino xbee with shield

Which XBee(s)? How is it (are they) configured?

how to send a word from the xbee in the serial-usb side

Exactly the same as if there were wires instead of radios connecting the two devices.

ok done with connecting them together and now they can read chars i type now i want to control the inputs to a servo motor to go 3 angles initial,right and left…will add 2 brush-less motors later
here is the code:

#include <Servo.h>
Servo myservo;
int pos = 0;
int angle = 135;
void setup()
{
myservo.attach(9);  //control pin
  Serial.begin(9600);  //baud rate
}

void loop()
{
char x;
x=Serial.read();
//Serial.print(x);
if(x=='a')
{
//move left
{

if(pos <= 30)
{
myservo.write(75);
delay(15);

}
else
{ 
int pos = 31;
}

}
//Serial.print('L');
}

if(x=='w')
{
//move foreward
Serial.print('F'); 
}
if(x=='d')
{


if(pos <= 30)
{
myservo.write(165);
delay(15);

}
else
{ 
int pos = 31;

}
//move right
//Serial.print('R'); 
} 
return;


delay(100);
}
return;

Why?

delay(100);

This code, after the return, will never be executed. Why did you put it here?

You have way too many { and }. You really should learn when they are needed, when they are optional, and when they are silly, and remove the silly ones.

You could do a lot better with formatting that code. There is even a tool to do it for you - Auto format, on the Tools menu. Learn to use it.

Do you have a problem?

i used return so that after doing the loop the code start over again from the start and the delay was using to try something but didnt remove it,it been long since last time i used c++ so i dont remember it quit well
a/w ur point is the code will work w/o return ?
after some edites i tried this code (still trying to add the brushless),the servo for the left and right directions works good now with return included:

#include <Servo.h>
Servo myservo;
Servo mybrushless1;

int pos = 0;
int angle = 90;
void setup()
{
  myservo.attach(9);
  mybrushless1.attach(6);

  Serial.begin(9600);
}

void loop()
{
  char x;
  x=Serial.read();
  //Serial.print(x);
  if(x=='a')
  {
    //move left
    {

      if(pos <= 30)
      {
        myservo.write(45);
        delay(1000);

      }
      else
      { 
        int pos = 31;
      }

    }
    //Serial.print('L');
  }

  if(x=='w')
  {
    //move foreward
    myservo.write(90);

    if(pos <= 30)
    {

      for(pos = 0; pos < 200; pos += 5)
      {
        myservo.write(pos);

        delay(1000);
      }
    }
    Serial.print('F'); 
  }
  if(x=='d')
  {


    if(pos <= 30)
    {
      myservo.write(135);
      delay(1000);

    }
    else
    { 
      int pos = 31;

    }
    //move right
    //Serial.print('R'); 
  } 
  if(x=='s')
  {
    int pos = 0;

    myservo.write(90);
    delay(1000);
  }

  return;


}

Note:with the code i wante ‘w’ to make the angle at 90 and the brushless works starting from 0 adding +5 till it reach 200
‘a’ and ‘d’ turn the servo left and right,and ‘s’ stop the brushless motor
(srry for my bad english,its not my native language)

a/w ur point is the code will work w/o return ?

I suggest you read up on why loop() is called loop(). http://arduino.cc/en/Reference/Loop

If you don’t code a return, it is implied at the end of a function.

i used return so that after doing the loop the code start over again from the start

That will happen automatically when the end of the function is reached. The explicit return is unnecessary.

  if(x=='a')
  {
    //move left
    { // <--- Useless

      if(pos <= 30)
      {
        myservo.write(45);
        delay(1000);

      }
      else
      {
        int pos = 31;
      }

    } // <--- Useless
    //Serial.print('L');
  }

Useless braces still present.

Declaring a new variable in the else block is useless, too. The variable goes out of scope (ceases to exist) immediately.

  if(x=='s')
  {
    int pos = 0;

    myservo.write(90);
    delay(1000);
  }

What’s this pos for? It is not used in the block it is declared in, and will go out of scope (cease to exist) at the end of the block.

I still don’t understand what your problem is/what you need help with.

with the code i wante ‘w’ to make the angle at 90 and the brushless works starting from 0 adding +5 till it reach 200
‘a’ and ‘d’ turn the servo left and right,and ‘s’ stop the brushless motor
int pos = 0; to make the brushless motor stop spinning ,wrong command?

#include <Servo.h>
Servo myservo;
Servo mybrushless1;
int pos = 0;
int angle = 90;
void setup()
{
  myservo.attach(9);
  mybrushless1.attach(6);

  Serial.begin(9600);
}

void loop()
{
  char x;
  x=Serial.read();
  if(x=='a')
    if(pos <= 30)
    {
      myservo.write(45);
      delay(1000);
    }
  //else
  //{ 
  //int pos = 31;
  //}
  if(x=='w')
    myservo.write(90);

  if(pos <= 30)
  {
    for(pos = 0; pos < 200; pos += 5)
    {
      mybrushless1.write(pos);
      delay(1000);
    }
  }
  if(x=='d')
    if(pos <= 30)
    {
      myservo.write(135);
      delay(1000);
    }
  //else
  //{ 
  //int pos = 31;
  //}
  if(x=='s')
  {
    int pos = 0;
    myservo.write(90);
    delay(1000);
  }
}

note:there is a problem with ‘w’,when i click w and try to use ‘d’ and ‘a’ again nothing happens like if it is in an endless loop

int pos = 0; to make the brushless motor stop spinning ,wrong command?

No, and yes.

pos = 0;

would make the motor stop. Defining another variable, of the same name, that goes out of scope immediately, would not.

  if(x=='w')
    myservo.write(90);

  if(pos <= 30)
  {
    for(pos = 0; pos < 200; pos += 5)
    {
      mybrushless1.write(pos);
      delay(1000);
    }
  }

If you enter a w, the code is going to send the servo to position 90. Then, it is going to set the position of the brushless motor to 0, 5, 10, …, 190, and 195. That’s 40 steps, with a 1 second pause between steps. Are you waiting 40+ seconds to see that the brushless motor gets to its final position? Only when it gets to its final position will the system read a new serial value.

By the way, you really should be using Serial.available() to determine how much data there is to read, and only reading when there is data to be read.

first:ty vm for ur time helping me :) second:where i should use the Serial.available() command? third:i gotta make a delay for the motor arming ,u suggest i changed the 5+ to someting else?

second:where i should use the Serial.available() command?

void loop()
{
   if(Serial.available() > 0)
   {
      // Read and use the serial data
   }
}

third:i gotta make a delay for the motor arming ,u suggest i changed the 5+ to someting else?

No. I suggest that taking 40 seconds to arm the motor is way too long.

i want 1sec only arming not 40 secs
any suggestion how to edit it for the 1 sec arming?
like this>:

#include <Servo.h>
Servo myservo;
Servo mybrushless1;
int pos = 0;
int angle = 90;
void setup()
{
  myservo.attach(9);
  mybrushless1.attach(6);

  Serial.begin(9600);
}

void loop()
{
  char x;
  if(Serial.available() > 0)
  {

    x=Serial.read();
    if(x=='a')
      if(pos <= 30)
      {
        myservo.write(45);
        delay(1000);
      }
    //else
    //{ 
    //int pos = 31;
    //}
    if(x=='w')
      //myservo.write(90);

      if(pos <= 30)
      {
        for(pos = 0; pos < 200; pos += 5)
        {
          mybrushless1.write(pos);
          delay(1000);
        }
      }
    if(x=='d')
      if(pos <= 30)
      {
        myservo.write(135);
        delay(1000);
      }
    //else
    //{ 
    //int pos = 31;
    //}
    if(x=='s')
    {
      pos = 0;
      myservo.write(90);
      delay(1000);
    }
  }
}

any suggestion how to edit it for the 1 sec arming?

Cut the delay time by a factor of 40. 1000/40 = 25 if my arithmetic is correct.

edited the delay to 25,did i use the Serial.available() right ?

#include <Servo.h>
Servo myservo;
Servo mybrushless1;
int pos = 0;
int angle = 90;
void setup()
{
  myservo.attach(9);
  mybrushless1.attach(6);

  Serial.begin(9600);
}

void loop()
{
  char x;
  if(Serial.available() > 0)
  {

    x=Serial.read();
    if(x=='a')
      if(pos <= 30)
      {
        myservo.write(45);
        delay(1000);
      }
    //else
    //{ 
    //int pos = 31;
    //}
    if(x=='w')
      //myservo.write(90);

      if(pos <= 30)
      {
        for(pos = 0; pos < 200; pos += 5)
        {
          mybrushless1.write(pos);
          delay(25);
        }
      }
    if(x=='d')
      if(pos <= 30)
      {
        myservo.write(135);
        delay(1000);
      }
    //else
    //{ 
    //int pos = 31;
    //}
    if(x=='s')
    {
      pos = 0;
      myservo.write(90);
      delay(1000);
    }
  }
}