Button Not Working?

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.

#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

The Button, 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 :confused:

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

if (buttonState = 1) {oops

if (buttonState = 1)

Big oopsie right there.

What values do you think buttonState can have? Hint: it will never ever ever equal 2.

Wrong variable, wrong operator.

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

Ok,right variable, wrong operator.

AWOL:
Ok,right variable, wrong operator.

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

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

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?

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

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.

#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);
  }
}

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

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.

no that problem has been solved

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

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.

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

...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.)

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

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

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

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

Instead of giving us a f**ing diagram with a bunch of parts on it that aren't what you really have, why don't you pull out a good old pen and paper and draw us a picture of what you do really have.