# Servo with a button

**URL:** <https://forum.arduino.cc/t/servo-with-a-button/418020>\
**Category:** General Guidance\
**Created:** [November 11, 2016, 2:00am UTC](https://forum.arduino.cc/t/servo-with-a-button/418020 "2016-11-11T02:00:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwojtowicz](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@dwojtowicz](https://forum.arduino.cc/u/dwojtowicz)\
**Post date:** [November 11, 2016, 2:00am UTC](https://forum.arduino.cc/t/servo-with-a-button/418020/1 "2016-11-11T02:00:40Z")

</div>

Hey im doing a project in class. We are going to manually spin the motor some degrees and then whenever the button is released the servo will move back to the 0 position. I am new to arduino and im not sure why this code is not working.

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

int pos = 0; // variable to store the servo position  
int button1 = 4; //button pin, connect to ground to move servo  
int press1 = 0;

void setup() {  
myservo.attach(9); // attaches the servo on pin 9 to the servo object  
pinMode(button1, INPUT);  
digitalWrite(4, HIGH); //enable pullups to make pin high  
}

void loop() {  
press1 = digitalRead(button1);  
if (press1 == HIGH)  
{  
for (pos \>0; pos \>= 0; pos -=1)  
{  
myservo.write(pos);  
delay(5);  
}  
}  
}

 ![servo.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/b/5bbd0061f5f17221a8de81ab3086d1b034dbd2d9.jpeg)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [November 11, 2016, 2:33am UTC](https://forum.arduino.cc/t/servo-with-a-button/418020/2 "2016-11-11T02:33:05Z")

</div>

```cpp
int button1 = 4; //button pin, connect to ground to move servo

```

```cpp
if(press1 == HIGH)

```

Those two lines are contradictory.

```cpp
for (pos >0; pos >= 0; pos -=1)

```

Is not right. See. [https://www.arduino.cc/en/Reference/For](https://www.arduino.cc/en/Reference/For)

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [November 11, 2016, 5:41am UTC](https://forum.arduino.cc/t/servo-with-a-button/418020/3 "2016-11-11T05:41:22Z")

</div>

I don't see anything wrong with:  
int button1 = 4; //button pin, connect to ground to move servo  
and  
if(press1 == HIGH)

except your diagram shows the button on D2 and not D4.  
Usually if one uses the internal pullup, the button would be wired to connect the pin to Gnd when pressed, and a LOW would be tested for.

You should probably change this  
for (pos \>0; pos \>= 0; pos -=1)  
to  
for (pos =180; pos \>= 0; pos -=1)  
as most servos only move from 0 to 180 degrees.

5mS may not be enough time to get to 180 initially.  
The Uno may also not have enough current available to drive the servo.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 11, 2016, 6:09am UTC](https://forum.arduino.cc/t/servo-with-a-button/418020/4 "2016-11-11T06:09:35Z")

</div>

Hi,  
You have to realise that you have two output states, 0deg and 90deg.  
So you need to code both output conditions, look up if ..else statement.  
You don't need to use a for loop to move the servo.

```cpp
myservo.write(0);
myservo.write(90);

```

Will do, unless you want to slowly move the servo arm.

Tom... 🙂

---

<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:** [May 5, 2021, 6:01pm UTC](https://forum.arduino.cc/t/servo-with-a-button/418020/5 "2021-05-05T18:01:30Z")

</div>

Closed on May 5, 2021, 6:01pm UTC


