最新消息:啵叻潮流,抢鲜了解时尚潮流风向。www.booolo.com

Arduino: Control a servo with a Pushbutton.

Technology kumuking 169000浏览 0评论

It’s a simple Arduino project. The servo is controlled by the pushbutton. When the button is pushed, the servo will turn clockwise. When the button is pushed the second time, the servo will turn anti-clockwise. Some guys might think it’s not valuable to do it, or just for fun.

Sometimes, you might have some products with two or more modes. Here what I show is one of the ways how to switch the mode. When the button is pushed once, the mode will be changed. The number of states is decided by the number of modes.

When I got those stuff, I began to code immediately. At first, I think when the button is pushed, it will send a signal to the board. And when the board get the signal, the state will be changed. But it’s wrong. I got a shallow thought, because when the button is pushed, the signal is sent continuous not only once, which means it will send the same signals many times during the button is pushed. So if I use my original way to write code, the state will be changed many times when I push the button once.

So I stop to drink tea and think about it. After I drank the tea, I find a method to solve it. I will use the former state to determine the next state. That’s what I learned about State Machine in the course VLSI Design.

What I am showing here just has two modes. It’s easy to modify it if there are more than 2 modes in any project.

I uploaded a video to show how the servo work with the control of pushbutton

01 #include <Servo.h>
02
03 // Set digital pin numbers:
04 const int servoPin = 8// The number of the Servo pin
05 const int buttonPin = 9// The number of the Pushbutton pin
06
07 int buttonState = 0// Variable for reading the pushbutton status
08 int directionState = 0// Variable for reading direction of the servo
09
10 Servo myservo// Create servo object to control a servo
11
12 int pos = 0// Variable to store the servo position
13
14
15 void setup() {
16   myservo.attach(8);  // attaches the servo on pin 8 to the servo object
17   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
18 }
19
20 void loop(){
21   // read the state of the pushbutton value:
22   buttonState = digitalRead(buttonPin);
23
24   if (directionState == 0){
25     //The button is pushed
26     if (buttonState == HIGH) {
27       directionState = 1;// The direction for the servo is clockwise
28
29       // goes from 0 degrees to 180 degrees in steps of 1 degree
30       for(pos = 0; pos < 180; pos=pos+1)
31       {
32         myservo.write(pos);  // tell servo to go to position in variable ‘pos’
33         delay(15);  // waits 15ms for the servo to reach the position
34       }
35     }
36
37   } else if (directionState == 1) {
38     // The button is pushed
39     if (buttonState == HIGH) {
40       directionState = 0// The direction for the servo is anti-clockwise
41
42       // goes from 180 degrees to 0 degrees in steps of 1 degree
43       for(pos = 180; pos>=1; pos=pos1)
44       {
45         myservo.write(pos);  // tell servo to go to position in variable ‘pos’
46         delay(15);  // waits 15ms for the servo to reach the position
47       }
48     }
49   }
50 }

