Limiting continuous servos

Hey All,

Working on my first serious arduino project and I need a bunch of servos to turn for a set amount of time and then stop. I managed to write a little piece of code that works for a single servo (see below) but when I add a second servo to the code some funny things happen:

The single servo code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int count = 0;
boolean hiend = false;
boolean lowend = false;

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

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
if ((val > 625) && (hiend == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts
count++; // starts a counter going to time how long the servo is turning
lowend = false;
myservo.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count > 25) { // sets the upper limit for the servo counter
// this makes the loop stay at 25 (the stop servo point) until the direction is changed
hiend = true;
myservo.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
Serial.print("hi");
}
} else
if ((val < 425) && (lowend == false)) { // sets a point slightly below the halfway point before servo starts other direction
count--; // starts the count going down
hiend = false;
myservo.write(5);
delay(200);
if (count < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
lowend = true;
myservo.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
Serial.print("hi");
}
} else
myservo.write(90);

Serial.println(count); // just to test the count/loop is working
}

The two (hopefully 6 at the end) servo code:

#include <Servo.h>

Servo myservo1; // create servo object to control a servo

int potpin1 = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
int count1 = 0;
boolean hiend1 = false;
boolean lowend1 = false;
int ledPin = 13; // LED connected to digital pin 13

Servo myservo3; // create servo object to control a servo

int potpin3 = 5; // analog pin used to connect the potentiometer
int val3; // variable to read the value from the analog pin
int count3 = 0;
boolean otherhi = false;
boolean otherlow = false;

void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(3); // attaches the servo on pin 11 to the servo object

}

void loop()
{
val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
if ((val1 > 800) && (hiend1 == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts
count1++; // starts a counter going to time how long the servo is turning
lowend1 = false;
myservo1.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count1 > 25) { // sets the upper limit for the servo counter
// this makes the loop stay at 25 (the stop servo point) until the direction is changed
hiend1 = true;
myservo1.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
}
} else
if ((val1 < 300) && (lowend1 == false)) { // sets a point slightly below the halfway point before servo starts other direction
count1--; // starts the count going down
hiend1 = false;
myservo1.write(5);
delay(200);
if (count1 < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
lowend1 = true;
myservo1.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
}
} else
myservo1.write(90);

val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
if ((val3 > 800) && (otherhi == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts

count3++; // starts a counter going to time how long the servo is turning
otherlow = false;
myservo3.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count3 > 25) { // sets the upper limit for the servo counter

otherhi = true; // this makes the loop stay at 25 (the stop servo point) until the direction is changed
myservo3.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
}
} else
if ((val3 < 300) && (otherlow == false)) { // sets a point slightly below the halfway point before servo starts other direction
count3--; // starts the count going down
otherlow = false;
myservo3.write(5);
delay(200);
if (count3 < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
otherlow = true;
myservo3.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
}
} else
myservo3.write(90);

}

But when I run the second code its works until one of the servos (seems like always the second one) get to the end... it pauses for a second then it keeps moving in the same direction - then oddly enough it starts controlling both servos !?!?! and neither are limited... any help would be greatly appreciated.

myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
 myservo3.attach(3);  // attaches the servo on pin 11 to the servo object

The second servo is called myservo3? Will the third servo be called myservo8?

Is the servo attached to pin 3 or pin 11?

boolean hiend1 = false;
boolean lowend1 = false;
boolean otherhi = false;
boolean otherlow = false;

What do you propose to call the hi and low values for the 3rd servo? sam and bob?

Since the code for handling the servos is the same, except for the servo instance and the potentiometer pin number, why is there not a function to read the pot and move the servo?

Continuous rotation servos have had the positional feedback mechanism removed. At best, you can control the speed and direction that they move, not their absolute position.

The second servo is called myservo3? Will the third servo be called myservo8?

What do you propose to call the hi and low values for the 3rd servo? sam and bob?

Really dude - that's all you had? Are all your 2000+ posts lame trolls or do you actually contribute the other people learning?

If you must know I had 3 running originally and took one of them out to see if I could get two running and just took the first and the third one - as for the second issue I originally had it hiend1, hiend2 etc but was just seeing if the booleans were causing the issue somehow and so I made them distinctly different.

You are correct that my comment is wrong for the pin (I actually moved the pins farther apart because I was thinking it might have been a feedback issue or something having the two so close to each other), but really did pointing that out really help solve anything (seeing as a comment has no effect on the execution of the code) or just make you feel a bit better about yourself???

As for:

Continuous rotation servos have had the positional feedback mechanism removed. At best, you can control the speed and direction that they move, not their absolute position.

This doesn't even address my question - really man you're reaching a bit to far just to try and feel smarter than someone else. The whole point of this code is that if I time how long it takes to turn the servo to a certain point I can get close enough to setting a fixed beginning and end (if you read the actual content of the code you might have picked up on that).

And as for:

Since the code for handling the servos is the same, except for the servo instance and the potentiometer pin number, why is there not a function to read the pot and move the servo?

What did you think this did:
val = analogRead(potpin);
?????

FYI - The point of the boolean is to prevent any value of the pot beyond the max setting from activating the servo (again read the code and not the semantics and you may have picked up on this).

Anyways, it seemed to run last night anyways, I think my problem lies in the amount of power I'm pulling over the USB from my laptop and it's causing some funny things to happen.

If anyone has anything that really helps with this issue I'd appreciate it :wink:

I can't tell, when your code says one thing and the comments say something else, which pin you've actually plugged the servo into. I was asking for clarification.

But, since I'm simply a lame troll, there's no reason for you to respond.

The whole point of this code is that if I time how long it takes to turn the servo to a certain point I can get close enough to setting a fixed beginning and end

Well, that can depend on all sorts of things like supply voltage, which as you've probably observed, is affected by load.
It can vary from servo to servo, depeding on how it was modded.

Could you please go back to your post, click on "modify", then highlight the code blocks and then click on the "#" button on the editor toolbar?

What did you think this did:
val = analogRead(potpin);
?????

Well, it reads a value from the A/D converter, and assigns it to a value, but it really isn't a function to read a pot and move a servo, is it?

Chill, "dude", and learn.