November 21, 2024, 10:15:28 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.


RC-2 reverse motor with random delaytime and random runtime

Started by tbelknap, February 15, 2017, 12:41:07 AM

Previous topic - Next topic

tbelknap

I have found myself lost within my own code. ugh. sorry.
I am using two RC-2 to control the forward and reverse motion of two motors.  Ideally I would like the prop's motion to be short (1-6seconds) and randomly moving in different directions. The delay between actions should be anywhere between 5 seconds - 2 minutes.

Here is the code I am currently writing.
' {$STAMP BS1}

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

SYMBOL MotorLeftForward = PIN0
SYMBOL MotorLeftBackward = PIN1
SYMBOL MotorRightForward = PIN3
SYMBOL MotorRightBackward = PIN4

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

SYMBOL IsOn = 1
SYMBOL IsOff = 0

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

SYMBOL timer = W1
SYMBOL delay = W2
SYMBOL delayshort = W3
SYMBOL  lottery = W4          ' random #

SYMBOL DELAY_MIN      = 1
SYMBOL DELAY_MAX      = 210

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

Power_Up:
  lottery = 1031            ' seed random #

Reset:
'         76543210
DIRS = %00001111            ' P4-P0 outs
PINS = %00000000            ' all outputs off

  PAUSE 1000                ' warm-up / re-trigger
' -----[ Program Code ]------------------------------------------------

Main:
  timer = 0                 ' reset debounce timer
  lottery = 0
Run_Delay:
  DEBUG "Running action delay", CR
  GOSUB Action_Delay            ' variable delay

Pick:
RANDOM lottery
delayshort = lottery // 6 + 1
DEBUG lottery
DEBUG "lottery pick"
IF delayshort = 1 THEN LeftMotorForward
IF delayshort = 2 THEN RightMotorForward
IF delayshort = 3 THEN BothMotorForward
IF delayshort = 4 THEN RightMotorReverse
IF delayshort = 5 THEN LeftMotorReverse
IF delayshort = 6 THEN BothMotorReverse

GOSUB Action_Delay

LeftMotorForward:
DEBUG "LMotorF"
MotorLeftForward = IsOn
RANDOM lottery                    'stir random generator
delayshort = lottery // 6 + 1     ' create delay, 1 to 5 seconds
delayshort = delayshort * 1000            ' convert to milliseconds
DEBUG "lottery"
PAUSE delayshort
MotorLeftForward = IsOff
GOTO Main

RightMotorForward:
DEBUG "RMotorF"
MotorRightForward = IsOn
RANDOM lottery                    'stir random generator
delayshort = lottery // 6 + 1     ' create delay, 1 to 5 seconds
delayshort = delayshort * 1000            ' convert to milliseconds
DEBUG "lottery"
PAUSE delayshort
MotorRightForward = IsOff
GOTO Main

BothMotorForward:
DEBUG "BMotorF"
MotorLeftForward = IsOn
MotorRightForward = IsOn
RANDOM lottery                    'stir random generator
delayshort = lottery // 6 + 1     ' create delay, 1 to 5 seconds
delayshort = delayshort * 1000            ' convert to milliseconds
DEBUG "lottery"
PAUSE delayshort
MotorLeftForward = IsOff
MotorRightForward = IsOff
GOTO Main

'RightMotorReverse:
'DEBUG "RMotorR"
'MotorLeftReverse = IsOn
RANDOM lottery ' stir random generator
delayshort = lottery // 6 + 1 ' create delay, 1 to 5 seconds
delayshort = delayshort * 1000 ' convert to milliseconds
PAUSE delayshort
MotorLeftReverse = IsOff
GOTO Main

LeftMotorReverse:
DEBUG "BMotorF"
MotorLeftReverse = IsOn
RANDOM lottery ' stir random generator
delayshort = lottery // 6 + 1 ' create delay, 1 to 5 seconds
delayshort = delayshort * 1000 ' convert to milliseconds
PAUSE delayshort
MotorLeftReverse - IsOff
GOTO Main

