Ok here is what im trying to do, I want to be able to send a value through the serial box and get the servo to turn a few degrees left. Then when send a different a value through the serial box I want the servo turn a few degrees right. I also want it to turn slowly and have the speed adjustable by changing the numbers in the code. Another thing I would like is to be able to specify the angle the servo is at when the code starts. I have it built but I am having a real hard time with the code. Could some one give me a hand? Thank you.
Did you get the "knob" example to work using analog input?
recall that the call for a servo is servo(angleDegree)
The servo will turn at a speed related to voltage supplied. It would require an intricate code using PWM and transistors to slow it down, I would tend to believe.
Whatever your range of numbers are, you could use map to convert to an angle. http://arduino.cc/en/Reference/Map
IE:
degrees = map(input, -1000, 1000, -90, 90);
The servo will turn at a speed related to voltage supplied. It would require an intricate code using PWM and transistors to slow it down, I would tend to believe
Bad information. Servos use PPM, which is handled by the servo library.
The servo sweep example demonstrates how to control the speed of servo rotation. Someone has also made a servo library that allows for easy servo speed control. Below is some simple servo code you can use to test your servo.
// zoomkat 10-14-11 serial servo test
// type servo position 0 to 180 in serial monitor
// 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(2000); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22"); // 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); // allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
//myservo.write(readString.toInt()); //for degees 0-180
readString=""; //empty for next input
}
}
degrees = map(input, -1000, 1000, -90, 90);
Don't try using this code directly with the Servo library.
thanks for the replies, i got the "knob" example working.
i was more thinking this kind of thing but with serial inputs:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1246742084
i added:
if( Serial.available() ) { // if data is available to read
val = Serial.read();} // read it and store it in 'val'
i tried replacing the if(digitalRead(leftPin) == LOW) part of the code with
if( val == '0' );
but have had no luck.
Have you tried Zoomkat's code?
i tried his code but the servo turned until it had reached its end and started to make a buzzing sound even before i could open the serial box
myservo.writeMicroseconds(2000); //set initial servo position if desired
Change that line to 1500, and try again
thanks, changing the line to 1500 worked, it set the servo to the 90 degrees positition. What should i be sending through the serial box?
Numbers in the range of about 1000 to 2000.
Though you said 2000 causes the servo to hit the end stops, so try a slightly lower range.
ahh i undersand now, need some code that would move the servo left or right using 2 values through the serial box. for example if i sent "1" though the serial box the servo would move say 5 degrees right and if i sent a "1" again the servo would move another 5 degrees right. How would i achieve this?
Modify Zoomkat's code, so that if the received String is 1, you write, say 1200 microseconds, and if it is 2, write, say, 1700 microseconds.
i dont know how to do that, would you be able to provide an example?
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int select = readString.toInt();
int servoPos = 1500;
switch (select) {
case 1: servoPos = 1200; break;
case 2: servoPos = 1700; break;
}
myservo.writeMicroseconds(servoPos);
readString=""; //empty for next input
}
thanks works great!