Problem interfacing servo using Xbee and arduino

Hi,

I’m trying to interface a servo motor using Xbee wireless module. I have written a Java program that reads the Logitech gamepad input and transmits it via a Xbee USB explorer connected to my laptop. I’m using the gamepad to send simple character commands to control the servo. For example, if I press the right joystick up, i send ‘A’, for right joystick down i send a ‘C’ and vice versa. I hooked up another Xbee on a second computer to verify that the commands are being transmitted correctly. Please see this code below:

#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3);  //Rx, Tx

void setup() {
    Serial.begin(9600);
    xbee.begin(115200);
}

void loop() {
  
    if(xbee.available()) {
       Serial.print((char) xbee.read());
    } 
    
    if(Serial.available()) {
        xbee.print((char) Serial.read());
    }
    xbee.flush();
    delay(100);
}

Now having confirmed that the characters are coming in correctly I tried the following to turn the servo based on the commands sent via the Xbee.

#include <Servo.h>
#include <Serial.h>
#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3);  //Rx/Tx

Servo servo;  //define servo
int servoPos = 90;
char ch;

void setup() {
    servo.attach(9);  //drive servo using digital pin 11 of arduino
    
    Serial.begin(9600);
    
    xbee.begin(115200);
}

void loop() {
  if(xbee.available() > 0) {
      ch = xbee.read();
  }
  
  switch(ch) {
     case 'A':
        servo.write(0);
       break;
     case 'B':
        servo.write(90);
        break;
     case 'C':
         servo.write(180);
         break;
     default:
         servo.write(90);
        break; 
  }
  delay(100);
}

The problem I’m facing is that the servo seems to turn fine in one direction and starts twitching, and when i don’t send in any command, I default the servo to turn 90 degrees. Here also, the servo is not exactly positioned at 90 degrees.
The servo movement is not smooth and it’s quite erratic, in the sense, the servo seems to turn to some angle randomly.
Do you see any problem with the code?

Thanks in advance for your replies.

Hi,

just an update on this. The same code when I try with Serial monitor the servo moves correctly:

Here is my code with the Serial monitor:

#include <Servo.h>
#include <Serial.h>

Servo servo;  //define servo
int servoPos = 90;
char ch;

void setup() {
    servo.attach(9);  //drive servo using digital pin 11 of arduino
    
    Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0) {
    ch = Serial.read();
  }
  
  switch(ch) {
     case 'A':
        servo.write(0);
       break;
     case 'B':
        servo.write(90);
        break;
     case 'C':
         servo.write(180);
         break;
     default:
         servo.write(90);
        break; 
  }
  delay(100);
}

As you can see the code is similar to the one using an xbee module. So i would like to know what I’m doing wrong? why don’t the servos move correctly when i’m driving them using commands transmitted via a xbee. Please help.