Making the a servo move faster + constant humming noise.

Hey guys

I was wondering if anyone could tell me what I’m doing wrong.

I have two issues regading the code below I’m currently using to operate my MG955 servo, with Arduino Uno.

First of all, I would like to know if it would be possible to make the servo reach the selected position faster than it does at the moment. I’m using it for a 3D printed Iron man helmet, and want the face plate to open and close a bit faster.

Second of all, I seem to be able to hear the servo constantly “humming” like it’s constantly running. How can I get rid of this? :o

I’m using the following code:

#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

void setup() {
  pinMode(10, INPUT_PULLUP);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

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

Thank you in adcance.

Why not just do a servo.write(finalDestination) instead of going there a degree at a time with a delay() between each step?

kenwood120s: Why not just do a servo.write(finalDestination) instead of going there a degree at a time with a delay() between each step?

So remove the delay lines?

So remove the delay lines?

NO! Do not have it move in one degree increments at all. Remove the whole for loop.

Second of all, I seem to be able to hear the servo constantly "humming" like it's constantly running. How can I get rid of this?

It is a result of the servo being asked to go to a place it can't, so it hunts back and forth.

So basically all you can do is to get a better servo, the digital type do not suffer from this I believe.

Remove the whole "for" thing, just send it where it's supposed to go with one servo.write()

So like this?

#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

void setup() {
  pinMode(10, INPUT_PULLUP);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  while(HIGH == digitalRead(10));
  Servo.write(0); // Set Servo 1 to 0 degrees

  }
  while(LOW == digitalRead(10));
  Servo.write(180); // Set Servo 1 to 0 degrees
  }
}

Cown:
So like this?

Yes

Okay, thanks alot guys, will test it out asap!

That Sweep example sketch has a lot to answer for.

Steve

slipstick: That Sweep example sketch has a lot to answer for.

No more than does Blink

Second of all, I seem to be able to hear the servo constantly "humming" like it's constantly running. How can I get rid of this?

Does it hold position when the servo powered-off? If not, maybe you can add a little friction or a magnet or some kind of catch that holds the face plate in place, but something the motor can overcome.

If there are only two stop-positions (open & closed), a servo may not be the best solution. A geared-down DC motor with hard-stops or limit-switches/sensors may work better. (But, you'd still probably like to power-down the motor once you're in-position.)

DVDdoug: Does it hold position when the servo powered-off? If not, maybe you can add a little friction or a magnet or some kind of catch that holds the face plate in place, but something the motor can overcome.

If there are only two stop-positions (open & closed), a servo may not be the best solution. A geared-down DC motor with hard-stops or limit-switches/sensors may work better. (But, you'd still probably like to power-down the motor once you're in-position.)

As far as I can tell yes, maybe if i try to alter the degrees a but it will stop?

-

So I've tried uploading the above code, but get a weird error, but can't seem to understand what's wrong:

C:\Users****\AppData\Local\Temp\arduino_modified_sketch_743516\sketch_sep15a.ino: In function 'void loop()':

sketch_sep15a:15: error: expected unqualified-id before '.' token

Servo.write(0); // Set Servo 1 to 0 degrees

^

C:\Users****\AppData\Local\Temp\arduino_modified_sketch_743516\sketch_sep15a.ino: At global scope:

sketch_sep15a:18: error: expected unqualified-id before 'while'

while(LOW == digitalRead(10));

^

sketch_sep15a:19: error: expected unqualified-id before '.' token

Servo.write(180); // Set Servo 1 to 0 degrees

^

sketch_sep15a:20: error: expected declaration before '}' token

}

^

exit status 1 expected unqualified-id before '.' token

Anyone know what's wrong?

Errr...didn't you call your servo "myservo"? I'd try writing to that.

And have another look at what a while loop is supposed to look like. That's if you're sure you really want to keep on writing the same value to your servo over and over and over again.

Steve

The whining noise if that is what your referring to can be stopped by using 'servo.detach()' once it has reached a position but again use 'myservo.attach(9)'

anishkgt:
The whining noise if that is what your referring to can be stopped by using ‘servo.detach()’ once it has reached a position

As long as the load on the servo doesn’t then move it out of position. A servo.attach()-ed servo holds its position because it is continually pulsed to remind it.

See servo.h:

#define REFRESH_INTERVAL    20000     // minumim time to refresh servos in microseconds

slipstick:
Errr…didn’t you call your servo “myservo”? I’d try writing to that.

And have another look at what a while loop is supposed to look like. That’s if you’re sure you really want to keep on writing the same value to your servo over and over and over again.

Steve

That unfortunatly, did not work, same error message :-/

anishkgt:
The whining noise if that is what your referring to can be stopped by using ‘servo.detach()’ once it has reached a position but again use ‘myservo.attach(9)

I will try this, thanks :slight_smile:

That unfortunatly, did not work, same error message :-/

So post your code again this time with the modifications you made so we can check you did it right.

Grumpy_Mike:
So post your code again this time with the modifications you made so we can check you did it right.

I altered it like this:

#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

void setup() {
  pinMode(10, INPUT_PULLUP);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  while(HIGH == digitalRead(10));
  myservo.write(0); // Set Servo 1 to 0 degrees

  }
  while(LOW == digitalRead(10));
  myservo.write(180); // Set Servo 1 to 0 degrees
  }
}

You're closing loop() prematurely with this } marked XXX below:

void loop() {
  while(HIGH == digitalRead(10));
  myservo.write(0); // Set Servo 1 to 0 degrees

  } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
  while(LOW == digitalRead(10));

Hi, You need to look at how to structure your while statement.

https://www.arduino.cc/en/Reference/While

Thanks.. Tom.. :)