redirecting a servo with an IF

Hello so i am new to arduino and have written some code to control a servo for my robotic hand/ arm. My problem is they tend to move past their safe range and draw to much current. So i have written a simple code to test my fix i am trying to write an "if" statement that will make the servo move back to a safe range if it moves to far, my problem so far is that i have written a solid code to control the servo with a pot but as soon as i hit my trigger level to try to move the servo back to a safe range it goes crazy making the servo do a 360 and the serial monitor goes all haywire
#include <Servo.h> //load the servo library
int pos; //declare and initilise the pos variable
int servo0Pin=11; //tell arduino servo is hooked to pin11
int potReading; //
int potPin0 = A0; //
int servoDelay=500; //
int readValue; //
int threshold = 50;
Servo servo0;
void setup() {
Serial.begin (9600);
servo0.attach (servo0Pin);

}

void loop() {
potReading = analogRead(potPin0);
pos = map(potReading,0,1023,0,180);
Serial.print(potReading);
if(potReading > threshold){
digitalWrite(potPin0, 90);
servoDelay;
Serial.print(" ");
Serial.println(pos);
servo0.write(pos);
}
}

Your description and your code don't match. The code will not write ANYTHING to the servo unless potReading is greater than threshold (a very low value) and then it always does a write(90) immediately followed by a write(pos).

Note that the line "servoDelay;" does nothing because it contains only "500". Perhaps you meant delay(servoDelay)?

Steve

Yes steve i had meant delay(servoDelay). I have revised my code slightly what i am looking for here is i had 5 servos and sadly they got overdriven and it ruined the internal gears so I am looking to have a code that if my input goes below my lower limit on the servo it will redirect it back to a safe range. The trouble i am having is that no matter where i set my range it makes the servo start to go in erratic patterns(I am guessing due to having my code lines mixed up) do you have any suggestions on how to accomplish this?

 #include <Servo.h>  //load the servo library
int pos;            //declare and initilise the pos variable
int servo0Pin=11;   //tell arduino servo is hooked to pin11
int potReading;     //
int potPin0 = A0;    //
int servoDelay=500; //
int readValue;      //
int threshold = 500;
Servo servo0;
void setup() {
  Serial.begin (9600);
  servo0.attach (servo0Pin);
   
}

void loop() {
  potReading = analogRead(potPin0);
  pos = map(potReading,0,1023,600,2300);
  Serial.print(potReading);
  servo0.write(pos);
  if(potReading > threshold){
  digitalWrite(potPin0, 1500);
  delay(servoDelay);
  Serial.print("     ");
  Serial.println(pos);

Sorry I'm completely confused over what this code is trying to do. Your if statement checks that the input value is OVER threshold not under. Then you do

  digitalWrite(potPin0, 1500);

Why are you doing a digitalWrite() to your analog input pin? What is that supposed to achieve?

As far as I can see you never write anything to a servo.

If what you're worried about is the value you're writing to a servo going outside some safe range wouldn't it be easier just to set the acceptable range in the map statement.

BTW if you're going to write microseconds to the servos rather than angles then you really should use servo.writeMicroseconds() not servo.write() which expects 0-180 as an angle.

Steve

slipstick:
then you really should use servo.writeMicroseconds() not servo.write() which expects 0-180 as an angle.

unless you pass it a larger value, then it assumes you meant writeMicroseconds and uses that instead.
I dislike this 'feature' very much.

@twilli24, if you know what range you want to limit the servo to, you can set those limits in the servo.attach().
Syntax
servo.attach(pin)
servo.attach(pin, min, max)
The second syntax lets you set the range. Any values less than min use min instead. Any values greater than max use max instead.
I have used the knob tutorial to test the range of servo physical limits. I slowly move the potentiometer until I can hear the servo bind. I note the value being written to the serial monitor and then move the pot back so I do not burn up the servo.
Then I modified the servo.attach() to use the min and max values from my tests. Actually, I used values just inside of the range that made the servo bind.

vinceherman:
unless you pass it a larger value, then it assumes you meant writeMicroseconds and uses that instead.
I dislike this 'feature' very much.

Likewise, which is why I said should use writeMicroseconds(). It's much cleaner to say what you mean.

Steve

Thank you vinceherman that is very helpful. I was trying to make sure i don't ruin anymore servos, I had broken 5 of them since they were not in a limited range and they went well beyond what they were capable and broke my gears and my board was fried on a couple of them as well. Slipstick I understand your point i have run it both ways wile working with my electrical engineering professor i was not fully aware of how it knew the difference but he seemed to know and i just took his lead . I do agree it would make it clearer if i state it in microseconds rather then degrees . Thank you guys for your help I feel comfortable with my new code to hook up my new servos.