basic stamp 2
navychief
Posts: 40
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
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.
' {$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
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?
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.
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.