Help!: wireless controlled robotic hand

Hello, Im trying to code a wireless robotic hand (use servo motor to move) controlled by a sensory glove equipped with flex sensor and IMU sensor. For the robotic hand Im using arduino uno with Xbee s1 and servo motor. For the glove sensor I use arduino nano (XBee-4Nano from mouser electronics https://www.mouser.com/ProductDetail/Gravitech/XBEE-4NANO/?qs=%2Fha2pyFaduiBklULv%2F3a7U3RKwpXbDtkI1ZQJeEhANcuvcpo9BrYIg==) which comes together with an xbee shield.

When Im transferring only one data for example one flex sensor to control one servo the data receive by the uno and send by the nano seem like the one I expected which is only the angle that the servo should move.

However when Im transferring more than one data which is the reading of two sensors or more to map with the servos the data is like the one I attached. It have -1 after every transfered data. This -1 is interupting my sevo movement and makes it look unstable. Please explain why is this happening and help how to solve.

Glove:

#include <Wire.h>
#include <MPU6050.h>

MPU6050 sensor ;

int16_t ax, ay, az ; //accelerometer
int16_t gx, gy, gz ; //gyrosensor

// Variables of the analog read form the flex sensors connected to the analog pins of Arduino Nano
int ResPinky   = A6;

// Variables of the values when the hand is completely opened
// This is needed for a continuous calibration
int OpenedPinky   = 0;

// Variables of the values when the hand is completely closed
// We can't set it to zero since that the minimum value reached
// in the analog read never reach zero. We'll assign the value of
// a first analog read, then the program in the loop will
// automatically  assing lower values
int ClosedPinky;    

// Variables of the values to send
int pinky   = 0;


void setup()
{
  Wire.begin ( );
  Serial.begin(9600);   // Activating serial communication, XBee Series 1 are pre-programmed at 9600 baud/s

  pinMode(ResPinky,   INPUT);
  ClosedPinky   = analogRead(ResPinky);

  sensor.initialize ( );
}

void loop()
{
  pinky   = analogRead(ResPinky);

   // Calibration reading and setting the maximum values. This needs you to completely open your hand a few times
  if (pinky   > OpenedPinky)
    OpenedPinky   = pinky;

  // Calibration reading and setting the minimum values. This needs you to completely close your hand a few times
  if (pinky   < ClosedPinky)
    ClosedPinky   = pinky;


  pinky   = map(pinky  , ClosedPinky  , OpenedPinky  , 0, 180);

  //write() write data to serial port in binary
  //print() print data to serial port in ASCII
  
    Serial.write(pinky);
  

  int servoposition;
  sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
  ax = map (ax, -17000, 17000, 0, 180) ;
  servoposition = constrain(ax, 0, 180);

  Serial.write(ax);
  
  delay(200);
}

Robot:

#include <Servo.h>
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 8 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 9 (Arduino's Software TX)
SoftwareSerial XBee(8, 9); // RX, TX


Servo servo5;
Servo servo6;



void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);


  servo5.attach(10);
  servo6.attach(11);

}

void loop()
{

  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    //the first byte of incoming serial data available (or -1 if no data is available) - int
    int servo5Ang = XBee.read();//Pinky
    int servo6Ang = XBee.read(); //IMU


    if (servo5Ang != 60 || servo5Ang != -1)
    {
      servo5.write(servo5Ang);
      Serial.print(servo5Ang);
    }
    
    if (servo6Ang != 60 || servo6Ang != -1)
    {
      servo6.write(servo6Ang);
      Serial.println(servo6Ang);
    }

  }
}

Capturearduino.PNG

You don't wait for the second value to arrive (hint - avalailable), you just plow ahead and read ectoplasm.

aarg:
You don't wait for the second value to arrive (hint - avalailable), you just plow ahead and read ectoplasm.

Can you explain further, I dont understand. It is that I need to add delay each time I write to the xbee?

qistie:
Can you explain further, I dont understand. It is that I need to add delay each time I write to the xbee?

No. Delaying won't help.
Why did you check available() for the first value? Did you do it for the second one? Serial transmission takes time.

Robot:

  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    //the first byte of incoming serial data available (or -1 if no data is available) - int
    int servo5Ang = XBee.read();//Pinky
    int servo6Ang = XBee.read(); //IMU


    if (servo5Ang != 60 || servo5Ang != -1)
    {
      servo5.write(servo5Ang);
      Serial.print(servo5Ang);
    }
    
    if (servo6Ang != 60 || servo6Ang != -1)
    {
      servo6.write(servo6Ang);
      Serial.println(servo6Ang);
    }

  }
}

[/quote]

I thought the available() is to check if data is received. and I think this one is to check each data coming not just the first one. I still dont understand why -1 exist in the data.

Because you read the second value before it has actually arrived.

Actually, delaying the second transmission will work but it's a terribly stupid fix.

aarg:
Because you read the second value before it has actually arrived.

Actually, delaying the second transmission will work but it's a terribly stupid fix.

delaying the second data did work but it also delay the robot movement which will cause it to be not accurate in timing. Is there any other way other than delaying. Im trying to be creative fixing this problem but this is my first time using xbee so i really have no idea

For the third(?) time...
You must always check to see if data is available before reading it.

The second read occurs mere nanoseconds after the first... the second data hasn't arrived yet.

That helps a lot. Thank you very much for your time and assistance :slight_smile:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R