0

I am interested in writing a simple Arduino program that will turn a servo back and forth (180 degrees each time) continuously.

I am looking at these servos and am planning on following this example.

Being so new to Arduino, I first wanted to confirm that the SoftwareServo library in that example will drive those particular servos. If it will not, then can someone begin by explaining to me why these servos are incompatible with the lib?

Assuming they are compatible, I am looking at the main example on that page (comments stripped out for brevity):

#include <SoftwareServo.h> 

SoftwareServo myservo;

int potpin = 0;
int val;

void setup() { 
    myservo.attach(2);
} 

void loop() { 
    val = analogRead(potpin);
    val = map(val, 0, 1023, 0, 179);
    myservo.write(val);
    delay(15);

    SoftwareServo::refresh();
} 

My concerns:

  • There seem to be 3 pins involved here: a potentiometer pin (0), an analog pin (val) and a servo pin (2). What is the purpose of each of these pins, and is this 3-pin wiring common across all/most servos?
  • Is there anything "wrong" with defining myservo.attach(2); up above the setup function, and leave the setup function an empty no-op function?
  • Where does the map(...) function come from? If it was defined inside SoftwareServo I would have expected its usage to be something like SoftwareServo::map(...), etc.
  • Why do we need the delay after calling myservo.write(val);? What would happen if this delay wasn't in there?
  • According to that link, calling SoftwareServo::refresh() once every 50ms is necessary in order to: "keep your servos updating." But what does this mean, really? Updating to what?!?
1

There seem to be 3 pins involved here: a potentiometer pin (0), an analog pin (val) and a servo pin (2). What is the purpose of each of these pins, and is this 3-pin wiring common across all/most servos?

No, there is only 2 pins. The value of the potentiometer attached to pin 0 is read and stored in the variable val. That is then used to set the angle of the servo motor on pin 2.

Is there anything "wrong" with defining myservo.attach(2); up above the setup function, and leave the setup function an empty no-op function?

Yes. It'll never get called. setup() is the first function to be called and its purpose is to perform any initialization routines, such as attaching the servo code to the pin the servo is connected to. So you have to call that either from setup() or from a function which setup() itself calls.

Where does the map(...) function come from? If it was defined inside SoftwareServo I would have expected its usage to be something like SoftwareServo::map(...), etc.

map() is part of the standard Arduino API. http://www.arduino.cc/en/Reference/Map

Why do we need the delay after calling myservo.write(val);? What would happen if this delay wasn't in there?

You don't need the delay() call, but you don't want to set the value of the servo too rapidly. You really want to allow at least one PWM cycle between each call to ensure the values have been set properly.

According to that link, calling SoftwareServo::refresh() once every 50ms is necessary in order to: "keep your servos updating." But what does this mean, really? Updating to what?!?

SoftwareServo is just what it says - a piece of software. You have to run that software for it to work, and the refresh() function is that bit that you run. If you don't call that then the PWM signal for the servo will never get generated and nothing will happen.

Oh, and yes those little servos should be fine.

1

1- there is only two pins a servo pin which is D2 and an analog pin which is A0. D2 pin for writing analog value to servo. D2 pin is used for getting analog pin value.

2- map function is a default function like sin, cos or digitalRead so library inport not necesary.

3- delay function and waiting 50ms is used to wait servo response time. if you change servo angle too quickly servo will not respond accurately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.