1

I can't get my head around this: when I turn the servo on it just spins on its own as if it's setting up, it goes to an angle superior to the one I want it to go. (Don't mind if it has to set up but it can't spin more than 20 degrees which is currently doing).

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards    
int pos = 0;    // variable to store the servo position

int degrees_of_motion = 20;
int servo_speed = 88;

void setup() {
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  myservo.write(0);
}

void loop() {
  for (pos = 0; pos <= degrees_of_motion; 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(servo_speed); // waits 15ms for the servo to reach the position
  }
  for (pos = degrees_of_motion; pos >= 0; pos -= 1) {
    // goes from 180 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(servo_speed);// waits 15ms for the servo to reach the position
  }
}

enter image description here

enter image description here

  • Humm, all the methods in object myservo are available after you instantiate it. So try initializing before you attach the servo (swap lines in your setup() method). Also be careful not to write a negative number to the servo. I don't think you are doing it. But just keep it in mind as the driver might interpret it as a really large number. – st2000 Feb 2 '18 at 15:13
  • st2000 Do you mean swapping myservo.attach(3); myservo.write(0). It doesn't make a difference also tried to put int after void setup. Was this what you meant? – Francesco Gentili Feb 2 '18 at 15:23
  • If you remove the positioning code in the loop() function so that the setup() does an attach followed by positioning the servo to 0 degrees, what happens? Also what if instead of the for loops in the loop() function you instead have a global variable for position which you set to zero degrees in setup() function and then have the logic in the loop() function just increment or decrement by 1 each time loop() is called? – Richard Chambers Feb 2 '18 at 15:52
  • If I remove the loop() function so that the setup() does an attach followed by positioning the servo to 0 degrees: Every time I turn it on it will spin to an unidentified angle really fast and come back to 0 and stop. Also global variable I am really new to Arduino not really sure what you mean. Thank you very much – Francesco Gentili Feb 2 '18 at 16:03
  • the question title implies that the motor moves before you run the sketch. it that what happens? – jsotola Feb 2 '18 at 17:21
1

What happens if you have a sketch such as the following? This uses the loop() function of the Arduino sketch to do one movement each time the loop() function is triggered.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // twelve servo objects can be created on most boards    

int pos = 0;   // variable to store the servo position
int direc = 1; // direction and amount of position iteration, + to increment, - to decrement

int degrees_of_motion = 20;
int servo_speed = 88;

void setup() {
  myservo.attach(3);  // attaches the servo on pin 3 to the servo object
  pos = 0;            // start off with zero degrees.
  direc = 1;          // start off incrementing the angle by 1
}

void loop() {
  if (direc > 0 && pos > degrees_of_motion) {
    direc *= -1;       // change the direction of iteration, keep amount
    pos = degrees_of_motion;
  }
  if (direc < 0 && pos < - degrees_of_motion) {
    direc *= -1;       // change the direction of iteration, keep amount
    pos = - degrees_of_motion;
  }

  myservo.write(pos);    // tell servo to go to position in variable 'pos'
  pos += direc;          // increment/decrement the position
  delay(servo_speed);    // waits 15ms for the servo to reach the position
}
  • Thank you but there is no difference with this sketch – Francesco Gentili Feb 9 '18 at 13:18

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.