Turn on servo when pot value in range, please help

Hi i'm having a bit of trouble starting out with arduino.

Basic i have a potentiometer, and when a certain value is read i want to be able to turn on a servo to lock its position, no values sent to it just turn on the servo so it will lock.
And when the potentiometer is not in the value range to lock the postion i want the servo to be able to freely move.

Can anyone help me with a bit of code or point me in the right direction.
I'm not sure what functions like attach, detach to use.
I didn't set a range to attach and detach to servo but it didn't work for me.

Any suggestions??
Thank you.

Where is the code you have? What does it not do correctly?

I've tried a few variations of attach and detach, but nothing seems to work the way i want it too.
This version will attach the servo when i reach the certain value but i can't seem to detach it. now i have put under the attach function ''detach at certain value'' but never actually detached.
Any help?
Apologies if the code is badly written :slight_smile:
Thank you.

#include <Servo.h>

Servo robotservo;
Servo fingerservo;

const int fingerpospin = A0; // finger potentiometer
int fingerposval = 0; // variable to read the value from the analog pin
int fingerposout = 0; //variable to read the value from the analog pin
const int fingertouchpin = A2; // robot resistive sensor
int fingertouchval = 0;
int fingertouchout = 0;

const int analogOutPin = 9; // Analog output pin that the LED is attached to

void setup()
{
robotservo.attach(2); // attaches the servo on pin 9 to the servo object
fingerservo.attach(3);
Serial.begin(9600);
}

void loop()

{
fingerposval = analogRead(fingerpospin);
fingerposout = map(fingerposval, 0, 1023, 0, 179);
robotservo.write(fingerposout);

// read the analog in value:
fingertouchval = analogRead(fingertouchpin);
// map it to the range of the analog out:
fingertouchout = map(fingertouchval, 0, 1023, 179, 0);
// change the analog out value:
fingerservo.write(fingertouchout);

if (fingertouchval >= 70) { fingerservo.detach();
}

// print the results to the serial monitor:
Serial.print("fingerposout = " );
Serial.print(fingerposout);
Serial.print("\t fingerposval = " );
Serial.print(fingerposval);
Serial.print("\t fingertouchout = ");
Serial.print(fingertouchout);
Serial.print("\t fingertouchval = ");
Serial.println(fingertouchval);

delay(100);
}

What are you expecting to have happen when the servo is detached? What does your serial output look like? 70 is a pretty low value for the threshold.

Your variable names need some work.

int fingerposout = 0;               //variable to read the value from the analog pin

That's not what it's for. Is the finger position ever going to be on input? If not, then out in the name seems silly. Using camelCase for names makes them easier to read, too.

Most of your global variables do not need to be global, and should not be global.

const int fingertouchpin = A2;  // robot resistive sensor

What is a robot resistive sensor?

There's too much code in your sample sketch. Just write a test case that does the attach and/or detach as you want, and see whether your servo does what you want when it is detached. Something like this, perhaps:

// untested
#include <Servo.h>

Servo fingerservo;

void setup()
{
	fingerservo.attach(3);
	Serial.begin(9600);
	
	Serial.println("Servo moved to 90");
	fingerservo.write(90);         
	
	delay(10000);
	Serial.println("Servo detached");
	fingerservo.detach();
}

void loop()
{
}

This is going to be a small part of a larger program hence some names might not make sense.

When the servo is detached all i want is the servo to be able to move freely, when attached and servo to be locked (or on waiting for position)

The reason for 70 is once the value is over 70 i want to attach the servo, below it not attached. just incase there some variability filter issues. Its actually a resistive sensor not potentiometer(i was just simplifying the details to explain easier), so when pressure applied to the resistive sensor it will lock the servo.

Yes finger position is going to control a different servo in another part of the program.

Thank you peter, i will try that in the morning.

When the servo is detached all i want is the servo to be able to move freely, when attached and servo to be locked (or on waiting for position)

Not going to happen. If the servo is moved when "unlocked", then there is no way for the arduino to know the current position of the servo to "lock" it.

Peter that worked, i also looped the code and it continuously attached a detached while when attached returned to 90 degrees.
Now would there be a way to attach the servo and stay in the same position in which i manually moved it too?
I have modified the servo motor that i can read the servo position by just soldering a wire the the servo pot, if i read the position then maybe when i attach servo just writing to that position?
OR
would there be an easier way???

PeterH:
There's too much code in your sample sketch. Just write a test case that does the attach and/or detach as you want, and see whether your servo does what you want when it is detached. Something like this, perhaps:

// untested

#include <Servo.h>

Servo fingerservo;

void setup()
{
fingerservo.attach(3);
Serial.begin(9600);

Serial.println("Servo moved to 90");
fingerservo.write(90);         

delay(10000);
Serial.println("Servo detached");
fingerservo.detach();

}

