Shop OBEX P1 Docs P2 Docs Learn Events
Simple Servo Control Question - Need to write simple subroutine — Parallax Forums

Simple Servo Control Question - Need to write simple subroutine

BotleRocketWarBotleRocketWar Posts: 1
edited 2010-05-04 15:45 in Robotics
Hey Guys,

I'm working on a small project and am having some issues with servo control.

I was hoping I could get some help with two small subroutines. I'm using the Basic Stamp 2 and the Board of Education. I am wanting to·create a subroutine that will, from neutral, rotate the servo approximately 30 degrees for 1 second, and then return to neutral.

Then I need another subroutine that will, also from neutral, rotate the servo in the OPPOSITE direction approximately 30 degrees for 1 second and then return to neutral.

This will be used to control the control surface of a small aircraft for the purpose of demonstrating a simple auto-pilot based on a few photoresistors.

Thanks!

Comments

  • ZootZoot Posts: 2,227
    edited 2010-05-04 12:36
    
    servo PIN 1 ' or whatever pin
    servoPulse VAR Word
    servoCount VAR Word
    
    ctr CON 750  ' may need to adjust for particular servo
    neg30 CON 580 ' ditto
    pos30 CON 920 ' ditto
    waitTime CON 1000 ' in milliseconds
    
    Main:
    
    IF someThing = someCondition THEN
       servoPulse = neg30
       GOSUB doServo
    ELSEIF someThing = someOtherCondition THEN
       servoPulse = pos30
       GOSUB doServo
    ELSE
       servoPulse = ctr
       GOSUB servoPulse
    ENDIF
    
    GOTO Main
    
    doServo:
       servoCount = waitTime / 20
       DO
         GOSUB servoPulse
         servoCount = servoCount - 1
       LOOP UNTIL servoCount = 0
       RETURN
    
    servoPulse:
        PULSOUT servo, servoPulse
        PAUSE 20
       RETURN
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2010-05-04 15:45
    Hi BottleRocketWar,

    If you haven't already, also take a look at Chapter 4 in What's a Microcontroller. It provides an in-depth explanation of the standard servo, and how to control it using the BASIC Stamp 2.

    Best of luck, and let us know how it turns out!

    -- Jessica

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jessica Uelmen
    Education Department
    Parallax Inc.
Sign In or Register to comment.