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