Use Button on Arduino to control a Servo Motor

I'm trying to use a button on Arduino to make a servo motor sweep when the button is pressed and stop when the button is released. I combine the codes in the examples of Button and Servo. My code goes like this.
In the video, you can see my circuit and how the servo reacts to the codes.

So I have several problems with my outcome. First is that the servo just doesn't stay in the position when I am not pressing the button. It rotates in a very slow speed. Second is that when I press or release the button, it seems to always take the servo a while to follow the order and change the state. Also, when the servo sweeps, it does not act actually as the codes say to go from 0 to 180 degrees and rotate back. The servo would rotate clockwise for a couple rounds and go counter-clockwise then.


Looking for the possible reasons online, and it says on Arduino website that "On a continuous rotation servo, this(write()) will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement)." While even if I wrote write(90), the servo still rotates. Then I tried to use detach() function, and it works this time. 

Now only when the button is pressed will the servo be attached to the signal.




Comments

Popular Posts