Can someone help me compact and clean up this code.

Hi guys,
I have tried compress and clean up this code but I haven’t been successful can you guys help? It is for a anamotricic hand.

int Finger1 = 0;
int Finger2 = 1;
int Finger3 = 2;
int Finger4 = 3;
int Finger5 = 4;

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

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

int FingerV1 = analogRead(Finger1);
int FingerV2 = analogRead(Finger2);
int FingerV3 = analogRead(Finger3);
int FingerV4 = analogRead(Finger4);
int FingerV5 = analogRead(Finger5);

if (FingerV1 < 200) FingerV1 = 200;
else if (FingerV1 > 460) FingerV1 = 460;
if (FingerV2 < 200) FingerV2 = 200;
else if (FingerV2 > 460) FingerV2 = 460;
if (FingerV3 < 200) FingerV3 = 200;
else if (FingerV3 > 460) FingerV3 = 460;
if (FingerV4 < 200) FingerV4 = 200;
else if (FingerV4 > 460) FingerV4 = 460;
if (FingerV5 < 200) FingerV5 = 200;
else if (FingerV5 > 460) FingerV5 = 460;

byte servoVal1 = map(FingerV1,460, 200, 255, 0);
byte servoVal2 = map(FingerV2,460, 200, 255, 0);
byte servoVal3 = map(FingerV3,460, 200, 255, 0);
byte servoVal4 = map(FingerV4,460, 200, 255, 0);
byte servoVal5 = map(FingerV5,460, 200, 255, 0);

Serial.print(servoVal1);
Serial.print(servoVal2);
Serial.print(servoVal3);
Serial.print(servoVal4);
Serial.print(servoVal5);


delay(100);
}

Here is the receiving:

#include

Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

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

myservo1.attach(2); // attaches the servo on pin 9 to the servo object
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}

void loop()
{
if(Serial.available() >=5)
{
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();
byte servoAng3 = Serial.read();
byte servoAng4 = Serial.read();
byte servoAng5 = Serial.read();


// Send the servo to the position read...  (note: you get to make this happen)
myservo1.write(servoAng1);
myservo2.write(servoAng2);
myservo3.write(servoAng3);
myservo4.write(servoAng4);
myservo5.write(servoAng5);
}
}

Thanks,
George

It helps if the code actually works in the first place. Your second bit of code wont compile because you haven’t actually included the Servo library - you just put #include with nothing after it.

Anyway, you can use arrays, for example:

#include "Servo.h"

Servo myservo[5]; // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  for (byte i = 0; i < 5; i++){
    myservo[i].attach(i+2);
  }
}

void loop()
{
  if(Serial.available() >=5) {
    byte servoAng[5];
    for (byte i = 0; i < 5; i++){
      servoAng[i] = Serial.read();
      myServo[i].write(servoAng[i]);
    }
  }
}

Have a go at changing the first peice of code to use arrays in a similar fasion to this peice. You will learn more if you have a go yourself.

Try using arrays rather than having 5 similar named variables.

eg.
#DEFINE ARRAYSIZE 5
int Finger[ARRAYSIZE]= {0,1,2,3,4};

byte servoValue[ARRAYSIZE];

Then you can loop over the array rather than having multiple lines with just slightly different variable names.

for (byte loopCount =0; loopCount < ARRAYSIZE; loopCount++)
{
servoValue[loopCount] = Function(FInger[loopCount]);
}

It will also help you spot all those unused variables. It is much easier to declare all the variables at the start of apiece of code rather than some at the start and some part way through.

Edit - OK Tom beat me to it but the same idea.

Great. Thanks guy! I'll have a go myself I just thought I would ask as I've just started and I still abit bamboozled!

Thanks!!

Try here