Shop OBEX P1 Docs P2 Docs Learn Events
basic stamp 2 — Parallax Forums

basic stamp 2

navychiefnavychief Posts: 40
edited 2011-03-13 09:34 in General Discussion
Need help with bs2 codes to rotate servo clockwise after button is pressed then blink led then servo to rotate ccw after releasing button then blink led 2. Thank you

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-12 20:14
    Several tutorials will be useful:
    1) "What's a Microcontroller?"
    This will show you how to handle buttons and leds
    2) "Robotics with the BoeBot".
    This will show you how to handle servos
    3) You'll also need "Basic Stamp Syntax and Reference Manual"
    This will show you basic programming concepts, particularly for the Basic Stamps.
    It also explains the various statements and includes examples of their use.

    All of these come with the Stamp Editor and are separately downloadable from Parallax's Downloads webpage
    here and here.

    If you have specific questions, please give details and include a listing of your program (if any). You can attach files to messages by clicking on the "Go Advanced" button. That'll bring up a more complex reply window that includes an Attachment Manager.
  • navychiefnavychief Posts: 40
    edited 2011-03-12 20:59
    This is what I have so far, I'm not sure for the codes to have the led on P5 to blink when servo is fully clockwise and led on P4 to blink when servo is fully ccw:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    duration VAR Word
    duration = 750
    PAUSE 1000

    DO
    IF IN0 = 0 THEN
    IF duration > 300 THEN
    duration = duration - 10
    ENDIF
    ENDIF

    IF IN0 = 1 THEN
    IF duration <1000 THEN
    duration = duration + 10
    ENDIF
    ENDIF

    PULSOUT 13, duration
    PAUSE 10

    DEBUG HOME, DEC4 duration, "=duration"

    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-12 21:20
    You're starting with writing code before you've defined what you want to do. So far, you've said that you have one button, one servo, and two leds. How do you have the button hooked up? How do you have the leds hooked up? I assume the servo is wired to some I/O pin and the power supply leads are connected how?

    After you've told us that, how do you want everything to behave? Define carefully what should happen when the button is pushed, when it's released, etc. Define carefully when the leds should turn on. If you want them to blink, then how? How many times? How quickly? How much time on and time off? What does the servo do? How does it behave? You mention "fully clockwise" and "fully ccw". What do you mean by that? Servos have mechanical stops. Are those the limits you're referring to? There is no sensor on the servo. How would your program know what position it's in?
  • navychiefnavychief Posts: 40
    edited 2011-03-12 21:37
    LED 1 active high on P9
    LED2 active high on P8
    PB active high on P0
    Servo on P13

    Program the controller to loop continuously and perform the following:
    -When PB is not pressed, the servo will rotate to fully CW and LED2 will blink at a rate of 4Hz once the servo is at position. Show on the screen "CW"

    -When the PB is pressed, the servo will rotate to fully CCW and LED1 will blink at a rate of 1 Hz once the servo is positioned. Show on the screen "CCW"

    Thank you for the help. Attached is the picture of setup.
    1024 x 765 - 148K
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-13 09:19
    Some thoughts ...
    When you command a servo to move to a particular position, you just provide an appropriate width pulse and the servo will move to that position. You don't have to provide intermediate positions unless you want the servo to move slowly, step by step, to that final position. You do have to allow enough time for the servo to move and you do have to continue to provide pulses about every 20ms.

    You've got two portions to your program. The first is when the PB has just been released and consists of 3 pieces ...
    1) Rotate the servo to a CW extreme position
    2) Show "CW" on the Stamp Editor's debug window
    3) Toggle LED2 once every 125ms (Cycle on - off - on at 4Hz) checking for the state of the PB each cycle.
    ... Continuing the toggling if the PB is still released. Going to portion 2 if the PB is pressed

    Portion 2 is similar with different details of what's to be done. You should have enough information between this and
    the examples in the tutorials to do what you need.
  • navychiefnavychief Posts: 40
    edited 2011-03-13 09:34
    Thank you very much for the insight
Sign In or Register to comment.