Making the a servo move faster + constant humming noise.

kenwood120s:
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));

Thank you, this fixed the code, I had one } too many :slight_smile:

But removing the delay hasn't changed the speed the servo is moving at. Is it possible that the servo just can't do it faster?

New 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(LOW == digitalRead(10));
  myservo.write(0); // Set Servo 1 to 0 degrees
 
  while(HIGH == digitalRead(10));
  myservo.write(110); // Set Servo 1 to 0 degrees
  }

Is it possible that the servo just can't do it faster?

Yes.

Do you know that:-

while(LOW == digitalRead(10));

Will stay in that loop doing nothing else until pin 10 reads HIGH?
and

while(HIGH == digitalRead(10));

Will stay in that loop doing nothing else until pin 10 reads LOW?

Just to humour us could you at least try it with the while loops using normal syntax as shown in https://www.arduino.cc/en/Reference/While

You know, with the squiggly brackets and no semicolon immediately after the condition and that sort of thing.

Steve

Interesting to see these lines written this way round:

while(HIGH == digitalRead(10))

and not the more usual:

while(digitalRead(10) == HIGH )

I suppose it will work though, just as one could write the equation of say a straight line as:

mx + c = y

although it's much more common to put the y by itself:

y = mx + c

Looks odd though.

Looks odd though

Yes it does but it could be a good habit for a beginner to use, this is because it would throw up an error if a single = is used instead of a == , a common beginner's mistake.

Grumpy_Mike:
Yes.

Do you know that:-

while(LOW == digitalRead(10));

Will stay in that loop doing nothing else until pin 10 reads HIGH?
and

while(HIGH == digitalRead(10));

Will stay in that loop doing nothing else until pin 10 reads LOW?

I use it in order to control the servo with a push button switch, so when the button is pushed down, the helmet is down and vice versa. Should I do it otherwise? I can't see how this would effect the servo speed.

Hi,
I we are pointing out the order of your while statement.

while(LOW == digitalRead(10));

Reads as While HIGH equivalent to digitlRead.

Usually its While digitalRead is equivalent to HIGH

If you read the documentation that has be pointed out to you. (Now for the third time)

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

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

is better written

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

Tom.... :slight_smile:

TomGeorge:
Hi,
I we are pointing out the order of your while statement.

while(LOW == digitalRead(10));

Reads as While HIGH equivalent to digitlRead.

Usually its While digitalRead is equivalent to HIGH

If you read the documentation that has be pointed out to you. (Now for the third time)

while - Arduino Reference

 while(HIGH == digitalRead(10));

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



is better written


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




Tom.... :)

Yes, I've tried this, but this has nothing to do with the speed of the servo, this changes nothing... I recon the servo just won't go any faster.

#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(digitalRead(10) == LOW);
  myservo.write(0); // Set Servo 1 to 0 degrees
 
  while(digitalRead(10) == HIGH);
  myservo.write(110); // Set Servo 1 to 0 degrees
  }

Hi,
How fast do you want the servo to go?
How long from 10 to 110?

What do the specs for the servo show?

The speed of your servo is limited by the gearing and power of the motor.

What is the servo moving, that you need more speed?

Tom... :slight_smile:
Sorry just re-read your first post.
How heavy is your visor?

Hi,
Your servo will be buzzing because you have signaled it to go to 10 or 110.

It is now trying to hold it there, the servo has no brake, so if the visor is trying to got down and the servo is holding it up, the servo motor will continue to buzz to apply the torque required to hold that position.

Also do you have a DMM to measure your power supply while this is happening?

What is your power supply?

A picture of your project so we can see your visor would help.

Tom.. :slight_smile:

TomGeorge:
Hi,
Your servo will be buzzing because you have signaled it to go to 10 or 110.

It is now trying to hold it there, the servo has no brake, so if the visor is trying to got down and the servo is holding it up, the servo motor will continue to buzz to apply the torque required to hold that position.

Also do you have a DMM to measure your power supply while this is happening?

What is your power supply?

A picture of your project so we can see your visor would help.

Tom.. :slight_smile:

Thanks for your replies, that cleared it up for me I think.

I would like it to go at its maximum speed (I want the faceplate to go upward and downward swiftly), but I think that what it runs at now is tops.

I've tried USB 3.0, since I originally planned it to be run by a computer motherboard, but have now powered it by a 12v 2.0a power supply. This doesn't seem to affect it.

I don't have anything I can measure the power with atm. but I can get it if need be.

The faceplate is made from 3d printed PLA, and weighs exactly 137,88g (0.30 lb).

Here's a link showing it in action:

Video

I have, today, changed the push button switch, with an LED push button instead and altered the code as seem below:

#include <Servo.h> 
 const int servoPin = 9;
 const int buttonPin = 10;
 int buttonState = 0; 
 int directionState = 0; 
 Servo myservo; 
 int pos = 0;
 
void setup() {
   myservo.attach(9);
   pinMode(buttonPin, INPUT);
 }
 
 void loop(){
   buttonState = digitalRead(buttonPin);
   if (directionState == 0){
     if (buttonState == HIGH) {
       directionState = 1;
       for(pos = 0; pos < 110; pos += 1)
       {
         myservo.write(pos);  
         delay(5);
       }
     }
 
   } else if (directionState == 1) {
     if (buttonState == HIGH) {
       directionState = 0;   
       
      for (pos = 110; pos>=1; pos -=1)
       {
         myservo.write(pos);
         delay(5);
       }
     }
   }
 }