force sensor control for servos

Hi Guys im new to Arduino and this is my first post.

Im working on a project for college where im designing a robotic hand that i will be controlling through simulations.

I have a good idea of how im going to do it but im really stuck on one part - the grip control.

ive got force sensors telling the hand when to stop squeezing, and im having a devil of a time with the if commands. when the hand grips something hard enough i want the motors to stop squeezing and just hold it, but they just go on through the programme and then follow the if command once done

this is the code im using - im first doing a simulations with one motor, i want it to go through a series of movements and as soon as i press on the sensor hard enough it stops, and moves to 45degrees

#include <Servo.h>

int sensorPin = 0;
int sensorValue;
Servo servoClaw;

void setup()
{
servoClaw.write(0);
servoClaw.attach(9);
}

void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue<450){

openClaw();
delay(2000);
closeClaw();
delay(2000);
}

sensorValue = analogRead(sensorPin);

if(sensorValue>450){ // i just want the movements to stop as soon as i press on the force sensor and move to 45, but instead it finishes going from 0-180 and then goes to 45
servoClaw.write(45);
}
}

void openClaw(){
servoClaw.write(0);
}

void closeClaw(){
servoClaw.write(180);
delay(2000);

}

if anyone could give me a hand with this id much appreciate it

When you send a new position to the servo it will move to that position until it either reaches that position or you send it another position to go to.

One way to control how far the servo moves is to move it incrementally (See: File->Examples->Servo->Sweep) and after every small step check for your force sensor limit.

that does sound like what I'm after. I had a look at the sweep example and I can see how it would be helpful for speed control. but what kind of command would make the arduino check the pressure after eCh increment ?

Costello:
that does sound like what I’m after. I had a look at the sweep example and I can see how it would be helpful for speed control. but what kind of command would make the arduino check the pressure after eCh increment ?

Something like:

   if(analogRead(sensorPin)<450)
      break;

The “break” statement will cancel the rest of a FOR or WHILE loop. The loop variable will be left set to the last value used.

int position:

void closeClaw() {
  int i;
  for (i = position; i < 180;  i++) {
    servoClaw.write(180);
    if(analogRead(sensorPin)<450)
      break;
  }
  position = i;
}

i tried the code but it must be the way i typed it because it didn’t work for me.
would you have a look at the sketch i used and let me know where i went wrong, i used the sweep code this time
#include <Servo.h>

const int servoPin = 9;
const int sensorPin = 0;

int sensorValue = 0;
int position;

Servo myservo;
int pos = 0;

void setup(){
myservo.write(180);
myservo.attach(8);
pinMode(sensorPin, INPUT);
}

void loop(){

{

openClaw();
delay(2000);
closeClaw();
delay(2000);
}
}

void openClaw(){
for (pos = 180; pos>=1;pos-=1)
{
myservo.write(pos);
delay(15);
}
}

void closeClaw() {
int i;
for (i = position; i < 180; i++) {
myservo.write(180);
if(analogRead(sensorPin)>450)
break;
}
position = i;
}

I think part of the problem is that I wrote “myservo.write(180);” where I should have written “myservo.write(i);”. Sorry about that.

Another problem is that when you “open” the claw you start by closing it all the way.

No need to slow down the opening of the claw so the loop there is not necessary.

#include <Servo.h>

const int servoPin = 8;
const int sensorPin = A0;

int position;

Servo myservo;

void setup(){
  myservo.write(0);  // Open claw.
  position = 0;
  myservo.attach(servoPin );
}

void loop(){
  openClaw();
  delay(2000);
  closeClaw();
  delay(2000);
}

void openClaw() {
  myservo.write(0);  // Go to open position
  position = 0;
}

void closeClaw() {
  for (; position < 180;  position++) {
    myservo.write(position);
    if(analogRead(sensorPin) > 450)
      break;
  }
}
[/quote]

Thanks a lot John your a magician:)
i treid out the code and it worked a treat. i didn’t use the “break” function because the loop would reset, and the claw will open and drop the object.
instead i used “detach” and it just switched the servo off when the claw had a grip on the object. now i can use other servos to move the wrist around to transport the object. and then just use “attach” to give back power to the claw and it will release the object.

Job done. Thanks a lot John, i wouldnt have got it without your help.
i added the code so anyone who wants to see how to use grip control can use it

#include <Servo.h>

const int servoPin = 8;
const int sensorPin = A0;

int position;
int val;

Servo myservo;

void setup(){
myservo.write(0); // Open claw.
position = 0;
myservo.attach(servoPin );
}

void loop(){
openClaw();
delay(2000);
closeClaw();
delay(2000);
}

void openClaw() {
myservo.attach(8);
myservo.write(0); // Go to open position
position = 0;
}

void closeClaw() {
for (; position < 180; position++) {
myservo.write(position);
delay(40);
if(analogRead(sensorPin) > 450)
myservo.detach();

}
}

next step now that i got the robotic hand movement simulations finished. im going to start doing up code to control it through a data sheet. ill post anything i do in case anyone is interested