I'm new to Arduino. Got a vts 05B today and don't know how to control it. I tried out the sweep example but once the motor arm turns through 180 degrees, it doesn't return to its original position. I've tried changing the position limits in the example.
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 5; 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(100); // waits 15ms for the servo to reach the position
}
for(pos = 5; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
}
Simple servo test code for use with standard hobby servos.
// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 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-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
myservo.writeMicroseconds(n); // for microseconds
//myservo.write(n); //for degees 0-180
readString="";
}
}
it looks like the sketch above relies on a delay to determine the end of the serial message. The tester below checks for a character that is not a digit to terminate the message.
You can set the Serial Monitor line ending dropdown to NewLine to automatically terminate a message with a NewLine character when the send button is pressed.
// Serial Servo Tester
// Use the Serial Monitor to write a value from 0 to 180
// Set Serial Monitor line ending to Newline
// for writeMicroseconds, use values from 544 to 2400
#include <Servo.h>
Servo myservo; // create servo object to control a servo
String inString;
void setup()
{
Serial.begin(9600);
myservo.attach(9); //the pin for the servo control
Serial.println("Enter angle between 0 and 180 (values from 544 written as Microseconds)");
}
void loop() {
while (Serial.available()> 0) {
int inChar = Serial.read();
if (isDigit(inChar))
{
// if the incoming character is a digit, add it to the string
inString += (char)inChar;
}
else
{
// here on the first character that is not a digit
int value = inString.toInt();
if(value >= 544)
{
Serial.print("writing Microseconds: ");
Serial.println(value);
myservo.writeMicroseconds(value);
}
else
{
Serial.print("writing Angle: ");
Serial.println(value);
myservo.write(value);
}
// clear the string for new input:
inString = "";
}
}
}
This version will compile on earlier Arduino releases
// Serial Servo Tester
// Use the Serial Monitor to write a value from 0 to 180
// Set Serial Monitor line ending to Newline
// for writeMicroseconds, use values from 544 to 2400
#include <Servo.h>
Servo myservo; // create servo object to control a servo
String inString;
void setup()
{
Serial.begin(9600);
myservo.attach(9); //the pin for the servo control
Serial.println("Enter angle between 0 and 180 (values from 544 written as Microseconds)");
}
void loop() {
while (Serial.available()> 0) {
int inChar = Serial.read();
if (inChar >= '0' && inChar <= '9')
{
// if the incoming character is a digit, add it to the string
inString += (char)inChar;
}
else
{
// here on the first character that is not a digit
int value = inString.toInt();
if(value >= 544)
{
Serial.print("writeing Microseconds: ");
Serial.println(value);
myservo.writeMicroseconds(value);
}
else
{
Serial.print("writing Angle: ");
Serial.println(value);
myservo.write(value);
}
// clear the string for new input:
inString = "";
}
}
}
Doesn't the "Servo.write" method already do this for you?
Yes, values equal to or greater than 544 are handled by Servo.write as microseconds. However the code as posted does the same thing and is preferred because its function is clearer.