Rookie problem with Servo and two buttons

Hi there,

i'm trying to make the servo work by using 2 buttons (one button at a time, seperatly.). in my program only one button works.
can someone give me some good advice/help?

#include<Servo.h>

const int buttonPin1 =10;
const int buttonPin2 =1;
int buttonState1 = 0;
int buttonState2 = 0;
Servo myservo;

void setup() {
myservo.attach(12);
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);

}

void loop() {
buttonState1 = digitalRead(buttonPin2);
buttonState2 = digitalRead(buttonPin1);

if (buttonState1 == HIGH){
myservo.write(0);
}
else{
myservo.write(90);
}

if (buttonState2 == HIGH){
myservo.write(0);
}
else{
myservo.write(90);
}
}

Well look at your code and ask yourself what happens if you press on button1. What does the second statement do?

tell us how your buttons are connected.
pins used as inputs can be anything if the pullup isn't used and the pin isn't connected to anything.

I don't know why you're making it so difficult for yourself to do serial debug.

I also wonder what happened to your code tags.

PeterKWH:
Hi there,

i'm trying to make the servo work by using 2 buttons (one button at a time, seperatly.). in my program only one button works.
can someone give me some good advice/help?

Can you describe the programming logic you want to have, a bit more detailed, please?

Do you want the servo react on button state (pressed or released) while nothing is changing and "steady state"?

Or do you want the servo to act on a "state change" of a button, when the button changes from "released state" to "pressed state"?

Please describe in detail and in plain English words what you want to do, exactly!

buttonState1 = digitalRead(buttonPin2);
buttonState2 = digitalRead(buttonPin1);

I love it, you just can't make this stuff up.

Do you really want to check pin one for button 2 and vice-versa?

-jim lee

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You have THREE Conditions to test for.

  • Button1 ON AND Button2 OFF, servo 0
  • Button2 ON AND Button1 OFF, servo 0
  • BOTH Button1 and Button2 OFF , servo 90

Not sure if your code is supposed to be servo 0 for either button?
Cut and paste errors?

Thanks.. Tom.. :slight_smile: