Hi, just so you know I am very new to arduino so apologies if im missing something simple, but it seems like no matter what I do, my switch always reads high, leading to my motor moving back and forth constantly. Heres my code:
#include <Servo.h>
Servo myservo;
int pos = 0;
int buttonState = 0;
const int buttonPin = 2;
If your button is between the input pin and 5V, you will need a 10K resistor between that input pin and gnd, so the pin is pulled to gnd, LOW, when the button is open circuit.
The same applies if you have the button between input pin and gnd, you need a pullup resistor to pull the input pin HIGH when the button is not pressed.
In this case of button from input pin the gnd, the Arduino code has a statement that turns on an internal pullup resistor.
Unfortunately there is no pull down statement for your applicattion, you will have to supply the pull down resistor.
If it is one of those popular 6mm tactile pushbutton switches, be sure to wire to diagonal pins to avoid a short. The best way to wire a switch is one side of the switch to ground and the other to an input pin with the pinMode set to INPUT_PULLUP. The input will read HIGH when not pressed and LOW when pressed. Adjust the program logic accordingly.