Controlling Servo with a single push button Coding Help Needed!

Hello Everyone, I have been learning Arduino over the last month in order to incorporate it into one of my engineering and design projects.

I need to have a single button controlling a 180 degree servo. I need it to turn one way until it hits its limit of 5 degrees or the button is released. Then on the next press I need it to turn the other way until it hits its limit of 170 degrees or until the button is pressed in a cycle or loop.

I currently have the system working good with 2 basic push buttons that came in my Arduino kit right now with one controlling the clockwise direction and the other controlling the counter-clockwise direction. The single button that I will ultimately be using is a keyestudio button switch which I believe puts out a analog signal instead of completing the circuit.

Right now I am not really sure how to use the code that I have right now and convert it to be able to do the same thing with the new button switch.

Thank you All,

RK

Code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int angle =170; // initial angle for servo
int angleStep =5;

#define LEFT 12 // pin 12 is connected to left button
#define RIGHT 2 // pin 2 is connected to right button

void setup() {
Serial.begin(9600); // setup serial
myservo.attach(8); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assign pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println(“Group 1”);
}

void loop() {

while(digitalRead(RIGHT) == LOW){

if (angle > 5 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 5;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print(“Moved to: “);
Serial.print(angle); // print the angle
Serial.println(” degree”);
}
}

delay(0); // waits for the servo to get there
}

while(digitalRead(LEFT) == LOW){

if (angle >= 5 && angle <= 170) {
angle = angle + angleStep;
if(angle >170){
angle =170;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print(“Moved to: “);
Serial.print(angle); // print the angle
Serial.println(” degree”);
}
}

delay(1); // waits for the servo to get there
}//

}

Rgkulow:
Then on the next press I need it to turn the other way until it hits its limit of 170 degrees or until the button is pressed in a cycle or loop.
....
....
The single button that I will ultimately be using is a keyestudio button switch which I believe puts out a analog signal instead of completing the circuit

Shouldn't that be released?

Can you supply details of the button you will actually be using?

You will have to look at the specs for the button you will be using to figure out how it responds. Do you have a link? Without that, it will be difficult to help.

https://wiki.keyestudio.com/index.php/Ks0029_keyestudio_Digital_Push_Button

I was actually wrong about the output it is the same as a basic button I believe.

I know how to hook it up but its the coding that I’m having problems with. I have no clue on how to take the code that I have now and modify it to one button that has alternating functions (every other it opens and between them when the button is pressed it closes)

So to confirm:

Power up or reset with servo at some random point between 5 and 170

[Where should it attach to? Default is 90. That can be changed. But if the servo's not actually at the attach pos (and chances are it won't be unless you can guarantee its position at shutdown) it will slam there not step]

Press and hold button, servo heads off towards 5 (or could be there already)

If servo gets to, or is at, 5 it stops

If button released before servo gets to 5, servo stops

Then, with servo either at 5 if it got there, or anywhere else it might have stopped, on the next press and hold of button, servo heads towards 170

If servo gets to 170 it stops

If button released before servo gets to 170, servo stops.

Is that correct?

Rgkulow:
Ks0029 keyestudio Digital Push Button - Keyestudio Wiki

I was actually wrong about the output it is the same as a basic button I believe.

Reading the specs I'd say Yes and No. Since it requires both + and - connections to power, it seems the output will always be either high or low and wont float. That means you don' t have to provide a pullup or pulldown resistor. No huge benefit to an Arduino project, since there's an internal pullup available anyway.

Seems from the sample code it's active low which is good.

Providing both + and - connections is presumably something you're happy to do.

Power up or reset with servo at some random point between 5 and 170

[Where should it attach to? Default is 90. That can be changed. But if the servo's not actually at the attach pos (and chances are it won't be unless you can guarantee its position at shutdown) it will slam there not step]

[/quote]

In the code I have now when ever the system is powered on it automatically moves the servo to 170 degree. I'm hoping that I will be able to keep this in the finial version. This would make it know where it starts off each time and the first button press would make it go towards 5 degree

You can attach it where you like but if the servos not actually at that angle when you power off it will fly there at power up.

The only way to know where a servo actually is is to bring out a 4th wire from the servo and read that. Adafruit sell one with a 4th wire.

I am okay with it moving to the 170 degree position on start up. I am making a clamping device(like a claw) and when the system is powered on Id like the claw to open up every time it turns on and then use a button to decide when it closes and opens up after that initial phase.

Use the ideas in the state change detect example to a) set a moving flag on a new press and b) clear that flag on a new release. Also set a step variable as +1 or -1 as a change of direction ready for the next press.

Then in loop() check the moving flag and if its set move the servo by the + or - step depending on direction. (And of course dont go past the ends of the 5 to 170 range)

Maybe have a small delay() to control speed; that can of course be made dealy() less with millis().

This way, each time thru loop() the button is checked so when the servo is moving a step at a time while the button is pressed, it stops as soon as it sees it's been released.

Thanks for saying it's for a claw; that made me realise it's exactly the thinking I will use on mine. In fact, same thinking can be used to move the arm and base.

Thanks! I never heard of that... I'll look into that later today. I give a update if it works out or if I might need more help

I since wrote a sketch to work the way you suggested, ala my coding idea above. So far I tested on a loose servo, not on my claw, but am sure it will be perfect.

I'll share here if you like.

Thanks for the idea!