Hello! i want to control a servo angle with a push of button. when i press it, the angle will be a value, and if it is released, it will write another value. i have written a sketch,but it does not work. can anybody lend a hand?
On this page, http://www.pighixxx.com/abc-arduino-basic-connections/
Open the pdf file: "Set 1 (Card 1,2,3)".
The button with pull-up is on the left. The orange label to "IN" is to the digital input (pin 2 in your sketch).
Hi thanks again for the reply. my wiring is similar to that.btw what is wrong with this sketch? i want it to function like what i have previously mentioned above
If you have an exteranl pull-up, the sketch is okay.
So perhaps there is something with the button or the connection witht the servo.
For example if you use a breadboard, the contacts could be bad.
Can you check both seperately ?
Can you make the servo turn with a sketch ?
Can you write the state of the button to the serial port ?
hi, i just checked what u asked, and i realsied that the button is not working, servo is. when i press the button, the serial monitor still read s 0. when i change the switch the situation is still the same. to be sure, the switch is connected to pin 2 and Gnd right? thanks
You should add a pull-up resistor of 10k from digital pin to 5V.
You could also use the internal pull-up resistor: pinMode (switchPin, INPUT_PULLUP);
A pull-up resistor pulls the digital input to 5V if the button is not pressed. If the button is pressed, the digital input is pulled low.