Sorry, crayon MARK, so we can tell which end of the horn is which.
Did the servo turn about 90° CCW or 270° CW?
the last photo is when it started to move to the right, en the first photo is where it stops. Sorry for the uncertainty. I think that it turned around 40/50 degrees.
Save your current program, then load this test program. Enter 0 (zero) in the top of Serial Monitor and press [ENTER], servo should go to what it thinks is zero position, then enter 180. Did the servo go to 180°?
If not, where did it go?
/*
Try this test sketch with the Servo library to see how your
servo responds to different settings, type a position
(0 to 180) or if you type a number greater than 180 it will be
interpreted as microseconds(544 to 2400), in the top of serial
monitor and hit [ENTER], start at 90 (or 1472) and work your
way toward zero (544) 5 degrees (or 50 micros) at a time, then
toward 180 (2400).
*/
#include <Servo.h>
Servo servo;
void setup() {
// initialize serial:
Serial.begin(9600); //set serial monitor baud rate to match
//set line ending to "No line ending"
servo.write(90);
servo.attach(9);
prntIt();
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int pos = Serial.parseInt();
pos = constrain(pos, 0, 2400);
servo.write(pos);
prntIt();
}
}
void prntIt()
{
Serial.print(" degrees = ");
Serial.print(servo.read());
Serial.print("\t");
Serial.print("microseconds = ");
Serial.println(servo.readMicroseconds());
}
The servo did not go to 180°, but I think it went around 160°
What angle does the servo move through when you run the Servo Sweep example ?
Try another servo.
Then it also goes from 0° to around 160, but if I put in my programm that I wrote earlier, it goes from 0° to 180°.
#include <Servo.h>
// this is to create a servo object to controll the servo
Servo myservo;
// twelve servo objects can be created on most boards
// rhis is the pin where the servo is hanging on
const int SERVO = 9;
// this is the variable to store the servo's position
int pos;
// this is the standart speed for the servo microseconds
const int STANDARTSPEED = 5000;
// this is fast speed for the servo microseconds
const int FASTSPEED = 2000;
// this is slow speed for the servo in microseconds
const int SLOWSPEED = 15000;
// this is the delay between CW and CCW of the servo in ms
const int WAITTIME = 150;
void setup()
{
// this is so we can use serial monitor later on in the programm
Serial.begin(9600);
// this is to attach the servo to pin 9
// the 544 is the mimum value of the servo
// the 2400 is the maximum value of the servo
myservo.attach(SERVO, 544, 2550);
}
void loop()
{
// this is to let the servo turn 180 degrees
for (pos = 0; pos < 210; pos ++)
{
// this is to set the position of the servo
myservo.write(pos);
// this is to set the speed of the servo
delayMicroseconds(FASTSPEED);
}
// this is so that the servo stops between the CW and the CCW
delay(WAITTIME);
for (pos = 210; pos > 0; pos --)
{
// this is to set the position of the servo
myservo.write(pos);
// this is to set the speed of the servo
delayMicroseconds(FASTSPEED);
}
// this is so that the servo stops between the CW and the CCW
delay(WAITTIME);
}
Sorry for the long waiting, that is beacuse I am a new user and I reached the maximum amount of replies for today
// this is to let the servo turn 180 degrees
for (pos = 0; pos < 210; pos ++)
{
// this is to set the position of the servo
myservo.write(pos);
Why are you writing values up to 210 to the servo ?
I setted the max and min value, and 210 is exactly 180 degrees,
Dis you set the same max/min values when you tried the Sweep example ?
no, it went from 0° to 160°
#include <Servo.h>
Servo myservo;
const byte SERVO = 9;
const byte POTPIN = A0;
const byte MAXPOSITION = 210;
const byte MINPOSITION = 0;
int pos = 0;
int valPot = 0;
bool CW = false;
void setup() {
myservo.write(); // this is to set the servo to 0 degrees
myservo.attach(SERVO, MINPOSITION, MAXPOSITION);
delay(100);
}
void loop() {
valPot = (valPot + analogRead(POTPIN))/2 ;
if (CW) {
pos++;
if (pos >= MAXPOSITION) {
CW = false;
pos = MAXPOSITION;
}
} else {
pos--;
if (pos <= MINPOSITION) {
CW = true;
pos = MINPOSITION;
}
}
myservo.write(pos);
delayMicroseconds(valPot*10);
}
Now it goes from 70° I think to 160°
you ask sketch to control rotation speed.
here without delay()
#include <Servo.h>
Servo myservo;
const byte SERVO = 9;
const byte POTPIN = A0;
const byte MAXPOSITION = 180;
const byte MINPOSITION = 0;
int pos = 0;
int valPot = 0;
bool CW = false;
uint32_t Pause = 0;
void setup() {
myservo.write(0); // this is to set the servo to 0 degrees
myservo.attach(SERVO, MINPOSITION, MAXPOSITION);
delay(100);
}
void loop() {
valPot = (valPot + analogRead(POTPIN)) / 2 ;
if (micros() - Pause >= (valPot * 10 + 1000)) {
Pause = micros();
if (CW) {
pos++;
if (pos >= MAXPOSITION) {
CW = false;
pos = MAXPOSITION;
}
} else {
pos--;
if (pos <= MINPOSITION) {
CW = true;
pos = MINPOSITION;
}
}
myservo.write(pos);
}
}
i have sg90 and fs90r servos, 90° and 360°. both works different.
Minor point. I think the code in Servo.h can interpret the number sent in one of two ways.
A number from 0 -> 180 will be interpreted as an angle.
A number above some threshold (not sure what value, don't have the file here), is interpreted as microseconds, and IIRC ranges from 500 to (?2500).
So, your 30 and 210 actually takes one end beyond range, and is probably being limited at 180, or misinterpreted in some way.
Hope that helps. I suggest you open up servo.h and find out what the limits really are, they're documented in there - and maybe, in the sweep example as well, not sure where I came across it.
C
Found it. The following are snippets from servo.h:
attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
default min is 544, max is 2400
write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
writeMicroseconds() - Sets the servo pulse width in microseconds
read() - Gets the last written servo pulse width as an angle between 0 and 180.
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
Despite what the comments say, the code actually does this
//for now, only accept angles, and angles that are between 0 and 200 degrees
if( value > 180 )
{
//treat this number as microseconds
writeMicroseconds( value );
}
else
{
//treat this number as degrees
uint16_t servoPulseLengthInUs = map(value, 0, 180, min, max);
writeMicroseconds( servoPulseLengthInUs );
}