Shop OBEX P1 Docs P2 Docs Learn Events
2 push buttons on a servo — Parallax Forums

2 push buttons on a servo

Ive been trying to get a servo to operate using a push button to move it in the clockwise direction and a separate one to operate in the counter clockwise direction. the trick is that each time one of the buttons is pressed the servo will move 20 degrees and STOP, relative to the button pressed. there needs to be a total of 8 positions. Any help would be greatly appreciated.

Comments

  • I suggest using a stepper motor instead of a servo.
  • Needs to be a servo. School project
  • Welcome to the forums, mcm1822.

    As a general rule, we typically won't provide full code examples for students working on school assignments.

    If you provide what code you've written so far and tell us where you are stuck then someone may be able to give you tips or assistance to get you moving in the right direction.
  • It's not for grade but I have 2 codes that are getting me in the right direction

    First one:

    Duration VAR Word
    Duration = 750
    PAUSE 1000

    DO
    IF IN = 1 THEN
    IF duration > 500 THEN
    duration = duration - 25
    ENDIF
    ENDIF

    IF IN4 = 1 THEN
    IF duration < 1000 THEN
    duration = duration + 25
    END IF
    ENDIF

    PULLOUT 14, duration
    Pause 10
    Loop

    This causes the servo to respond to the push buttons but they don't stop at any particular point.


  • My second code causes the servo to rotate in increments but will not utilize push buttons and only rotates one direction
  • Your first code doesn't stop because it keeps seeing the button pressed. You cannot press and release a button in so quickly. Set a flag when you first see a button and then wait for it to be released.
  • GenetixGenetix Posts: 1,740
    edited 2016-12-15 21:34
    Hello mcm1822,

    Your program has a few errors that F7 (Check Syntax) will find.
    1.) In the first IF...THEN, the IN is missing the pin number.
    2.) The 2nd to last ENDIF is a one word command, not 2 separate words.
    3.) The servo command is PulsOut, not PullOut.

    A DO...LOOP will run forever until you remove the power or download another program.

    Also, use the C icon on the posting toolbar to attach code so the formatting isn't mangled.

  • ercoerco Posts: 20,244
    What a great problem! How are you coming, mcm1822? Can you show a video of your setup in action?
  • Genetix wrote: »
    Hello mcm1822,

    Your program has a few errors that F7 (Check Syntax) will find.

    Since he described what the program did, I just assumed he had some typos when he entered it into the forum comment.

    mcm, you can copy and paste your program. Stick it between the code markers you get when you press the C button on the format bar.
  • If you haven't already, you should read through Chapter #4, Activity #6 in What's A Microcontroller? for an overview of controlling servo positions with a pushbutton.
Sign In or Register to comment.