# Button Not Working?

**URL:** <https://forum.arduino.cc/t/button-not-working/466908>\
**Category:** Programming\
**Created:** [June 26, 2017, 9:03pm UTC](https://forum.arduino.cc/t/button-not-working/466908 "2017-06-26T21:03:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 9:03pm UTC](https://forum.arduino.cc/t/button-not-working/466908/1 "2017-06-26T21:03:33Z")

</div>

I'm working on a little "experiment" where if i were to press a button, the servo would spin 180 degrees, and if you pressed the button again, it would spin 180 degrees back. The button part seems to not be working although i'm not getting any errors.

```cpp
#include <Servo.h>

Servo servo;
int buttonPin = 8;
int buttonValue = 0;
int buttonState = 0;

void setup() {
  servo.attach(7);
  servo.write(160);
  pinMode(buttonPin, INPUT);
}

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

  if (buttonState == HIGH) {
    buttonValue++;
  }
  if (buttonValue = 1) {
    servo.write(0);
  }
  if (buttonValue = 2) {
    buttonValue = 0;
  }
  if (buttonValue = 0) {
    servo.write(160);
  }
}

```

Also, how i set up the wiring:  
[Diagram](https://puu.sh/wv0KC/38f54637c1.png)

[The Button](https://www.osepp.com/electronic-modules/sensor-modules/76-push-button-module), I'm using that as a button and i couldn't find a good way to represent it here, so i used a potentiometer in the diagram 😕

And finally, my servo keeps vibrating as well as heating up quite quick, should i be worried?

---

<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:** [June 26, 2017, 9:04pm UTC](https://forum.arduino.cc/t/button-not-working/466908/2 "2017-06-26T21:04:32Z")

</div>

` if (buttonState = 1) {`oops

---

<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:** [June 26, 2017, 9:09pm UTC](https://forum.arduino.cc/t/button-not-working/466908/4 "2017-06-26T21:09:57Z")

</div>

Wrong variable, wrong operator.

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 9:13pm UTC](https://forum.arduino.cc/t/button-not-working/466908/5 "2017-06-26T21:13:23Z")

</div>

Sorry i didn't upload the correct code, and google closed on me

---

<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:** [June 26, 2017, 9:15pm UTC](https://forum.arduino.cc/t/button-not-working/466908/6 "2017-06-26T21:15:16Z")

</div>

Ok,right variable, wrong operator.

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 9:18pm UTC](https://forum.arduino.cc/t/button-not-working/466908/7 "2017-06-26T21:18:58Z")

</div>

> AWOL:  
> Ok,right variable, wrong operator.

What does that mean exactly , i've only been programming arduino for about a month.

---

<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:** [June 26, 2017, 9:21pm UTC](https://forum.arduino.cc/t/button-not-working/466908/8 "2017-06-26T21:21:07Z")

</div>

But in that time you've seen the difference between an assignment and a comparison.

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 9:24pm UTC](https://forum.arduino.cc/t/button-not-working/466908/9 "2017-06-26T21:24:17Z")

</div>

> AWOL:  
> But in that time you've seen the difference between an assignment and a comparison.

\

I'm a bit confuzed, and sorry if i'm being rude and/or pushy, but what exactly did i do wrong?

---

<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:** [June 26, 2017, 9:28pm UTC](https://forum.arduino.cc/t/button-not-working/466908/10 "2017-06-26T21:28:09Z")

</div>

`if (buttonValue = 1) `You wrote three assignments when you wanted three comparisons.

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 9:35pm UTC](https://forum.arduino.cc/t/button-not-working/466908/11 "2017-06-26T21:35:21Z")

</div>

> AWOL:  
> `if (buttonValue = 1) `You wrote three assignments when you wanted three comparisons.

ok thank you, that triggered a memory telling me to add another "=" to each if statement, it doesnt do what i want sort of, but when i;m not pressing the button, it jiggles vigorously, and if i hold the button it stops.

```arduino
#include <Servo.h>

Servo servo;
int buttonPin = 8;
int buttonValue = 0;
int buttonState = 0;

void setup() {
  servo.attach(7);
  servo.write(160);
  pinMode(buttonPin, INPUT);
}

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

  if (buttonState == HIGH) {
    buttonValue++;
  }
  if (buttonValue == 1) {
    delay(20);
    servo.write(0);
    delay(20);
  }
  if (buttonValue == 2) {
    buttonValue = 0;
  }
  if (buttonValue == 0) {
    delay(20);
    servo.write(160);
    delay(20);
  }
}

```

---

<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:** [June 26, 2017, 10:22pm UTC](https://forum.arduino.cc/t/button-not-working/466908/12 "2017-06-26T22:22:25Z")

</div>

Most likely, then, your switch is not wired correctly. How IS it wired?

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 10:28pm UTC](https://forum.arduino.cc/t/button-not-working/466908/13 "2017-06-26T22:28:35Z")

</div>

> PaulS:  
> Most likely, then, your switch is not wired correctly. How IS it wired?

no that problem has been solved, the issue now is the vigorous shaking of the servo when the button isn't being pushed down.

---

<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:** [June 26, 2017, 10:34pm UTC](https://forum.arduino.cc/t/button-not-working/466908/14 "2017-06-26T22:34:15Z")

</div>

> no that problem has been solved

How? Your diagram does not show a resistor, and you aren't using the internal pullup resistor.

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [June 26, 2017, 10:40pm UTC](https://forum.arduino.cc/t/button-not-working/466908/15 "2017-06-26T22:40:40Z")

</div>

If the button isn't pushed then digitalRead() will show HIGH.

So the buttonValue gets incremented. Let's say it starts off at 0, it gets to 1 and the servo tries to drive to zero.

The next time around the loop, the pin is still HIGH so buttonValue becomes 2. That's then quickly corrected back to 0. The last if() statement sees this and tries to drive the servo to 160.

..repeat forever.

One quick way to fix this would be to move the reset-to-zero after the check-for-zero. Re-order the if() statements so they are looking for 0, 1, 2 in that order.

@PaulS - check the diagram for the button he's using. (Link in 1st post). There's a 10K pullup.

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [June 26, 2017, 10:41pm UTC](https://forum.arduino.cc/t/button-not-working/466908/16 "2017-06-26T22:41:46Z")

</div>

> doosteep:  
> i couldn't find a good way to represent it here, so i used a potentiometer in the diagram 😕

...And that's the biggest reason I hate F\*\*ing diagrams. Don't show components you don't have (or don't intend to buy.)

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 26, 2017, 10:57pm UTC](https://forum.arduino.cc/t/button-not-working/466908/17 "2017-06-26T22:57:45Z")

</div>

> check the diagram for the button he's using. (Link in 1st post). There's a 10K pullup.

I doubt that @doosteep realized that when he wrote

```arduino
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
    buttonValue++;
  }

```

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [June 26, 2017, 11:01pm UTC](https://forum.arduino.cc/t/button-not-working/466908/18 "2017-06-26T23:01:49Z")

</div>

...Particularly since the "example code" which comes with the button specifically calls for a pull_down_ resistor!

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 26, 2017, 11:28pm UTC](https://forum.arduino.cc/t/button-not-working/466908/19 "2017-06-26T23:28:05Z")

</div>

> MorganS:  
> If the button isn't pushed then digitalRead() will show HIGH.
> 
> So the buttonValue gets incremented. Let's say it starts off at 0, it gets to 1 and the servo tries to drive to zero.
> 
> The next time around the loop, the pin is still HIGH so buttonValue becomes 2. That's then quickly corrected back to 0. The last if() statement sees this and tries to drive the servo to 160.
> 
> ..repeat forever.
> 
> One quick way to fix this would be to move the reset-to-zero after the check-for-zero. Re-order the if() statements so they are looking for 0, 1, 2 in that order.
> 
> @PaulS - check the diagram for the button he's using. (Link in 1st post). There's a 10K pullup.

The 10k is just to represent the button as iit isn't in fritzing

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 26, 2017, 11:52pm UTC](https://forum.arduino.cc/t/button-not-working/466908/21 "2017-06-26T23:52:51Z")

</div>

> The 10k is just to represent the button as iit isn't in fritzing

You are missing the point. There is a 10K pullup resistor built into the button module. It is like running the pin in INPUT\_PULLUP mode. The digitalRead() of the button will be HIGH when it is not pressed, and LOW when it is pressed.

> **[ButtonSwitch.pdf](https://www.osepp.com/downloads/pdf/ButtonSwitch.pdf)**
>
> 33.62 KB

---

<div class="post-metadata">

**Author:** ![doosteep](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@doosteep](https://forum.arduino.cc/u/doosteep)\
**Post date:** [June 27, 2017, 12:20am UTC](https://forum.arduino.cc/t/button-not-working/466908/22 "2017-06-27T00:20:47Z")

</div>

thank you all, the problem has been solved.

#include \<Servo.h\>

Servo servo;  
int buttonPin = 8;  
int buttonValue = 0;  
int buttonState = 0;

void setup() {  
servo.attach(7);  
servo.write(160);  
pinMode(buttonPin, INPUT);  
}

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

if (buttonState == LOW) {  
buttonValue++;  
}  
if (buttonValue == 0) {  
delay(20);  
servo.write(160);

}  
if (buttonValue == 1) {  
delay(20);  
servo.write(0);

}  
if (buttonValue == 2) {  
buttonValue = 0;  
}  
if (buttonValue == 0) {  
delay(20);  
servo.write(160);

}  
}

[Next page](https://forum.arduino.cc/t/button-not-working/466908.md?page=2)
