Go Down

Topic: Varying Servo Rotation Limit (Read 1 time) previous topic - next topic

kelvin8

Thanks that seems like it might work but how would you be able to increase or decrease the servo limits using the potentiometer. I am referring to the code below:

Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int pos = 90; // servo start position between up and dn
int dn = 40; // servo lower limit
int up = 140; // servo upper limit

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


void loop()
{
  for(pos > dn; pos < up; pos += 5)  // dn and up set servo limits
  {                                   
    Serial.println(pos);  // for serial monitor viewing
    myservo.write(pos);   // tell servo to go to position in variable 'pos'
    delay(150);           // waits 150ms for the servo to reach the position
  }
  for(pos < up; pos >dn; pos-=5)     // dn and up set servo limits
  {                               
    Serial.println(pos);
    myservo.write(pos);     // tell servo to go to position in variable 'pos'
    delay(150);             // waits 150ms for the servo to reach the position
  }
}
/code]


kelvin8

Hi Everyone, I tried rewriting a new code for this same problem but have not been able to test it yet. It will be really helpful if someone could look through it and tell me if they think its correct.

Code: [Select]
#include <Servo.h>
Servo myservo;

int potpin1 = 0;            //analog input pin A0
int potpin2 = 1;            //analog input pin A1

int minPos;                  //variable to store servo minimum position
int maxPos;                  //variable to store servo maximum position
int pos1 = 0;

void setup()
{
 Serial.begin(9600);
 myservo.attach(9);
 Serial.println("testing position variable servo");
}

void loop()
{
 minPos = map(analogRead(potpin1), 0, 1023, 0, 90);
 maxPos = map(analogRead(potpin2), 0, 1023, 90, 180);
 pos1 = minPos;
 
 for(pos1 >= minPos; pos1 < maxPos; pos1 += 1)    //limit from 90 degrees
 {
   Serial.println(pos1);
   myservo.write(pos1);
   delay(15);
 }
 pos1 = maxPos;
 for(pos1 <= maxPos; pos1 > minPos; pos1 -= 1)     //limit from 180 degrees
 {
   Serial.println(pos1);
   myservo.write(pos1);
   delay(15);
 }
}
/code]






kelvin8

Update: If this doesn't work i had another idea i was thinking about but not completely sure how to go about it.

The servo starts sweeping from 0 to 180 degrees
The user puts on a switch on and it changes to 20 to 160 degrees and when it's off it goes back to the original sweep
The user puts on another switch and it changes to 40 to 140 degrees and it does the same as above.

Basically the switches increase or decrease the limits for rotation.

These are the angles i want to use:

0 to 180 degrees (original sweep)
20 to 160 degrees (switch 1)
40 to 140 degrees (switch 2)
60 to 120 degrees (switch 3)
80 to 100 degrees (switch 4)

so maybe an if statement starts a new sweep function. Thanks

PaulS

Code: [Select]
  for(pos1 >= minPos; pos1 < maxPos; pos1 += 1)    //limit from 90 degrees
The first clause of a for statement is an initialization section. I can't see that pos1 >= minPos makes a good initialization statement. The result of the operator is true or false.


enanthate

So, use the buttons to change maxPos and minPos.
You are allowed to use your head! You know enough to figure this out on your own ;)
Trust yourself.

Go Up