November 16, 2024, 01:44:47 AM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Random 'snap' motion project

Started by astrodoc71, September 02, 2009, 03:12:25 AM

Previous topic - Next topic

astrodoc71

Hi group,
I want to make a servo operate a prop rat's tail. I want the
tail to appear to randomly "snap" back and forth as if the rat is
agitated. Here's the details:

  * I have one servo hooked to P0
  * I want the servo to move to a random position
    between 45 degrees and 135 degrees
  * I want the delay between movements to be random from .5
    seconds to 2.5 seconds
  * It needs to run continuously when powered up
I'm a complete newbie at BS1 programming so any help would be appreciated
Thanks!
Dave

JonnyMac

September 02, 2009, 08:45:04 AM #1 Last Edit: September 02, 2009, 04:39:17 PM by JonnyMac
Here you go, this will give you something to learn from.  Important notes when using servos:

1) The pin(s) used needs to be made an output and low to start with; use LOW pin# or PINS and DIRS (as I did)
2) Servos should be refreshed every 20ms to hold position.  This program creates long delays and updates the servo as required with a special subroutine called Servo_Pause.

' =========================================================================
'
'   File...... Rat_Tail.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2009 EFX-TEK
'              Some Rights Reserved
'              -- see http://creativecommons.org/licenses/by/3.0/
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated... 02 SEP 2009
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Servo           = 0                     ' tail servo


' -----[ Constants ]-------------------------------------------------------

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  pos             = B2
SYMBOL  timer           = B3

SYMBOL  lottery         = W5                    ' random value


' -----[ Initialization ]--------------------------------------------------

Reset:
 PINS = %00000000                              ' clear all
 DIRS = %00000001                              ' make P0 an output

 pos = 150                                     ' start at center
 timer = 100
 GOSUB Servo_Pause


' -----[ Program Code ]----------------------------------------------------

Main:
 RANDOM lottery                                ' stir random #
 pos = lottery // 101 + 100                    ' randomize, 100 to 200
 RANDOM lottery
 timer = lottery // 2001 + 500                 ' random, 500 to 2500
 GOSUB Servo_Pause                             ' move servo and hold
 GOTO Main


' -----[ Subroutines ]-----------------------------------------------------

' Load timing delay into "timer" before calling
' -- "timer" is destroyed by routine so it must be reset each call

Servo_Pause:
 PULSOUT Servo, pos                            ' refresh servo
 IF timer < 20 THEN SP_Exit
   PAUSE 19                                    ' delay (minus servo pulse)
   timer = timer - 20                          ' update timer
   GOTO Servo_Pause

SP_Exit:
 RETURN

' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

astrodoc71

Spooktacular! I really appreciate it! I'll post a video of the project when it's done.
Thanks again,
Dave