BothMotorReverse:
DEBUG "BMotorR"
MotorLeftReverse = IsOn
MotorRightReverse = IsOn
RANDOM lottery ' stir random generator
delayshort = lottery // 6 + 1 ' create delay, 1 to 5 seconds
'timershort = timershort * 1000 ' convert to milliseconds
PAUSE timershort
MotorLeftReverse = IsOff
MotorRightReverse = IsOff
GOTO Main

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

Action_Delay:
  RANDOM lottery                                ' re-stir
  delay = DELAY_MAX - DELAY_MIN + 1             ' get span
  delay = lottery // delay + DELAY_MIN          ' randomized seconds

  DEBUG delay, CR

Delay_1S:
  PAUSE 1000                                    ' delay 1 second
  delay = delay - 1
  IF delay > 0 THEN Delay_1S                    ' repeat if not done
    GOTO Pick

' -----[ User Data ]-------------------------------------------------------

Bit_Masks:
  EEPROM  (%00000001, %00000010, %00000100, %00001000)

JonnyMac

Sometimes we can get so verbose -- trying to help ourselves and others -- that we can tangle ourselves; kind of like a politician who tries to squeeze too many fancy words into a sentence and it turns into gibberish.

After untangling your program it consumes about 91% of the program space. Once I understood your process, I wrote a leaner version (no DEBUG statements required) that does the same thing -- we can do this because it's easy to collapse redundant code (always look for those opportunities). It may take a moment to grasp this, but take the time and I think you'll be happy. I changed your IOs just a bit.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  MtrRRev         = PIN3
SYMBOL  MtrRFwd         = PIN2

SYMBOL  MtrLRev         = PIN1
SYMBOL  MtrLFwd         = PIN0


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

SYMBOL  L_Fwd           = %00000001
SYMBOL  L_Rev           = %00000010
SYMBOL  R_Fwd           = %00000100
SYMBOL  R_Rev           = %00001000
SYMBOL  B_Fwd           = %00000101             ' both foward
SYMBOL  B_Rev           = %00001010             ' both reverse

SYMBOL  All_Stop        = %00000000

SYMBOL Delay_Min        = 5
SYMBOL Delay_Max        = 120

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  IsOn            = 1                     ' active-high I/O
SYMBOL  IsOff           = 0


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

SYMBOL  timer           = B2                    ' for debounce loop
SYMBOL  move            = B3                    ' move to make
SYMBOL  last            = B4                    ' last move

SYMBOL  lottery         = W5
SYMBOL   lottoLo        =  B10
SYMBOL   lottoHi        =  B11


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

Power_Up:
  ' put code here that only happens at power-up/hard reset

Reset:
  PINS = All_Stop
  DIRS = %00001111                              ' P3..P0 are outputs

  last = -1                                     ' allow any move to start
  READ 0, lottoLo
  READ 1, lottoHi


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

Main:
  RANDOM lottery
  timer = Delay_Max - Delay_Min + 1             ' span of range
  timer = lottery // timer + Delay_Min          ' 5..120
  GOSUB Delay_Secs

Get_Move:
  RANDOM lottery
  move = lottery // 6
  IF move = last THEN Get_Move                  ' no repeats!
    last = move                                 ' save for next time

Run_Motors:
  LOOKUP move, (L_Fwd, R_Fwd, B_Fwd, L_Rev, R_Rev, B_Rev), PINS

  RANDOM lottery
  timer = lottery // 6 + 1                      ' random, 1..6
  GOSUB Delay_Secs

  PINS = All_Stop

  WRITE 0, lottoLo
  WRITE 1, lottoHi

  GOTO Main


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

' Delay in seconds
' -- but delay in "timer" before calling

Delay_Secs:
  IF timer = 0 THEN DS_Exit
    PAUSE 1000
    timer = timer - 1
    GOTO Delay_Secs

DS_Exit:
  RETURN


' -----[ User Data ]-------------------------------------------------------

Lotto_Start:
  EEPROM (02, 14)
Jon McPhalen
EFX-TEK Hollywood Office