Help with servo (first time use)

Hello
this is the first time I use the servo and I think I miss some understanding
I want the servo to move right or left according to my compare

for example
int EnterNumber=200
int CompareNumber=100
if (EnterNumber<CompareNumber)
{
//servo go right for 1 sec
}
else if (EnterNumber=>CompareNumber)
{
//servo go left for 0.5 Sec
}

Thanks ,

Personally, I don't think it makes sense to move a servo for a specific length of time: rather move it by a certain number of degrees, or to a certain position. The distance moved in a certain time depends on the load; most servo apps are to do witih position, like moving a linkage to unlock a door for example.

When your sketch starts, centre the servo to 90 degrees; then move it "left" to 45 degrees say, or "right" to 135 or whatever.

Edit: Look at the servo sweep example for the coding.....

I see
so let me try again what I want to do
I have an Axis Magnetometer that show me the degree\azimuth
now I want to to enter a number 0-360 'then make the servo move until he get to this azimuth (the Axis Magnetometer will sit on the servo )

let say the Axis Magnetometer give me 100 degree
now I'm tell him to move to 80 degree
what do I need to write to move it 20 degree right ?

hope now my question is more understood

Thanks ,

If you know the destination (100) then you don't even need to know where it is right now.... it can be at 60 or 80 or 120, you just give the command:

myservo.write(100);

and it'll go there from anywhere.

Wait a mo - is this a continuous rotation ex-servo?

Servo: Servo (radio control) - Wikipedia

"Continuous rotation servo": Servo (radio control) - Wikipedia

In fact continuous rotation servos aren't normally servos at all, in
that there is no closed-loop control of speed (which is what it implies).

this is what I have
Sail Winch Servo (can go up to 720 degrees)
I have bought it because I want to be able to use to turn 360 .
now , maybe I need to understand it first - so :
how do I tell him to go to his 0 position? or when I power it up ,the last position is the 0 position until next reset?
and then how do I tell him to rotate only 90 degrees?
do I need to tell him right or left? how do I do this ?

maybe after I will do this (for testing and learning ) I will be able to create a more complicated

Thanks ,

Below is some servo test code you can use to see how your servo works. In your particular setup, you might be able to use a continuous rotation servo with the Magnetometer providing position feedback.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to 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 all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

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 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n); 
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

Is david1234 the same as david_david? Is this two separate members asking two different yet related questions?

I have open a new user , because I forgot the pass .
but lets go back to the code:
I don't understand how it work
when I press 1000 - it suppose to move for 1 second , no?
but it moved only for ~ 5 .

I don't understand why
also when I press 90 - it suppose to move only 90 degrees ,no? but it make 3 full circle , why?

A little thought is required :slight_smile:

The normal servo instructions move a servo from 0 to 180 deg.

The same instructions move a sail winch servo from 0 to 720 deg. In other words it moves the servo 720/180 = 4 times as far. So 90 for a normal servo will be 4x90 or 360 for a sail winch servo.

All servos are slightly different so my numbers may not be exact, but they will be close.

...R

something is still doesn't add up ,and I don't understand
you are telling me that in my servo when I tell him 90 - it will move 90*4=360 degrees (full circle ),right?
well it doesn't ,after every reset the number of circles change (some time 5 ,5.5 ,4 )
and then when I press 6 - it move to 90 degrees right ,so if I tell him 12 it should move 180 degrees ,no ?
from some reason it make 2.5 circles

maybe something is wrong in the calibration of it ?
is there no way to calibrate it to work 0-360 degrees only?

this is what I have now
in this code I have manage to calibrate it to work a full circle when I enter to it values from 1 to 13 (the delay is so I can see where is stop) .
but now I have an error of ~30 degrees ,it's like I have only 12 steps for the all way round
how do I make it to work on more steps ? I need ~120 steps (that way I will have only an error of ~3 degree)
I have try to use float and not int - but the servo only move when I get a int number

#include <Servo.h>

Servo myservo;  // create servo object to control a servo    
float pos = 0;    // variable to store the servo position

void setup()
{
  Serial.begin(9600);
  //  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.println("Start......");
  delay(2000);
  Serial.println("Done");
}


void loop()
{
  for(pos; pos < 14; pos +=.5)  // 
  {                                                            
    delay (500);
    Serial.print( "pos is - ");
    Serial.println(pos);
    myservo.attach(9);
    myservo.write(pos);
    delay(3000);               
    myservo.detach() ; 
    }

  }

thanks ,

Sorry, but I find your description a bit confusing.

Try this simple code and see what happens - does it do the same thing every time you reset the Arduino?

#include <Servo.h>

Servo myservo; 
byte servoPin = 9

void setup() {
   myservo.attach(servoPin);

   myservo.write(0);
   delay(1000);
   myservo.write(45);
   delay(1000);
   myservo.write(90);
   delay(1000);
   myservo.write(135);
   delay(1000);
   myservo.write(180);

}

void loop() {

}

...R

it goes 2 round to the left then on a loop to the right
doesn't stop

david1234:
it goes 2 round to the left then on a loop to the right
doesn't stop

I don't really know what precisely this means.

I expect the servo to start by going all the way to one end (not sure whether this will be clockwise or anti-clock).
Then after a delay of 1 second it should move by a half revolution at a time (with one second delays) all the way in the other direction.

Is that what it does?
Does it do exactly the same thing every time?

If so does it help to explain how to control your servo?

...R

not at all......

after a long day I have mange to do this code:

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

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 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(9); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number
    n=map(n,0,360,0,14);

    Serial.print("writing Angle: ");
   Serial.println(n);
    myservo.write(n); 
    delay (1000);


bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

in this code - when I enter 360 , the servo make full circle , but I only have 14 steps to play with -- only have 30 degrees change
I want to be able to have 10 degrees range

david1234:
not at all......

I don't know what that refers to.

You didn't answer my questions from Reply #14.

There seems little point writing complex code until the simple code works properly.

...R

=map(n,0,360,0,14);

14 steps, you say?

Robin2

this is the code I have now (what you have post only added serial output)

#include <Servo.h>
#include <SoftwareSerial.h>
Servo myservo; 
int servoPin = 9;

void setup() {
  Serial.begin(9600);
   myservo.attach(servoPin);

   myservo.write(0);
   Serial.println("0");
   delay(1000);
   myservo.write(45);
   Serial.println("45");
   delay(1000);
   myservo.write(90);
   Serial.println("90");
   delay(1000);
   myservo.write(135);
   Serial.println("135");
   delay(1000);
   myservo.write(180);
   Serial.println("180");

}

void loop() {

}

and it doesn't do what you said it will at all .
when it start to work - he is working for 5 minutes now(and counting) - turning to the same side
like he is in some kind of loop or something

david1234:
and it doesn't do what you said it will at all .
when it start to work - he is working for 5 minutes now(and counting) - turning to the same side
like he is in some kind of loop or something

It is entirely possble there is a stupid mistake in my code - it happens all the time.

But you need to give me a much more detailed description of what is happening if I am to find the problem.

I can't imagine that you mean it started turning at time 0 and it is still moving, without ever having stopped, 5 minutes later.

Please describe exactly how you use the program, action by action, together with the behaviour of your servo. Don't omit any detail.

Remember I can't see what you can see and I don't have a servo like yours.

Edit to add - I have just tested YOUR version of my code with a regular servo and it works just as I expect - moving 45degrees every second. In your case I expect the movements to be 45x4 or 180 degrees at each step.

I notice you have a reference to SoftwareSerial although it is not used in the code. Servos do not work with SoftWareSerial, so delete it.

...R