Adjust gripper angle or pressure

Greetings all and happy Thanksgiving!

Please, I am trying to write a code to control the gripper closing force or angle according to a reading from a sensor ( I am not sure if only one sensor needed or two)

so the gripper servo will respond and go to the best closing angle which is enough to:

Hold tight the object

Lift the object for two seconds or so

put back the object

Release object

The serial monitor will display Force applied and adjusted gripper Angle continuously while dealing with each object

The gripper will expect another object ( I am trying to use three different sizes or weights) to gripp, hold and lift and put back so in this case the gripper will adjust to new angle or pressure. My gripper works between ~ 45 degrees (full open) to ~ 110 degrees (full close)

Please i need help with such a code. I could get a reading from sensors every time i press on them but i could not so far able to code the gripper adjustment around each object

Here is my test code:

#include <Servo.h>

Servo s1;
Servo s2;
int pos;
int fsr = 15;     // pin for sensor and 10k resistor
int force;     // analog reading 
 
void setup() 
{
  Serial.begin(9600);
  s1.attach(8);   
  s2.attach(9);  
}
 
void loop()
{
 s2.write(45); //start gripper open
 delay(1000);
 s2.write(100);
   force = analogRead(fsr);  // FSR reading
  Serial.print("FSR reading = "); 
  Serial.println(force); // display fsr value when gripper close
  pos = map(force, 0, 1023, 45, 100); // relates force reading to degrees of gripper servo position
  Serial.print("Gripper Angle = ");
  Serial.println(pos); // gripper angle reading from pos
  delay(300);
if(force < 1 ){
  s2.write (90);

  if(force = 10){
    s2.write(85);

    if(force >18){
      s2.write(75);
    }
  }
}  
  s2.write(pos); // go to pos
  delay(250); // Delay 250 milliseconds 

s1.write(25); //the object will be lifted
  delay (3000);
  s1.write(0); // put down object
  delay (1000); 
}

Electronics:

I have one sensor attached to each gripper finger.

One servo for gripper

One servo to lift arm

Arduino Mega 2560 microcontroller

Interlink Electronics 0.5" Circular FSR

Interlink Electronics 1.5" Square FSR

Thank you so much. Your help will make much difference!

Greetings all,

I am still working on a solution. This project is very challenging and hopefully will look great when done.

Thank you!

How is the force sensor response? 0 means no force, 1023 means full grip? In the if-statements you seem to loosen the grip as the force value increases. What happens when force is between 1 and 10, or 11 and 18? Does it not care how much it should pinch?

p.s. Learn how to use logic in if-statements properly. In your code, when force is less than 1, how can force then be equal to 10 in the next if-statement? And subsequently when force is determined to be equal to 10, how can it be larger than 18? You are executing the if-statements in a dependent fashion. So basically in all this you are checking if force is 0 or not. Setting the servo to 90 when force is 0, and just continue (not commanding the servo there) when it is not. That is not a very good grip control.