I am extremely confused on how to get a servo to move the way that I want it to. I know that by default 90 is the rest position of the servo. Everything I have read indicates that you just need to put the angle you want the servo to go through and it will do it. I cant seem to support that with my experiments though. I tried putting 45 and a huge delay in the loop (like 10s). I thought that it would slowly go to position 45 and then stop because of the delay but it didnt it just kept moving the whole time as though it had completely skipped over the delay. I dont understand the example Servo Sweep function with the for loop. Does the servo. write accept an angle and moves through that angle or does it receive an angle and rotate until it reaches that position? Then regarding the delay, what does this control, if I have a huge delay will it move through or too the same angle slower??? As you can tell im really confused. I have put my code below. I want the servo to go from 90 to 180 and then back from 180 to 90 but it just moves in the same direction constantly. Its annoying.
Yes that sketch will go back and forth
The basic code just to put it some place would be like
Edited from the Library
the 50 is the placement number
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(A5); // attaches the servo on pin Analog 5
}
void loop()
{
{
myservo.write(50); // tell servo to go a s spot
delay(15); // waits 15ms for the servo to reach the position
}
}
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// 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.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
}
that one wil run it from Middle (90 Deg) to End (179 deg )
i uploaded it to mine _ love having a breadboard in front of me _ Ow , is yours a continuous servo ?
Never mind i reread your post
I want the servo to go from 90 to 180 and then back from 180 to 90 but it just moves in the same direction constantly. Its annoying.
Sounds like you have a continous rotation servo, which cannot be specifically sent to an angle position. Perhaps you can supply more info on the servo you have.
As I said a continuous rotation servo is an ex-servo, it cannot be commanded to go to a certain position and stop. Instead it is capable of rotating in one or the other direction at a variable speed or to stop. It becomes just a variable speed, bidirectional, geared motor drive. And while it is controlled using the servo library commands it operates much different then a standard non-modified servo. Suppliers do buyers a big disservice by calling them servos and you are not the first to buy one by accident thinking you were getting a servo with some additional feature, rather then getting something that once was a servo, but has had a electronic lobotomy to convert it to a variable speed/direction geared motor.