Pages: [1]   Go Down
Author Topic: Trying to control servo with push button  (Read 589 times)
0 Members and 1 Guest are viewing this topic.
Directly above the center of Pluto, or near Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26491
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Floating input pin?
Logged

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

Directly above the center of Pluto, or near Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you saying make the button var a float?
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26491
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, I'm asking how the switch is connected.
Logged

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

Directly above the center of Pluto, or near Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7255
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Directly above the center of Pluto, or near Pittsburgh
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works perfectly! Thanks!
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7255
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: