Individual servo control using flex sensor readings [SOLVED]

Hi I am having some problems writing code for my project
I’m building a robotic hand that uses flex sensors to wirelessly move servos according to values that the flex sensors give.
The problem that I am facing is getting each servo to correspond to the correct servo motor.
So far I am able to get one servo motor running but I need to get five working.
The code I have written so far is to try and get two to move.
im using 2 Arduino unos , 2 Xbee shields and 2 Xbee series 1’s
the robotic hand is wirelessly controlled by a glove with 5 flex sensors sewn in (sending Arduino)
the robotic hand is controlled by servos that respond to the amount of flex in each individual flex sensors (receiving Arduino)
The problem strictly lies in the programming as i have tested everything else to be working fine
the code im using so far for 2 servos is:

Sending Arduino:

int Finger1 = A0;
int Finger2 = A1;

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

void loop()
{
  byte servoValue1;
  byte servoValue2;
  
  int FingerV1 = analogRead(Finger1);
  int FingerV2 = analogRead(Finger2);
  
  if (FingerV1 < 390) FingerV1 = 390;
  else if (FingerV1 > 700) FingerV1 = 700;
  if (FingerV2 < 515) FingerV2 = 515;
  else if (FingerV2 > 705) FingerV2 = 705;
  
  byte servoVal1 = map(FingerV1,390, 700, 0, 9);
  byte servoVal2 = map(FingerV2,515, 705, 0, 9);
  
  Serial.print(servoVal1);
  Serial.print(servoVal2);

  delay(100);
}

receiving Arduino:

#include <Servo.h>

int servoPin1 =10;
int servoPin2 =9;

Servo myservo1;
Servo myservo2;

void setup()
{
  Serial.begin(9600);
  
  myservo1.attach(servoPin1); 
  myservo2.attach(servoPin2);
  
  delay(500);
}

void loop()
{
  while(Serial.available() == 0);
  {
  byte data1 = Serial.read() - '0';

  int pos1 = map(data1, 0, 9, 0, 180);
  pos1 = constrain(pos1, 0, 180);
  myservo1.write(pos1);
  }
  {
  byte data2 = Serial.read() - '0';

  int pos2 = map(data2, 0, 9, 0, 180);
  pos2 = constrain(pos2, 0, 180);
  myservo2.write(pos2);
  }

  
  Serial.flush();

}

