# Servo motor with one button

**URL:** <https://forum.arduino.cc/t/servo-motor-with-one-button/874623>\
**Category:** General Guidance\
**Created:** [June 14, 2021, 6:37pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623 "2021-06-14T18:37:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick\_d121](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nick\_d121](https://forum.arduino.cc/u/nick_d121)\
**Post date:** [June 14, 2021, 6:37pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/1 "2021-06-14T18:37:44Z")

</div>

so the idea behind this project is i have a servo motor with one button what im trying to do is the motor starts at 0 deg you hit the button once the servo goes to 180 deg you hit that same button again the motor goes back to 0 deg. The problem that im having is that when i hit the button it goes to 180 and goes right back to 0. I have tried deboucing the button and that did not work i have tried all kind of ways i will post the most recent thing i have tried. anyone feel free to chime in see if we can crack this thing.

```cpp

#include <Servo.h>
#include <ezButton.h>

// constants won't change
const int BUTTON_PIN = 5; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin

ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0; // the current angle of servo motor

void setup() {
  Serial.begin(9600); // initialize serial
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object

  servo.write(angle);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()) {
    Serial.println("The button is pressed");

    // change angle of servo motor
    if(angle == 0)
      angle = 180;
    else
    if(angle == 180)
      angle = 0;

    // control servo motor arccoding to the angle
    servo.write(angle);
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 14, 2021, 6:56pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/2 "2021-06-14T18:56:30Z")

</div>

@nick_d121 Why do users choose to ignore the advice on posting code ?

**The easier you make it to read and copy your code the more likely it is that you will get help**

Please follow the advice given in the link below when posting code , **use code tags** and post the code here

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

As to your problem, you need to detect when the button **becomes** pressed rather than when it **is** pressed. I imagine the the ezButton has such a function or look at the StateChangeDetection example in the IDE to see how to do it without a library

Each time you detect that the button has become pressed move the servo to the opposite angle

---

<div class="post-metadata">

**Author:** ![nick\_d121](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nick\_d121](https://forum.arduino.cc/u/nick_d121)\
**Post date:** [June 14, 2021, 7:46pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/3 "2021-06-14T19:46:52Z")

</div>

thank you for your feed back i will keep that in mind next time, so i have looked through the stateChangeDetection example from that i was able to make a program where it would print lets say up and down for example to see that it was registering that the button was doing different things depending on the state it was pushed in. Still, the servo wants to go back to 0 deg when it moves to 180 and for example it will run through the up state over and over again.

```cpp
#include <Servo.h>

int buttonState = 0;
int lastButtonState = 0;
int buttonPin = 7;
int buttonPushCounter = 0;
int directionState = 0;

int servoPin = 9;
int angle = 0;

Servo servo;
  

void setup() {
  pinMode(buttonPin, INPUT);
  servo.attach(servoPin);
  servo.write(angle);
  
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState){
    if (buttonState == HIGH){
       // if the current state is HIGH then the button went from off to on:
     
      Serial.println("on");
      
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      buttonPushCounter++;
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

      if (directionState == 0 && buttonPushCounter == 1){
        Serial.println("going up");
        servo.write(180);
        directionState = 1;
      }
      else if (directionState == 1 && buttonPushCounter == 1){
        Serial.println("going down");
        servo.write(0);
        directionState = 0;
    
      }
    }
    // Delay a little bit to avoid bouncing
    delay(200);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  buttonPushCounter = 0;
} 

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 14, 2021, 8:15pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/4 "2021-06-14T20:15:15Z")

</div>

You don't need the directionState variable. Just write 0 to the servo when buttonPushCounter is even and 180 to it when buttonPushCounter is odd

buttonPushCounter is odd if `buttonPushCounter % 2` equals 1

---

<div class="post-metadata">

**Author:** ![nick\_d121](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nick\_d121](https://forum.arduino.cc/u/nick_d121)\
**Post date:** [June 14, 2021, 8:39pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/5 "2021-06-14T20:39:49Z")

</div>

so unfortunately with the servo that didnt work either like i said when doing that i can get it to print up and down as well when servo is not active but when the servo code is active its seems like its not completing the loop

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 14, 2021, 8:41pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/6 "2021-06-14T20:41:01Z")

</div>

Please post your attempt to use the technique that I suggested

---

<div class="post-metadata">

**Author:** ![nick\_d121](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nick\_d121](https://forum.arduino.cc/u/nick_d121)\
**Post date:** [June 14, 2021, 8:41pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/7 "2021-06-14T20:41:57Z")

</div>

```cpp
#include <Servo.h>

int buttonState = 0;
int lastButtonState = 0;
int buttonPin = 7;
int buttonPushCounter = 0;
int directionState = 0;

int servoPin = 9;
int angle = 0;

Servo servo;
  

void setup() {
  pinMode(buttonPin, INPUT);
  servo.attach(servoPin);
  servo.write(angle);
  
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState){
    if (buttonState == HIGH){
       // if the current state is HIGH then the button went from off to on:
     
      Serial.println("on");
      
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
      buttonPushCounter++;
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

      if ( buttonPushCounter % 2){
        Serial.println("going up");
        servo.write(180);
        //directionState = 1;
        //delay(3000);
        Serial.println(servo.read());
      }
        
      
     else {
        Serial.println("going down");
        servo.write(0);       
        //directionState = 0;
        //delay(3000);
        Serial.println(servo.read());
     
      }
    
    }
    // Delay a little bit to avoid bouncing
    delay(200);
    

    
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  //buttonPushCounter = 0;
  //Serial.println("end of loop");
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 14, 2021, 8:56pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/8 "2021-06-14T20:56:38Z")

</div>

How is the button input wired ?  
Have you got a pullup or pulldown resistor in place to keep the input pin in a known state at all times ?

---

<div class="post-metadata">

**Author:** ![nick\_d121](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nick\_d121](https://forum.arduino.cc/u/nick_d121)\
**Post date:** [June 14, 2021, 8:59pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/9 "2021-06-14T20:59:57Z")

</div>

so its a mini push button switch on one side I have connect to 3.3v and the other post is connected to ground with a 5k ohm resistor and on the other side of the ground I have it going to the input

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 14, 2021, 9:14pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/10 "2021-06-14T21:14:54Z")

</div>

Here is your code slightly rearranged  
Note that the pin numbers have been changed because I tested it on my test setup and that I used INPUT\_PULLUP to avoid the need to use external pullup/pulldown resistors

The main difference from your code is that the test for direction and writing to the servo are outside of the state change detection test. this results in the servo being written to continuously but there is no harm in that but it could be changed if it offends you

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [June 14, 2021, 9:16pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/11 "2021-06-14T21:16:12Z")

</div>

I would suggest using INPUT\_PULLUP and wiring your switch from GND to the input. Then just check for LOW instead of HIGH.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 12, 2021, 9:16pm UTC](https://forum.arduino.cc/t/servo-motor-with-one-button/874623/12 "2021-10-12T21:16:42Z")

</div>

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
