Go Down

Topic: Wireless XBee Communication (Read 99 times) previous topic - next topic

collinjk11

I've been working on this project for a while now with little success.  I am trying to move a servo wirelessly with two XBee's.  This will be a steering mechanism for a vehicle, with a joystick from the controller moving the servo on the vehicle.  I have found a great example online to follow.  Attached is my code for the transmitter and the receiver.  I have linked the XBee's in several different programs, Putty, XCTU, and CoolTerm.  After linking the XBee's I tested them by plugging each into a seperate laptop, one running in XCTU and the other running in Arduino.  After taking them hundreds of yards apart I was still able to send things like "hello" in XCTU and receive them in Arduino serial and vice-versa.  I am also able to move the servo on a single Arduino with the joystick.  When trying to send the potentiometer values wirelessly the servo will not move.  I have available and have tried with both pairs of series 3 and series 2 pro XBee's.  Any help would be greatly appreciated

GaryP

I am not familiar with xbee's, does it really work that simple?

I put your code inside the tags, you do it next time by yourself.
 :)

Cheers,
Kari


Controller:
Code: [Select]

int joystick = 0;

void setup()
{
  Serial.begin(9600);              //Set baud rate to 9600.
}

void loop()
{
  int val = map(analogRead(joystick), 0, 1023, 0, 9);
  Serial.println(val);
  delay(50);

  Serial.print("X-axis: ");
  Serial.print(analogRead(joystick));
  Serial.print("\n");
  delay(200);
}


Receiver:
Code: [Select]

#include <Servo.h>

int servoPin = 7;                //Defines Pin
Servo Steering;                  //Create Servo Object

void setup()
{
  Serial.begin(9600);            //Set baud rate to 9600.

  Steering.attach(servoPin);     //Attaches the Servo to our Object
  delay(500);
}

void loop()
{
  while(Serial.available() == 0);       //Loop to wait for data from XBee

  int data = Serial.read() - '0';       //Converts incoming data bits into integers
 
  int pos = map(data, 0, 9, 0, 180);    //Maps postions 0-9 to servo degrees from 20-160
  pos = constrain(pos, 0, 180);         //Constrains the servo from 0-180 for safety

  Steering.write(pos);                  //Turns the Servo
  Serial.flush();                       //Flushes extra incoming characters not needed


  Serial.print("X-axis: ");
  Serial.print(analogRead(data));
  Serial.print("\n");
  delay(200);
}
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

GaryP

Does your receiver work as it should? What arduino board you have? Can you print out received messages and be sure that information is correct?

Show the circuit how your components are connected together, especially the power for the servo. Do you have separate power supply for the servo?

Cheers,
Kari
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

Power_Broker

The data is most likely being transferred by the XBees, but you are not parsing the serial data correctly - resulting in your code not working.

Look at this tutorial: https://forum.arduino.cc/index.php?topic=288234.0
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Go Up