转载请注明:Yung » Arduino: Control a servo with a Pushbutton.

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (49)

  1. Hello, I was looking on the net and I saw your website. Keep up the great work.
    26732054728年前 (2011-04-10)Reply
    • thank you
      kumuking8年前 (2011-04-13)Reply
  2. Hi, I saw this on your Youtube site, and this is what I really needed. Is it possible if you can give me the wiring diagram of this? I have an arduino Uno. And if I want to do this for two servos instead of one, is it possible to control it with one button press? if possible, can you help me with it? I want to make a motorized screen for my carputer. Thanks.
    LHLXD7年前 (2011-08-07)Reply
    • This code is written almost half years ago. Today I checked this code again, and found it's not good. I will tell you about the flow of the program. Wiring: a button and a servo should have at least 3 wires. Two for powers, one for data. You can find everything in the datasheet. The data wires should be connected to digital pins. When the button is pressed, the pin connected to button should be high, then to checked the status of all servos. Find the status of all servos, then the servo will behaved by its current state. If you need, I can give you a source file.
      kumuking7年前 (2011-08-07)Reply
      • If You give me the source file, I'd be gladly appreciate your help and assistance. I'm very new to the arduino program. I bought the arduino last year but hadnt used it at all except playing around with LED lights. I checked out so many arduino books but all use computer as input or potentiometer, but no button inputs.
        LHLXD7年前 (2011-08-07)Reply
        • I've uploaded it here: http://www.2shared.com/file/hm0wGRW3/pushbutton_controls_2servos.html
          kumuking7年前 (2011-08-07)Reply
          • first off I want to thank you for the code and it was very helpful with my project. Please if you can email me a wiring diagram for this project it would be greatly appreciated :). Thank you once again and keep up the good work!!
            Joel Samuel5年前 (2014-01-22)
  3. Thanks. I have another question, Does it matter what push button to use? I have a momentary push button with two contacts only, not three. Will it still work? If yes, where do I hook it up to? Thanks again.
    LHLXD7年前 (2011-08-07)Reply
    • do you have a datasheet for this pushbutton?
      kumuking7年前 (2011-08-07)Reply
      • I have this momentary push button: http://www.radioshack.com/product/index.jsp?productId=2062539 I wonder if that works. or do I have to use these? http://www.radioshack.com/product/index.jsp?productId=3014504&numProdsPerPage=60
        LHLXD7年前 (2011-08-07)Reply
        • It's not used to control a servo. Usually, we use the momentary button to control power. When you build a project, you need a battery. And then you need it to switch on or off the power for the board.
          kumuking7年前 (2011-08-07)Reply
          • so you are trying to say that I need to get an on and off switch instead of a momentary switch? If so, Is this good for it? http://www.radioshack.com/product/index.jsp?productId=3118987&numProdsPerPage=60
            LHLXD7年前 (2011-08-07)
          • like this. Your choices are for power control. http://www.micro4you.com/store/arduino/arduino-module/arduino-digital-push-button.html
            kumuking7年前 (2011-08-07)
  4. will this one do? http://cgi.ebay.com/Arduino-Push-Button-v2-0-module-Sensor-Shield-/270686390287?pt=LH_DefaultDomain_0&hash=item3f062a880f#ht_3586wt_1165 I dont really know if the site you gave me is reliable or not. sorry to bother you with all these questions. and I thank you for helping me.
    LHLXD7年前 (2011-08-07)Reply
    • right. This one absolutly works.
      kumuking7年前 (2011-08-07)Reply
  5. alright, thank you so much for your help on this. I am going to buy this. once i get everything programmed, I hope you dont mind me asking you a few questions later on if I have any problems. Thanks again!
    LHLXD7年前 (2011-08-07)Reply
    • You are welcome.
      kumuking7年前 (2011-08-07)Reply
  6. Is this a clone Arduino MEGA 2560 R3 model or a legitimate board? I ask because I found one on Ebay for $24 and I think the "legitimate" one from Amazon.com is $55. Where did you buy your board from? Also, is that a "breadboard" that you plugged the switch into? Do you have a wiring diagram? Also, do I need a Sensor Shield? Sorry for all the questions but I'm new to this. Thanks, Mike
    Michael Pick7年前 (2012-05-09)Reply
    • I was in Vietnam, so I didn't see this comment until now. The first question: Yes, it's a Arduino MEGA 2560 board. The second question: This board was supplied by the lab. We had a very big project at that time. But any big project came with small ones. This is a part of small features. The third question: Yes, that's a breadboard. The forth question: I don't have a wiring diagram, since it's a really simple one. The fifth question: I don't think so. What I used are a Arduino MEGA 2560 board, a breadboard, a button sensor, and a servo, and some wires. It's fine to ask more if you are still confused on any point.
      kumuking7年前 (2012-05-15)Reply
  7. Thanks for getting back with me! I ordered my first Arduino UNO the other day so I look forward to playing with it! 🙂
    Michael Pick7年前 (2012-05-15)Reply
    • You are so welcome. I believe you will enjoy it.
      kumuking7年前 (2012-05-16)Reply
  8. Hi. Great stuff, I downloaded your pushbutton controls two servos file to run two servos but can't open the pde file. could you email it to me in a text version? I'm trying to open an iron man helmet with push one and close it with push two of the button. Thanks.
    Laura Disteldorf Rich6年前 (2012-12-07)Reply
    • Hi. You know what? I don't have your email address. That's the only problem. Huhhh.
      kumuking6年前 (2012-12-08)Reply
    • You can leave your email address here. I will send the file to you, and delete the comment including email address.
      kumuking6年前 (2012-12-08)Reply
  9. How would you change the speed of the servo?
    ceresskate6年前 (2013-05-15)Reply
    • The speed is controlled by the degree and the delay time. Code: for(pos = 0; pos < 180; pos=pos+1) and delay(15).
      kumuking6年前 (2013-05-15)Reply
  10. Oh ok thank you !! another weird question, I'm totally new to Arduino but i know of programming and such, but is there a way to use one arduino board and two servos and two buttons each controlling its represented servo?
    ceresskate6年前 (2013-05-21)Reply
    • Yes, it will be much easier to use two buttons to control two servos.
      kumuking6年前 (2013-05-21)Reply
  11. oh great ! do you know the code for that? cuz i was thinking of modifying your code and adding another button to control one servo, do you think thats possible?
    ceresskate6年前 (2013-05-21)Reply
    • you can name servo1 and servo2, and then attach them to different pins. Also you can attach two buttons to different pins. When button1 is pressed, the servo1 will turn. Code is very easy. You can modify my code for your purpose.
      kumuking6年前 (2013-05-21)Reply
  12. thank you sooo much ! after i am finished with my invention i will make sure to show you, you are so much help !
    ceresskate6年前 (2013-05-21)Reply
    • You are so welcome!
      kumuking6年前 (2013-05-21)Reply
  13. hi there when i try to control the servo with the pushbutton the code just makes the servo go in a sweep mode....please help! and when i hold the button it stops
    ceresskate6年前 (2013-05-28)Reply
    • Can you show me your code? You can send it in Contact page. Let me see your code first to find out what's going on.
      kumuking6年前 (2013-05-28)Reply
  14. Hi there, could you please send me the code aswell as the schematic drawing and breadboard layout for operating a servo with 1 push button. I have a arduino uno board and is just starting out, knowing very little bout electronics
    Lindsay5年前 (2013-10-02)Reply
    • Everything was finished almost three years ago. I am not sure if the documents are in my computer, but I'll try to find them out.
      kumuking5年前 (2013-10-02)Reply
      • is it possible you can forward me the revised code....I do not know what I Ann doing when it comes to arduino. A wiring diagram would be helpful too. I ordered all the pieces you say I need.thanks
        lou fernandez4年前 (2014-10-13)Reply
  15. thank you for sharing the code!, can i use it in a week project for my school? 🙂
    Blanca5年前 (2014-06-06)Reply
    • You are so welcome. And yes, you can use it for your project.
      kumuking5年前 (2014-06-07)Reply
  16. i wanted to animate a servo with push button and came across your code. i ordered everything i need to make it work. i know this is an old post but do you still have the revised code you talked about? i am totally lost when it comes to arduino. any help you can provide would be greatly appreciated. thanks
    lou4年前 (2014-10-13)Reply
  17. is it possible you can forward me the revised code….I do not know what I Ann doing when it comes to arduino. A wiring diagram would be helpful too. I ordered all the pieces you say I need.thanks
    lou fernandez4年前 (2014-10-15)Reply
  18. Hey there! Im working on a project similar to some of the other fellas on here, I would love the updated code, and diagram to be able to use the push-button for the servos. I tried the code that you posted and didnt have any luck. Let me know, Thanks!!
    Andrew4年前 (2014-10-26)Reply
  19. Exactly what I was looking for and for some reason was banging my head against the wall trying to sort it. Thank you!
    david c4年前 (2015-01-04)Reply
  20. Hi, looking at your video, your code is exactly what i am looking for. Please, oh please can you upload the wiring diagram showing how you wired up your project. I am brand new to Arduino and have no clue. I have an Arduino Uno R3. When I upload your code to the unit, it runs constantly, until i hold in the button, then it stops and wont run again, even if pressed. I would really appreciate all the help I can get. Once again, thanks for the example code and keep up the amazing work!
    Donovan4年前 (2015-02-28)Reply
    • Hi Donovan, I didn't do any arduino project for a long time. I am sorry that I cannot find the layout. But I believe your can find out how to connect by the code since the pin has been given. Thank you for your reply.
      kumuking4年前 (2015-03-04)Reply
  21. are you able to upload the codes in another website because the 'http://www.2shared.com/file/hm0wGRW3/pushbutton_controls_2servos.html' website has malware. or i can give you my email
    Tony3年前 (2015-12-18)Reply
    • 2 shared is a file share website. And actually you can just copy and paste the code from this post.
      kumuking3年前 (2016-02-16)Reply
  22. Hello dear friend. I need your help please. I want to do this: http://mechatrofice.blogspot.de/2015/07/servo-control-by-push-button.html I need that, but that the servo turns all the time 360°. So when you push one button, the servo turns as many times as needed clockwise. And the same when you push the other button it goes 360° anti-Clockwise. It will be a servo winch. and it will need to pull a fishingline. Hope you can help me. Regards
    SomosTres2年前 (2017-01-26)Reply
    • blogspot is blocked in China. I cannot figure out what is your demand.
      kumuking2年前 (2017-04-03)Reply