Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Xbee 1 with Arduino & Servo- Send and receive speed


#1

HI,
Scott here from Oregon.
I am controlling a servo on one xbee connected to an arduino and sending on another xbee connected to another arduino. It works fine yet it does not seem instantaneous. There appears to be a lag. If turn the pot quickly there is about a half second lag time before the servo moves. If I turn the pot slowly, then they seem to work together at the same time. Is there a way to increase the reaction time of the servo? Here is my code below:
Thanks,
Scott

Sending:

[code]//Sending
int val = 0;
int mapVal = 0;
int potPin = 0;

void setup()
{
Serial.begin(9600);
pinMode(potPin, INPUT);
}
void loop()
{
val = analogRead(potPin);
mapVal = map(val,0,1023,0,179);
Serial.write(mapVal);
}[/code]

Receiving

[code]//receiving

#include <Servo.h>;
Servo scottServo;
int servoPin = 9;
int servoValue = 0;

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

void loop()
{
if(Serial.available() > 0)
{
servoValue = Serial.read();

scottServo.write(servoValue);

}
}[/code]


#2

Hello, Scott.

I don’t expect that you should be experiencing that much lag. Your code seems to be ok. Try removing the XBees from your setup and checking the reaction time with just the pot and servo connected directly to one Arduino. If reaction time is still slow, you could also try a different servo to see if the reaction times are different.

-Ken


#3

OK, Thanks Ken! I’ll give it a try.
Scott


#4

Sorry to resurrect an old thread, but there a couple things that could be changed in your code that might benefit the speed. I’m not sure how Serial.write() handles ints, but it is designed for bytes. You should change mapVal to be a byte in the first program. The second change would be to add a couple millisecond delay to the transmit side loop. Transmitting a byte at 9600 baud takes around a millisecond. Reading an analog pin and doing a little math most likely can be done much quicker. A 2 or 3 millisecond delay will keep you from overflowing the buffers, and will not create noticeable lag.

[code]//Sending
int val = 0;
byte mapVal = 0;
int potPin = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(potPin, INPUT);
}
void loop()
{
  val = analogRead(potPin);
  mapVal = map(val,0,1023,0,179);
  Serial.write(mapVal);
  delay(2);
}[/code]

For the sake of efficiency, you should change servoValue on the receiving side to be a byte as well. An 8-bit processor such as the 328p in Arduinos can process 8-bit numbers much faster than 16-bit numbers. It probably wouldn’t make any noticeable difference in this program, but optimizing like this can be very important in time-sensitive and complex programs.

As a side note, you don’t need a semi-colon after an include statement. This is because includes are handled by the pre-processor, which is the same part of the compilation process that removes comments.

#include <Servo.h>; /Unnecessary semi-colon #include <Servo.h> //Corrected