I am new to Arduino and Electronics and i need some help with my coding or to be honest need a code i can alter and play with.
I have a servo which i have managed to hack (i watched a youtube video) by removing the 'Pot' and soldering on resisters instead, i also removed the gear pin thingy that stops the gear moving past 180?. I have managed to get my servo to move via the Arduino in only one direction but continuously.
What i need it to do is to move in one direction for a specified amount of time, then stop, and then move backwards to the original position (preferably through timing). I would also like to control how fast it moves through its journey.
What you describe is usually called a "Continuous Rotation Servo".
You control it like a hobby servo but since the feedback pot has been disabled it always thinks it is stuck in the center position (near 90). If you tell it to move to 85 it will move slowly in one direction. If you tell it to move to 0 it will move quickly in the same direction. If you tell it to move to 95 it will move slowlt in the other direction.
The stop point may not be 90, exactly. There may not even be an exact stop point.
Unless you have a sensor that will tell you how much the wheel is turning you will only have rough control over how far you travel and how accurately you get back to the starting point.
You can get better control using writeMicroseconds() (1000 = 0°, 1500 = 90°, 2000 = 180°).
Zoomkat regularly posts a simple sketch that'll help you find the stop-point.
Have a search around the forum.
(maybe this should be a sticky in this section?)
Below is the dual input servo test code. With testing it is easy to see that the writeMicroseconds is generally prefered for finer control with the continous rotation servos.
// 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
}
}