NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Control Servo with Button
May 01, 2011 by lnino |
Hi at all. Because I can't wait to get my parts which I have bought in a online shop to finish my Port Expansion project I started a new one. :-) I am trying to control a servo with buttons. The servo shall go into a special position by a button press. I studied as source the servo squirter project and some other stuff I found in the internet. But I don't get it work. The servo won't move. Can somebody find an error? Here is my code. servo_test.c:
|
---|---|
May 01, 2011 by missle3944 |
Hi lnino. I am delayed on a project similar to this one. On my (working)code. I have it set when the button is pushed then add 25 to pos. The pos=+ is what you are using to control servo. If i was you I would do the servo squirter project and just cut out the 2nd motor code.That is what I did here I think OCR1B is the pwm stuff that you dont use to control the servo but try something this...
Then once you push the button the servo will move 3000 over. -missle3944 |
May 01, 2011 by lnino |
I had problems to get the servo squirter working when I modified it. Maybe you can post your code to see what I did wrong. How did you get the right position? In my project it always stays at the same value. |
May 03, 2011 by lnino |
Hi at all. Now I got my code partitionally working. But it still won't exactly do what I want. I want to press a button and the servo should walk to a secific position. And the servo should stay on this position until I press another key or something else. Can someone help me to solve this issue. At the moment the button increases OCR1A and walks forward and after that maybe because of the interrupt the servo goes backwards. Also my display shows always the text of my old programm. Which is definatly not in the chip. And it shows many squares. Is that because my button is also connected to PINB like the display? And why is the led flashing and not lighting permanently? Because of the interrupt? Some data: Servo is connected to PB2 Button is connected to PB1 LED is connected to PC0 Here is a video of my test: Here is the code:
|
May 03, 2011 by Ralphxyz |
Sorry lnino but your video is not viewable on my mac OS X 10.6.7. I even went through a complicated driver install but nothing appears, maybe after a reboot. Ralph |
May 03, 2011 by lnino |
Maye you can see it here: |
May 11, 2011 by lnino |
Hi, does anybody have an idea? |
May 11, 2011 by Noter |
You need to debounce your button. Without debounce you may press once but the mcu will see many contacts which gives unpredictable results. |
May 11, 2011 by lnino |
I think this is not the problem. If I press the button just for a short period the servo moves just a bid forward. If I press the button for a long time the servo moves on with a big step. My problem is when I release the button the servo moves backwards. When I push the button I want that the servo moves to a specific position. And when I release the button the servo should stay in that position. How can I do that? |
May 11, 2011 by Ralphxyz |
lnino, as Noter said that sounds like a bouncing switch are you doing anything about de-bounce? Ralph |
May 11, 2011 by Noter |
Ok, I see maybe the debounce is not the problem. How many times can you add 100 to OCR1A before it wraps back to zero? And how long does the button need to be closed for that to happen? Maybe add a delay to the loop so slow things down? |
May 12, 2011 by lnino |
It wraps back to zero every time. If i push the button once it moves for instance 1cm forward and after that back to zero. If I keep the button pressed it moves forward until it reaches the end. When I release the button it goes back to zero. Therefore I am not able to keep a position. A delay would only slow down the back movement to zero, but won't stay in the current position. Or will this solve my problem? Difficult problem. |
May 12, 2011 by Noter |
Here's what I think is going on. The while loop is really fast. I don't think you can press and release the button quick enough to get only a few iterations. Probably you're getting thousands of iterations even with the shortest press you can do. The results are unpredictable and the motor definately can't respond to changes in the PWM as fast as they are happening so who knows where it will end up. If you add a delay of 100ms then you'll get 10 iterations per second while the button is pressed and then a quick press should move the position forward and leave it there. A longer press will wrap back to zero and move the position back. Maybe even 500ms would work better. Experiment with different values to see which one works best. |
Please log in to post a reply.
Did you know that you can adjust most wire strippers to make it easier to strip insulation faster? Learn more...
|