void loop()
{
}

I have soldered a wire to the servo pot so i can always read the servo pot position.
Would that help?
Maybe i can just write the that new location by using peters test code from previous?

zoomkat:

When the servo is detached all i want is the servo to be able to move freely, when attached and servo to be locked (or on waiting for position)

Not going to happen. If the servo is moved when "unlocked", then there is no way for the arduino to know the current position of the servo to "lock" it.

foster182:
I have soldered a wire to the servo pot so i can always read the servo pot position.
Would that help?
Maybe i can just write the that new location by using peters test code from previous?

Yes, but you will need to convert from the analog value you get from the servo potentiometer to the corresponding servo angle/pulse length, and it will need some trial and error to work out the exact characteristics of your particular servo potentiometer to get that right. Since your calculation will never be absolutely perfect, you may still find the servo jumps slightly when you reattach it. If you felt it was worth the bother, you could continue to monitor the potentiometer position after reattaching the servo, and adjust the servo back to the original position.

While this is all feasible, I do wonder if a servo is actually what you need in this application since you seem to be using it essentially as a brake.

ok, would there be an easier way to just active the servo to create the brake effect?
I'm surprised you can't just attach and power up the servo without driving to a position.

This type of servos is designed to produce specific behaviour. You direct them to go to a position, and they go there and stay there.

That behaviour is not what you want here. Conceptually, what you want is a brake. I have no idea what the mechanism is you're connecting to this, so I have no idea what options you have to add a brake. Using a motor as a brake and driving it via a gearbox is likely to be pretty hard on the gearbox and require significant force to be applied to it. You might well be able to make it work, but it doesn't feel like a smart solution.

Perhaps if you could explain a bit more about what the mechanism is, we could suggest better approaches. For example, there have been quite a few discussions about electronically controlled faders and dials that could also be operated manually; maybe one of the ideas in those past discussions would apply to your problem.

Ok i'm attempting to build a haptic glove that can also control an external robot hand and receive input to stop finger motion on the glove
The servos i'm looking to use with a brake function are on the glove in the picture.
I want to be able to freely move the fingers, ie servo linkages and when the external robots fingertip comes into contact with some object, i want to be able to stop the servo from allowing to finger to move freely be activating the servo which will initially work in theory as a brake.

I know its not what the servo was intended for, but i'm sure there a way around to use the servo for what i need.
At the moment i don't have any other choice but to try it this way :~

I have soldered a wire to the servo pot so i can always read the servo pot position.
Would that help?

When you say act as a brake, I assume you mean hold position and not move. Anyhow, below is some tinkering on using the pot in the servo to determine its position.

http://www.lynxmotion.net/viewtopic.php?f=2&t=2748

http://www.lynxmotion.net/viewtopic.php?f=31&t=3182

foster182:
when the external robots fingertip comes into contact with some object, i want to be able to stop the servo from allowing to finger to move freely be activating the servo which will initially work in theory as a brake.

I know its not what the servo was intended for, but i'm sure there a way around to use the servo for what i need.
At the moment i don't have any other choice but to try it this way :~

I see what you're getting at now. I can see a couple of drawbacks with this approach. Firstly, the servo's resistance to movement is not directional - it would stop you from opening your hand, as well as closing it. Secondly, the servo will have considerable resistance to movement even when it is 'free'.

If I was you I'd look for a way for the servo to act as an end stop for the finger movement, i.e. so that it stops the operator's grip from closing but not opening, rather than being locked to the finger. I don't know what you're using to detect the finger position, but movement sensing and movement restriction would obviously need to be designed to work together. If you're using a finger sensor based on pressure, you could simply let the servo move to track the position of the robot finger so that the glove cannot move any further than the robot finger. That would give you force sensing (the robot could replicate the force applied by the operator) as well as position feedback. I can imagine this working very well.

If you're using a finger sensor based on position or flex then it would be harder to make it work really well, but as long as the servo position was controlled so that it stayed clear of the finger until the robot's finger stopped moving, it could be made to work. I guess it depends what you're trying to achieve - it would be enough for gross motor control but hard to achieve the fine control needed for delicate work.

The advantage of all these approaches over a simple brake is that the fingers can be allowed to continue to move if they're gripping something resilient.

Hi, I ended up here becouse I'm trying to build something like this.
But my idea is to use a motor instead of a servo.

Does anyone know a kind of electric motor that can be manually stopped without being harmed? much like the force feedback motor inside a racing simulator weel.

Thanks! and gook luck with that globe :wink:

Yes i was also thinking about using a dc motor to do this instead, this part of my project is put on hold for now :slight_smile: