using an fsr for grip control

Hi folks ive been having a problem trying to add grip control using a force sensor to a mechanical claw project that i have going. Ive got code written that can do this fine in movement simulations in loops, or when controlling the claw through the serial interface using the keyboard.
But now im using a glove with flex sensors sewn into to it, to control the claw over an xbee connection. So when i move my fingers or wrist the mechanical claw will move too. Now with this real time control, the code ive been using for grip control isnt working. This is what ive been using

sending

int Finger1 = 0;
int Finger2 = 1;
int Finger3 = 2;



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

void loop()
{  

   int FingerV1 = analogRead(Finger1);
   int FingerV2 = analogRead(Finger2);
   int FingerV3 = analogRead(Finger3);
  
   
   if (FingerV1 < 385) FingerV1 = 385;
   else if (FingerV1 > 550) FingerV1 =550;
   
   if (FingerV2 < 425) FingerV2 = 425;
   else if (FingerV2 > 485) FingerV2 = 485;
   
   if (FingerV3 < 370) FingerV3 = 370;
   else if (FingerV3 > 490) FingerV3 = 490;
   
   
  
   byte servoVal1 = map(FingerV1,550, 385, 0, 180);//twist
   byte servoVal2 = map(FingerV2,485, 425, 0, 180);//bend
   byte servoVal3 = map(FingerV3,370, 490, 0, 180);//grab
  
   
   Serial.print(".");
   Serial.print(servoVal1, DEC);
   Serial.print(",");
   Serial.print(servoVal2, DEC);
   Serial.print(",");
   Serial.print(servoVal3, DEC);
   Serial.print(",");
 
   
}

and receiving

#include <Servo.h>

Servo myservo1;  
Servo myservo2;
Servo myservo3;


int pressurePin =A1;



// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';

void setup ()
{
  Serial.begin(9600);
  
   myservo1.attach(8); //twist
   myservo2.attach(9); //bend
   myservo3.attach(10); //grab
  
} // end of setup

int twist, bend, grab;

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {

  // convert strings into numbers:

  middle = atoi (strtok (data, ","));
  thumb = atoi (strtok (NULL, ","));
  ring = atoi (strtok (NULL, ","));
  pointer = atoi (strtok (NULL, ","));
  rotation = atoi (strtok (NULL, ","));
  
    myservo1.write(twist);
    myservo2.write(bend);
    myservo3.write(grab);
     if (analogRead(pressurePin) > 450)// heres the grip control
    myservo3.detach();
    
    if (analogRead(grab) >170) // and here's where im getting the problems 
{
    myservo3.attach(10);
    myservo3.write(grab);
}
    
  }  // end of process_data
  

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case TERMINATOR:   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case STARTER:  
        // reset buffer 
        input_pos = 0;        
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data
}  // end of loop

The idea is that the flex sensors will read the position of my fingers and wrist and send that data over xbee radio to the other arduino, and the mechanical claw will mirror the movements
When i clench my hand the gripper will close on an object, the pressure sensor will cut off the power to the servo when the grip becomes tight enough and the claw will be left holding the object. Then when i unclench my hand to full extension with the flex sensors will read this and send a command to the arduino to give power back to the claw and the claw will drop the object.

The problem is that im relying on data from the flex sensors to trigger power back to the gripper servo. And the data is never constant. I tried printing to the serial monitor the values the claw arduino is receiving over xbee, from the flex sensor, and it just gave a value of 423 descending to around 200 in about 10seconds.

i dont think this method can work with this application, but if anyone has any suggestions I'm all ears.

also if anyone has any other ways of using an fsr for grip control I'd love to hear it

Did you test the flex sensors? Maybe it's the way your sending the data. Instead of case statements being used like that, use if statements to split the data at the commas and the case statements to store the data.

If (data == ',') My_case++;
else // store data to servo array.

Switch(My_case) {
case 0: /* servo1 data*/ break;
case 1: /* servo2 data*/ break;
.
.
}

Flex sensors in a glove probably aren't going to very precise or consistant in operation.

You are sending three values, and expecting five. Why?

You seem to not be aware of Tools + Auto Format. It's time you remedied that.

the

middle = atoi (strtok (data, ","));
thumb = atoi (strtok (NULL, ","));
ring = atoi (strtok (NULL, ","));
pointer = atoi (strtok (NULL, ","));
rotation = atoi (strtok (NULL, ","));

Is just a mistake, theres supposed to be just 3 named twist, bend and grab.

The data from the flex sensors is never very accurate, the ranges change slightly every time i use them. but they're good enough to be used to position the servos.

I think it is a problem with the way the data is being sent. I printed the value that the receiving arduino got from the flex sensors and it doesnt give constant data. When you open the serial monitor it just shows numbers from 400 descending to around 200 and staying there. Beats me how the arduino is using that to control the servos.

Never heard about the auto format, but then theres a lot i havent heard of. Could you explain more about your idea hazardsMind. I dont understand how id use that in the program

Your method of receiving the incoming data could be simplified. Your sending a byte and SerialRead receives one byte. Now depending on how you go about sending the data, determines the best way to receive it and split it.

My method looks at the data coming in and determines were to put it based on what characters it sees coming in. My example looks of a point to split the data, in this case a comma. Every time it sees a comma it will switch the case statement to another array and continue to store data until told to switch or stop. 3 cases means 3 unique servo movements.

So if you sending:
Send: 255,100,98.
It will split the data like so.

Receive:
Servo1 = 255
Servo2 = 100
Servo3 = 98

Some multiple servo receiving test code. If this works, you might code on the sending side to send in this format.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Ill try that first chance i get. Thanks guys