Hi. I am trying to use two XBee S1 and two Arduino Uno to control a servo motor (continuous rotation) wireless with a joystick. I already got everything working if I do everything in one arduino, so the joystick code and the code to write the value to the servo work perfectly. The problem is when I connect my servo with the XBee shield, the servo is always rotating in one direction at full speed. I have been told it has something to do with the serial connection the XBees use. I was told to use Software Serial to change my RX and TX pins. I changed XB_TX to 6 and XB_RX to 7 on both Shields. However, I don't see anything on the serial monitor when I try this.
This is my code for the reciever:
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial XBee(6, 7); // RX, TX
const int servo1 = 9; // first servo
int servoVal; // variable to read the value from the analog pin
Servo myservo1; // create servo object to control a servo
void setup() {
//Servo
myservo1.attach(servo1); // attaches the servo
// Inizialize Serial
XBee.begin(9600);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
{ // If data comes in from serial monitor, send it out to XBee
XBee.write(Serial.read());
}
if (XBee.available())
{ // If data comes in from XBee, send it out to serial monitor
Serial.write(XBee.read());
}
if (Serial.available() > 0) {
servoVal = Serial.read();
Serial.write(servoVal);
myservo1.write(servoVal);
//while(Serial.available()>0) Serial.read(); //flush?
}
}
And, this is my code for the transmitter:
//#include <Servo.h>
#include <SoftwareSerial.h>
//// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
//// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(6, 7); // RX, TX
const int joyV = 0; // U/D Parallax Thumbstick
int servoVal; // variable to read the value from the analog pin
void setup() {
// Inizialize Serial
XBee.begin(9600);
Serial.begin(9600);
}
void loop(){
if (Serial.available())
{ // If data comes in from serial monitor, send it out to XBee
XBee.write(Serial.read());
}
if (XBee.available())
{ // If data comes in from XBee, send it out to serial monitor
Serial.write(XBee.read());
}
servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 73, 120); // scale it to use it with the servo (result between 70 and 180)
Serial.println(servoVal);
}