Hi! First post ever.
I'm testing out a concept and there's this itch I need to scratch.
I tought I could make it easy the first time. The Servo (Tower Pro Sg90, 180°, if it's relevant) just needs to rotate clock or counterclockwise if certain conditions are met.
Don't know if I should write it like actual code or I could just write a generic algorithm. My question is specifically about how to do a bit of the code, highlighted below, so I'm trying to be brief.
void loop() {
if (Sensor0input>Sensor1input) {
Just Rotate the damn Servo Clockwise;
}
else if (Sensor0input<Sensor1input) {
Just rotate the damn Servo COUNTERclockwise;
{
else {
do nothing;
}
}
I have to include some calibration variables to the sensor inputs and some tolerance to the comparisons, else the servo will get twitchy
I couldn't figure out the bold bits. Got around it by using a method hat reads and writes a step motor position, which turned out to be quite a chunky code in comparison to these 6 lines that seemed so simple
So, my question is how can I write those bits. Is it even doable?.
Yes, I know it can pulse the servo beyond the 180° range. But I can just gear it to extend the range. I actually need those 180º.
Oh, I also devised a method that uses a potentiometer to read the servo position. Many ways to skin a cat. But I'm going to take this project industrial scale and hopefully cut off the microcontroller. Don't know how to do that yet, but my colleagues are more knowledgeable than me in electronics.
Just Rotate the damn Servo Clockwise;
theServo.write(whatever_angle_you_want_it_to_go_to);
or are you expecting it to keep rotating ?
Servos don't know anything about clockwise/counterclockwise. They're positional. They just go from where they are now to where you command them to go. So if the servo is at position 0 and you do a write(180) it goes to wherever 180 is. If it's already at 180 it doesn't move. If you can't work on that basis then servos aren't for you.
BTW many/most servos have physical end stops so whatever signal you send you won't get them to go past their intended range.
Steve
I do, actually. Until the conditions (Sensor0input==Sensor1input) are met
Think I already started out with the wrong foot and should do a Do-while loop?
I would still need the board to just tell the Servo to rotate in a way. I would need to read the servo position to use Servo.write.
And you just gave me the idea to simply do some math using the difference between the Sensors to write the servo position straight away.
But I would still would like to know if there's a way to do my original idea. Could come in handy in the future.
Edit: I think Steve just answered my question.
So, it can't be done then?
Well, the Math way it is, then.
If you need the servo to rotate within a prescribed range, it's plausible. There are sail winch servos that can rotate 720 degrees if you need it.
If you always need to be able to turn more clockwise if your inputs demand it, a true servo will not do. The fact that you were able to solve this with a stepper suggests that you're in this situation.
There are continuous servos, which are really not servos at all and can rotate CW or CCW forever. They have no idea of any position info though.
Below is some simple servo test code from back in the day. You can read the servo position by tapping the voltage on the wiper in the servo. But if the servo is not where it was commanded to go, you probably have mechanical or other issues.
// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
while (Serial.available()) {
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(3);
}
}
if (readString.length() >0) {
Serial.println(readString);
int n = readString.toInt();
Serial.println(n);
myservo.writeMicroseconds(n);
//myservo.write(n);
readString="";
}
}
Just use a DC motor. Then there is no confusion about shaft position, just which direction of rotation is to be called "clockwise" (it depends on your point of view).