Shop OBEX P1 Docs P2 Docs Learn Events
Help controlling servos with buttons — Parallax Forums

Help controlling servos with buttons

kingspudkingspud Posts: 128
edited 2005-06-09 21:01 in Robotics
Hi All,

Does anyone have any code that I can use with a BOE, PSC board, and BS2px that would control mutilple servos with buttons?

I need to be able to move different·servos based on the condition of·different buttons making contact.· If a normally open button is pressed then the servo would change direction.

So as an example:· I would have 4 servos and·4 different buttons and when a buton is pressed a corresponding servo would change direction.

If you have this code or can help me with it I would really appreciate it.

Thank to all in advance

Joe

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-05-25 18:36
    Joe,

    ·· I would recommend reading the "What's A Microcontroller?" text, a free download in PDF format from our website.· It will explain servo control, reading buttons, etc.· Then you can learn how to do the specifics.· A link to this file is posted below.

    http://www.parallax.com/dl/docs/books/edu/wamv2_2.pdf



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-25 18:40
    I just happened to have a couple servos connected to my trusty PDB, so I wrote the attached program.·· The program, along with the material Chris suggested should get you going.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • kingspudkingspud Posts: 128
    edited 2005-05-25 22:27
    Thanks guys, I will read the doc and try out the program!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-26 00:28
    One thing of note: I was testing this on the PDB which uses active-low buttons.· If your buttons are active-high, remove the tilde in the code highlighted below.

    ' Scan and debounce button inputs.· Buttons that have changed
    ' from 0 to 1 are reported in 'xBtns'

    Get_Buttons:
    · nBtns = %1111································ ' enable all four inputs
    · FOR idx = 1 TO 10
    ··· nBtns = nBtns & ~BtnPort··················· ' test inputs
    ··· PAUSE 5···································· ' delay between tests
    · NEXT
    · xBtns = nBtns ^ oBtns & nBtns················ ' find changes, 0 -> 1
    · oBtns = nBtns
    · RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • edited 2005-05-26 00:57
    Joe,

    There is an example of controlling a servo with pushbuttons in What's a Microcontroller (Chapter 4, Activity #4). It's got a nice little explanation along with schematic, wiring diagrams, and a simple example program.· What's a Microcontroller is available for free download from www.parallax.com -> Downloads -> Stamps in Class Tutorials.

    Regards, Andy

    Post Edited (Andy Lindsay (Parallax)) : 5/26/2005 1:04:05 AM GMT
  • edited 2005-05-26 01:54
    Whoa, sorry; I missed the part about PSC board on the first read. Taking the example in What's a Microcontroller, you'll have to replace the PULSOUT, PAUSE, and DEBUG commands with some code to adjust the value of the duration variable for the PSC, then a SEROUT command to send that value to the PSC.·

    Okay, and then there's the part about multiple servos.· Hmmm, it doesn't look like the example from What's a Microcontroller really applies here.· Better stick with Jon's code; it looks like that's the way to go.

    Post Edited (Andy Lindsay (Parallax)) : 5/26/2005 2:02:35 AM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-26 02:28
    I missed the PSC thing too -- sorry about that.· The program attached to this post is an update of the original that would use the PSC and be running on the BS2px.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • kingspudkingspud Posts: 128
    edited 2005-05-26 05:22
    Jon,

    You are a Basic Stamp GOD!!!!!

    The program worked perfect!!

    When I push button 1 then servo 1 goes all the way right and if I push it again it goes all the way to the left, etc...
    This is so perfect!

    I love it!

    Once I go through the code I should be able to tweek it to my needs!

    The only question I would have is I'm trying to write a program that will work as follows: While a servo is moving possibly back and forth exploring... if a button or contacts are hit then the servo would stop or change direction and/or another servo would move a little bit and the first servo would continue to move back and forth exploring again. So....

    "Since the buttons are Normally Open is this program tweekable so that wile the servos are moving
    slowly in one direction a button hit would make then either stop or continue in the opposite direction?"

    If you know the tweek or can explain it then I would GREATLY appreciate it.

    Thank you soooooo much for this program, it helps out a bunch!

    Joe
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-26 13:36
    Thanks, Joe, but really, I'm just goofy guy who bought a BASIC Stamp 11 years ago and have been working with them every day since.· Along the way I've accumulated a big bag of tricks (part of that code [noparse][[/noparse]detecting which button went from 0->1]·is from my pal, Dr. Tracy Allen).

    I'm not sure about what you're after ... are you wanting one servo to scan until another button is pressed at which the original servo would stop and the new servo would start scanning?· It's a little more complicated but could be done.· I'll help out with code if you confirm what you want, even going so far as to spelling out your logic in gut-simple steps.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • kingspudkingspud Posts: 128
    edited 2005-05-26 16:31
    Hi Jon,

    Basically I am trying to create an autonomous robot that can navigate up a wall which has holes drilled in it in random locations. So... the robot would have two arms with an elbow and shoulder joints like human arms, three servos per arm. Joints would be 1 servo for the elbow and 2 for the shoulder. The program would run as follows:

    1. Set robot in starting location on wall

    1. The right arm would raise out of the hole and reach up as high it is could and start its "motion tracking".

    2. "Motion Tracking" - Forearm on right arm "#1 servo" would track back and forth the full 180 degree, once the full left to right is made without finding a hole the second shoulder servo "#2 servo" would move the arm down a little and the process would start again. This process would continue till either a hole is found or the full downward motions is made.

    2. If no hole were found then the left arm would go back to its original starting location and the process would go to the left arm!

    3. If a hole were found than a contact would be made "#3 servo" would rotate the arm down and lock in the hole and the right arm would station itself in the hole for support, then the process would continue with the left arm.

    So...

    1. Start location - both arms set in start holes.
    2. The right arm pulls out of starting hole "#3 servo"
    3. The right arm reaches up as high as possible "#1 and #2 servos"
    4. The right forearm tracks back and forth "#1 servo"
    5. NO HOLE – lower shoulder - "#2 servo" go to 4.
    6. Continue process till hole is found
    7. No Hole Found at all – Lock right arm in original location – Left arm tracks now
    7. HOLE FOUND - lower shoulder "#3 servo" - lock arm in hole
    8. Left arm now runs from line 2.
    9. The robot tries to make its way up the wall to final location.

    All the contact or button responses are based on a button on the end of the arm under the part that fits in the hole. This button runs along the wall making constant contact "NORMALLY CLOSED", if the hole is reached the contact is "OPEN"! When the contact opens then the arm know to lower and lock in hole! Once one arm lock than the other arm raises and continues searching for holes! Etc…

    I hope this explains what I am trying to do, if I missed something let me know.

    Thanks for all your help!

    Joe
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-26 17:10
    Wow, that's a very ambitious project and certainly more than I can bang-out in an afternoon. Since you've now got working code for servo control with buttons, and a clear idea of what you want to do ... go to it. Of course, we'll be here to support you when the going gets tough. Just know that this is a big project (in my opinion), and may take you a week or two of coding to get it right. Be patient ... and have fun!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Steve-OSteve-O Posts: 8
    edited 2005-06-08 06:02
    Jon,
    I'm a little lost as to the circuitry of the program you wrote. I'm using a NX-1000 board and was just using one of the 8 push buttons at the bottom of the board. Any pointers would be greatly appreciated!

    By the way, in case you were curious...

    I am currently working on a sectional, retractable arm. My goal is to be able to press a button, have the servos raise the arm, press the button again and have them lower the arm. So far the servos can support the arm, but I have a feeling I'll eventually have to use something stronger.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Steve Reust

    Wichita, KS
    BS2
    NX-1000 Project Board
    Parallax Servo Controller
    Motor Mind C coming soon

    Post Edited (Steve-O) : 6/8/2005 6:07:23 AM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-08 12:38
    There's nothing to it: the buttons on the NX-1000 are pulled up, hence go low when pressed. All you have to do is connect the buttons to the BASIC Stamp with hook-up wire, and understand that the Stamp will read "0" when a button is pressed. This is easy to fix with PBASIC by using the ~ (invert) operator.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • kingspudkingspud Posts: 128
    edited 2005-06-09 21:01
    Hi Guys,

    I have a continuation to Steve-O's question... I to have made a servo controlled arm where the joints are the servos and I need the same funtionality with the buttons. BUT... I wrote a program loop that has the servos moving back and forth waiting for a button push but when the button is pushed it seems as if the program dosen't acknowledge the pushed button! I would like to see it work as follows:

    1. Arm moves back and forth with a button in the off state but when the button is pushed the servo will stop and another servo will move till button is pushed again!

    This is rather a test program for more of what I need but this would help me out greatly if you could show me the how the code you sent would include these button ckeckers.

    "Servo_Buttons2.BPX"

    I understand the above program code but can't seem to get it to work with the opposite of how it works now. Right now the button controls a motionless servo and moves it to an extreme. I would like the button to be in a constant check mode and when pressed it stops the servo and starts another.

    Thanks

    Joe
Sign In or Register to comment.