// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#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(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
What I am confused about is the delay function. In the above code, the motor goes from 0 to 180 degrees then back to 1 degree (off topic, but why does it go back to 1 degrees and not 0?) in a total of 30ms. So the 30ms should be the period, in 1 second there should be about 1/(30*10^-3) cycles, but it definitely does not seem like it's doing that on my servo motor.
I am using this servo readyheli.com - This website is for sale! - readyheli Resources and Information..
Does anyone know what could be causing this problem?
There's also another problem in the code. Whenever the servo motor transitions from clockwise to counter clockwise, there's a brief pause, but not when there is a transition from counter clockwise to clock wise. The latter is a smooth transition. Any idea what could be the cause for this?
It could be the issue you identified earlier - if the servo won't move through the range you're instructing it to, you'll see a pause as you iterate through numbers of degrees it can't achieve. Try reducing the range through which the servo is asked to move.
There's also another problem in the code. Whenever the servo motor transitions from clockwise to counter clockwise, there's a brief pause, but not when there is a transition from counter clockwise to clock wise. The latter is a smooth transition. Any idea what could be the cause for this?
Servo test code that can be used to test the mechanical range/limits of your servo.
// 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, 500, 2500); //the pin for the servo control, and range if desired
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
}
}