Go Down

Topic: Trying to control servo with push button (Read 668 times) previous topic - next topic

jag3498

First of all, I do not have all that much programming experience. I want to control a servo with a button, so that I press the button and it moves the servo in a certain way, but this code will not work for me. I press the button and the servo will go back and forth 4-5 times.

Code: [Select]

#include <Servo.h>
Servo door;
int button;   
void setup() {
pinMode(4, INPUT);
 
door.attach(9);
door.write(0);
}

void loop() {
button = digitalRead(4); 
if (button == HIGH) {
delay(10);
     button = digitalRead(4); 
    if (button == HIGH) {
     door.write(180);
     delay(1000);
   door.write(0);
     delay(1000);
    }
}
}


Thanks,
Jack

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

jag3498

Are you saying make the button var a float?

Arrch


Are you saying make the button var a float?


No, he is saying that button needs either a pull down resistor, a pull up resistor or the code needs to enable the internal pull-up resistor.

AWOL

No, I'm asking how the switch is connected.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

jag3498

The push button is on a breadboard, with a wire from 5v to the first push button pin, then a wire from the other pin to the 4 pin

liudr

In this case, you are missing a pull-down resistor between arduino pin and ground. Use anything between 2K ohm and 40K ohm.

You will want to reverse the logic of your push button so you can use the internal pull-up resistor, so you don't have hook up a resistor.


liudr

You are welcome. I still recommend using the internal pull-up resistor. It's easy. No additional resistors on your breadboard.

Go Up