Slow servo on button press.

Hey guys,

I think this is a simple problem but I can’t seem to figure it out.

I have a servo that needs to swing back and forth quickly on an RF button.

The code works but take it’s sweet time getting the servo into position. I tried just injecting the sweep default sketch into it but then it runs the sweep continuously or will wait for the press, then run continuously… THANKS!

#include <Servo.h>

Servo panServo;

int servoPanAdjust = 0; //How much to add/subtract from the default setting by the remote
int servoPanDefault = 0; //Default position of servo when key is pressed

void setup() {

pinMode(2, INPUT);
pinMode(9, OUTPUT);

panServo.attach(8);

}

void loop() {

if (digitalRead(2) == HIGH) {

servoPanAdjust = (0, 175); // scale it to use it with the servo (value between 0 and 180). My serve was not functioning above 175 so I limited here.

panServo.write(servoPanAdjust);
delay(15);
}

else {
panServo.write(servoPanDefault);
delay(10);
}
}

servoPanAdjust = (0, 175);

What is that syntax supposed to do?

There's no sweep to that code. If those servos are moving too slowly then it is because you are underpowering them.

That was some dirty vestige of some earlier code. Looks like this now with the same behavior :

#include <Servo.h>

Servo panServo;

int servoPanAdjust = 175;
int servoPanDefault = 0;
void setup() {

pinMode(2, INPUT);
pinMode(8, OUTPUT);

panServo.attach(8);

}

void loop() {

while (digitalRead(2) == LOW) {

// scale it to use it with the servo (value between 0 and 180). My serve was not functioning above 175 so I limited here.

panServo.write(servoPanAdjust);
delay(15);
panServo.write(servoPanDefault);

}
}

Also, moves fine to position, has as delay between moves. (much longer than the delay(15):wink: The delay works with greater value but when holding the switch it repeats in between moves slowly

Now you've got a while loop telling it to go back and forth as long as you hold the button. That's completely different from what you started with.

Try this: Without any coding language or coding ideas, explain what this thing is supposed to do. Like if you were writing instructions for someone to use it. Don't be technical. Just explain what is supposed to happen when you press that button? What if you hold the button?

What actually happens?

None of that code sweeps anything. If the servo is moving slowly then you are underpowering it.

Delta_G:
There's no sweep to that code. If those servos are moving too slowly then it is because you are underpowering them.

It's supposed to swing a couple of toys back and forth in a natural motion as I hold down the button, for the duration of holding down the button. What I was saying is that when it was sweeping it was moving just fine, thats why I dismissed the power idea.

Yes, and what I am saying is that there is no sweep in this code. So if the servo is moving slowly with this code then it is because you are underpowering the servo. With this code it should just go directly to the position you told it to. Although 15ms isn't very long to give it time to move and be there for long.

As written it will send the servo to servoPanAdjust position, wait for 15ms (which may not even be long enough for it to get there) and then write the servo back to servoPanDefault. At that point it immediately writes it back to servoPanAdjust so I doubt you would ever see it even trying to go to servoPanDefault position as it is only told to go there for a few nanoseconds which certainly isn't enough time for it to reach the position.

So how do I add a loop to smooth it out that doesn't ignore my switch?

How it was behaving

Skyring54:
How it was behaving

- YouTube

That definitely isn’t using your latest code so it doesn’t really help. But it seems to be doing what you say you want. Of course when you put a load on the servo it will probably move slower.

If you need the servo movement to be faster then you need either a higher voltage driving that servo or more likely a more powerful faster servo.

Steve

Typical servos take about 120 mS to swing 60°, so you need about 350 mS delay between 175° swings.

Skyring54:
So how do I add a loop to smooth it out that doesn't ignore my switch?

It doesn't ignore anything. What do you mean? You just need to make sure that it has time to do what you tell it to do.

slipstick:
That definitely isn't using your latest code so it doesn't really help. But it seems to be doing what you say you want. Of course when you put a load on the servo it will probably move slower.

If you need the servo movement to be faster then you need either a higher voltage driving that servo or more likely a more powerful faster servo.

Steve

Thats the latest code. Moves plenty fast when I run a loop to get it to turn. Has to be code issue.

Delta_G:
It doesn't ignore anything. What do you mean? You just need to make sure that it has time to do what you tell it to do.

I tried to drop the "sweep" loop below the switch code and when I set the read to high it just ran the loop continuously when I set it to low it it waited for the switch but wouldn't stop the loop on release

Skyring54:
I tried to drop the "sweep" loop below the switch code and when I set the read to high it just ran the loop continuously when I set it to low it it waited for the switch but wouldn't stop the loop on release

Describing code in other words is hard to follow. Post a piece of code you used if you want to talk about it and get any sort of feedback on why it worked the way it did.

Skyring54:
Thats the latest code. Moves plenty fast when I run a loop to get it to turn. Has to be code issue.

Then you have a very strange servo because mine do nothing like that when I run the last code you posted.

Steve

slipstick:
Then you have a very strange servo because mine do nothing like that when I run the last code you posted.

Steve

It was strange. I hooked up alternate power on a much bigger servo and tried to run it and ....well it acted like you guys suspected it would. I'll post where I am at now and see if I can describe its behavior better.

#include <Servo.h>

int clickityclak = 2;

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() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(clickityclak, INPUT);
pinMode(9, OUTPUT);

}

void loop() {
if (digitalRead(clickityclak) == HIGH){
for (pos = 0; pos <= 180; pos += 5) { // 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(25); // waits 15ms for the servo to reach the position
}
}
for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(25); // waits 15ms for the servo to reach the position
}
}

This just sweeps. It seems to ignore the button I have on it entirely. I just need to stop/start on the button press. The button I’m using is the one in the video (315mhz transmitter/receiver from adafruit) I know the button itself is fine.

Yes, that code reads the button once and then does 36 steps one way and then 36 steps back before it goes back around to read the button again.

You need code that reads the button, takes one step in the right direction, and then reads the button again.

int step = 5;
int pos = 0;

void loop() {

          // if button is pressed
     if(digitalRead(buttonPin) == LOW){
           pos = pos + step;
           if(pos == 0 || pos == 180){
               step = -step;
           }
           myServo.write(pos);
     }
}

Skyring54:
So, I need to ditch the "if" loop

if statements don't loop. They're just statements.

Skyring54:
or is there a way to step, check, step inside of it?

Yes, I just showed you. Let the loop function do the looping. Check the button and take one step.