NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Basic Electronics » Using pushbuttons to increment position of servo
November 22, 2011 by Jacp |
Hello. I am a Norwegian Electronics student and I have some questions related to changing the position of a servo using pushbuttons. I have a pushbutton much alike the one called Tactile Switch on this link: http://www.futurlec.com/SwTactile.shtml I tried using two pushbuttons to increment and decrement the position of a small servo. However regardless if I pushed the button once or held it in for a while the servo would only go to it's full position. Eighter 1ms or 2 ms. Therefore I was wondering if this button emits any kind of noise when pressed? And if this is not the problem what is? Sorry for my bad English! Hope to get some answers. Here is the code I used:
|
---|---|
November 22, 2011 by treymd |
Probably. I believe you've encountered "switch bouncing". Appropriatly enough, the solution is called debouncing, where you use any number of methods to filter out the multiple on/off states that a switch may ancounter when transitioning from off to on. The easiest method would be to insert a short delay after you detect a button press, so that your program basically ignores the repeated bounces and acts only on the first one. |
November 23, 2011 by Jacp |
Thanks Treymd! I added "delay_ms(5)" after "pos+=25;" and now it all works pretty well! |
November 23, 2011 by SpaceGhost |
I have some code for two servo motor control that uses 4 tactile-type push buttons (left & right for each servo) for incremental control that works pretty well -
The LCD is pretty useful here to "map out" servo positions. For example, you could run the program to see the values of posx and posy, then re-write the program adding something like -
that would add another pushbutton that by pressing will set the positions of the servos to your predetermined positions. Using 3 for "posx+= 3;",etc. gives my servos a slow, steady travel - and I don't need a delay. Holding a button down causes the servo to turn until it reaches PMW_MAX or PMW_MIN. Quick, short presses of a button cause a servo to move in small increments. Maybe your speed of 25 was a bit too fast, and maybe the delay has now slowed the reaction of the button press? I may be wrong though. However, I did not find the need for a pushbutton delay for switch debounce in my code. Dave |
Please log in to post a reply.
Did you know that a NerdKit can take control of a remote-controlled car? Learn more...
|