Arduino Xbee flex sensors to servo motor communication problems

Hi i’m making a robotic hand for my school project and im having a lot of problems with programming.
im using:
2 Arduino Unos with 2 Xbee shields and 2 series 1 Xbees
one setup reads the values of flex sensors and the other turns the servo motors
the servos im using are: towardpro MG946R

I think i have the sending Arduino working properly but im not quite sure about the receiving Arduino.
when i test one flex sensor for 1 servo the servo wont move to the specified position, instead it just humms and flickers

right now im running the servo from a 6v battery supply (4 AA batteries)

here is the code for the sending Arduino:

int finger = A0;

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

void loop()
{
int val = map(analogRead(finger), 390, 700, 0, 9);
Serial.println(val);
delay(100);
}

and the code for the recieving:
#include <Servo.h>

int servoPin =10;

Servo Servo1;

void setup()
{
Serial.begin(9600);

Servo1.attach(servoPin);

delay(500);
}

void loop()
{
while(Serial.available() == 0);

int data = Serial.read() - ‘0’;

int pos = map(data, 0, 9, 0, 180);
pos = constrain(pos, 0, 180);

Servo1.write(pos);
Serial.flush();
}

this is just to test 1 finger and to get it to move to 9 different positions.
i would really appreciate any help :slight_smile:

I don't know anything about xbee, but how are you making sure that the correct message arrives at the receiving end? Rather than drive a servo, I'd suggest you do something really mundane, like turn a led on or off..... That should help you isolate the problem

i have already confirmed that the two xbee are communicating properly with an led test
ive used putty to program both xbee’s
ive also run tests to make sure the flex sensors are getting the proper readings as well as that the servos are operating properly

Try using print instead of println in the sending code. Look up what flush does too - it's likely not what you think. Alternatively, just get rid of it.

Thank you so much for the help JimboZA and wildbill!
I changed the println to print and everything runs smoothly, funny how such a small thing can make a big difference!
hopefully i can get all the others to work as well :slight_smile:

So what was the problem with println?- the CR/LF after the data screwing it up?

Yes. I think the OP assumed that flush would get rid of the line ending, but these days it doesn’t.