i would appreciate any help :slight_smile:

  while(Serial.available() == 0);
  {
  byte data1 = Serial.read() - '0';

  int pos1 = map(data1, 0, 9, 0, 180);
  pos1 = constrain(pos1, 0, 180);
  myservo1.write(pos1);
  }
  {
  byte data2 = Serial.read() - '0';

The curly braces look stupid when they are not needed.

Wait until there is at least one byte to read. Then, read 2 bytes. Nope. Hasn't worked for anyone else. Won't work for you.

ok so no curly brackets

void loop()
{
  while(Serial.available() == 0);
 
  byte data1 = Serial.read() - '0';

  int pos1 = map(data1, 0, 9, 0, 180);
  pos1 = constrain(pos1, 0, 180);
  myservo1.write(pos1);

  byte data2 = Serial.read() - '0';

  int pos2 = map(data2, 0, 9, 0, 180);
  pos2 = constrain(pos2, 0, 180);
  myservo2.write(pos2);
 

  
  Serial.flush();

}

i'm not quite sure how to get each servo motor to correspond to each flex sensor. is there anything that i'm not including in the code to split the values being sent to the servos? or is it the programming in the servos that isn't right? or both?
this is my first Arduino project so i'm don't have a full understanding of the terminology used.

ok so no curly brackets

And, no comments, and no substantive changes. You are still waiting for one byte to arrive, and then reading two bytes. Stop that!

You've opened a nasty can of worms with having two threads with the exact same question.....

so then i should get it to read two bytes instead of waiting for one? what exactly do i need to change to make that happen?
and im sorry for posting this twice i realized that i had put it in general electronics rather than programming questions, my mistake

so then i should get it to read two bytes instead of waiting for one? what exactly do i need to change to make that happen?

Say what? If you want to do something when there are two bytes to read:

void loop()
{
   if(Serial.available() >= 2)
   {
      byte one = Serial.read();
      byte two = Serial.read();
   }
}

ok so how does this look than

void loop()
{
   if(Serial.available() >= 2)
   {
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();


myservo1.write(servoAng1);
myservo2.write(servoAng2);
   }
 
Serial.flush();

}

ok so how does this look than

No so good. Writing 0 to 9 to the servos doesn't seem that useful.

What do you think Serial.flush() is doing?

i thought that Serial.flush gets rid of the extra data that might come in from the serial.Read that isnt needed

i thought that Serial.flush gets rid of the extra data that might come in from the serial.Read that isnt needed

If you are still using 0022 or 0023, that is correct. If you are using any version released in the last couple of years, Serial.flush() blocks waiting for all pending OUTPUT data to be sent. Not terribly useful for you, since you never send any data (from the receiver).

having the Serial.flush dosn't seem to make a difference if anything its a little better with it.
what should i do instead of writing 0-9 to the servos.
originally it was supposed to turn the servos to 9 different positions but with the code :

   if(Serial.available() >= 2)
   {
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();


myservo1.write(servoAng1);
myservo2.write(servoAng2);
   }

it seems to be moving only a tiny bit

what should i do instead of writing 0-9 to the servos.

What are you sending? You read an analog sensor, which returns a value in the range 0 to 1023. Then, you map that to a range of 0 to 9, which you then send as '0' to '9'.

On the receiving end, you read '0' to '9', and assume that they mean something to the servos. OK. What does '5' mean to the servo?

Since the servo can move through a much larger range than '0' to '9', one has to wonder why you are sending '0' to '9'.

Use 0 to 255 (or 180) as the to range in the map() calls. Then, use Serial.write() to send the data. Then, your receiver code will do something more like you expect it to.

wow thanks to everyone for their help!!
it finally works after i changed a few things around :smiley:
i think that the main problem was that i was using serial.print instead of serial.write at the end because i have tried endlessly at altering the code but nothing ended up working until i changed the serial.print
this is the final code for anyone wondering what it is:
sending Arduino:

int Finger1 = A0;
int Finger2 = A1;
int Finger3 = A2;
int Finger4 = A3;
int Finger5 = A4;

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

void loop()
{
  byte servoValue1;
  byte servoValue2;
  byte servoValue3;
  byte servoValue4;
  byte servoValue5;

  int FingerValue1 = analogRead(Finger1);
  int FingerValue2 = analogRead(Finger2);
  int FingerValue3 = analogRead(Finger3);
  int FingerValue4 = analogRead(Finger4);
  int FingerValue5 = analogRead(Finger5);
  
  if (FingerValue1 < 390) FingerValue1 = 390;
  else if (FingerValue1 > 700) FingerValue1 = 700;
  if (FingerValue2 < 515) FingerValue2 = 515;
  else if (FingerValue2 > 705) FingerValue2 = 705;
  if (FingerValue3 < 380) FingerValue3 = 380;
  else if (FingerValue3 > 700) FingerValue3 = 700;
  if (FingerValue4 < 410) FingerValue4 = 410;
  else if (FingerValue4 > 700) FingerValue4 = 700;
  if (FingerValue5 < 400) FingerValue5 = 400;
  else if (FingerValue5 > 700) FingerValue5 = 700;
  
  byte Value1 = map(FingerValue1,390, 700, 0, 180);
  byte Value2 = map(FingerValue2,515, 705, 0, 180);
  byte Value3 = map(FingerValue3,380, 700, 0, 180);
  byte Value4 = map(FingerValue4,410, 700, 0, 180);
  byte Value5 = map(FingerValue5,400, 700, 0, 180);

  Serial.write(Value1);
  Serial.write(Value2);
  Serial.write(Value3);
  Serial.write(Value4);
  Serial.write(Value5);

  delay(100);
}

the receiving Arduino:

#include <Servo.h>

int servoPin1 =11;
int servoPin2 =10;
int servoPin3 =9;
int servoPin4 =6;
int servoPin5 =5;

Servo Servo1;
Servo Servo2;
Servo Servo3;
Servo Servo4;
Servo Servo5;

void setup()
{
  Serial.begin(9600);
  
  Servo1.attach(servoPin1);
  Servo2.attach(servoPin2);
  Servo3.attach(servoPin3);
  Servo4.attach(servoPin4);
  Servo5.attach(servoPin5);
  
  delay(500);
}

void loop()
{
 {
   if(Serial.available() >= 5)
   {
byte ServoAngle1 = Serial.read();
byte ServoAngle2 = Serial.read();
byte ServoAngle3 = Serial.read();
byte ServoAngle4 = Serial.read();
byte ServoAngle5 = Serial.read();

Servo1.write(ServoAngle1);
Servo2.write(ServoAngle2);
Servo3.write(ServoAngle3);
Servo4.write(ServoAngle4);
Servo5.write(ServoAngle5);
   }
 }
 }

i will upload a photo as soon as i get the fingers stringed up :smiley:
Thanks again to everyone for their help and support!!

Glad it's sorted, so now you can indulge me: you said it's wireless comms between two Arduinoes I think?- yet I see nothing in the code to do with that. Or am I missing something?

byte ServoAngle1 = Serial.read();
byte ServoAngle2 = Serial.read();
byte ServoAngle3 = Serial.read();
byte ServoAngle4 = Serial.read();
byte ServoAngle5 = Serial.read();

Two or more variables with the same name apart from a numerical suffix should always suggest that use of arrays would be appropriate.

  byte servoValue1;
  byte servoValue2;
  byte servoValue3;
  byte servoValue4;
  byte servoValue5;

What are these for?

  if (FingerValue1 < 390) FingerValue1 = 390;
  else if (FingerValue1 > 700) FingerValue1 = 700;
  if (FingerValue2 < 515) FingerValue2 = 515;
  else if (FingerValue2 > 705) FingerValue2 = 705;
  if (FingerValue3 < 380) FingerValue3 = 380;
  else if (FingerValue3 > 700) FingerValue3 = 700;
  if (FingerValue4 < 410) FingerValue4 = 410;
  else if (FingerValue4 > 700) FingerValue4 = 700;
  if (FingerValue5 < 400) FingerValue5 = 400;
  else if (FingerValue5 > 700) FingerValue5 = 700;

or 5 calls to constrain().

As promised attached is a picture of my project :slight_smile:
just got to tweek the fingers a bit to make them bend more and it should be all sweet.
in response JimboZA the Serial.begin a=establishes a serial connection that the xbee’s communicate on.
if you want to know more Jeremy Blum does some great tutorials using xbees on youtube :slight_smile:
thanks again to everyone for their help

MicklePickle:
JimboZA the Serial.begin a=establishes a serial connection that the xbee's communicate on.

Ah, ok, thanks.

Hi I am having some problems writing code for my project which is robotic claw that uses flex sensors to wirelessly move servos.
The problem that I am facing is code for the servo motor to operate by varying the flex sensor. can anybody help me in this..??