November 24, 2024, 04:50:41 PM

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.


LED as Beacons and Servos with LEDs as searchlights

Started by dacostasr, March 26, 2013, 01:30:23 PM

Previous topic - Next topic

dacostasr

I'm cannabilizing some code that I used for turning doll heads in a halloween prop to my other hobby.

I got the servos to work fine and the searchlights come on at the same time and turn off fine...when the servos quit...but I want PINS 4, 5, and 6 to flash constantly (like an airplane beacon)

I've tried all sorts of "odd" things but can't figure it out...here is what I'm using:

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


' -----[ Program Description ]---------------------------------------------
'
' Trigger :: Parallax-compatible PIR or N.O. button (mat switch, etc.)
'            -- connect N.O. button between P6.W and P6.R


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


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

SYMBOL  Trigger         = PIN7                  ' SETUP = DN
SYMBOL  LED5            = 6
SYMBOL  LED4            = 5
SYMBOL  LED3            = 4
SYMBOL  LED2            = 3
SYMBOL  Servo3          = 2
SYMBOL  Servo2          = 1
SYMBOL  LED1            = 0




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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0



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

SYMBOL  pos2            = B2
SYMBOL  dest2           = B3
SYMBOL  pos3            = B4
SYMBOL  dest3           = B5
SYMBOL  temp            = B6
SYMBOL  timer           = W4
SYMBOL  lottery         = W5

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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00011111                              ' set output pins

  pos2 = 150 : dest2 = 150
  pos3 = 150 : dest3 = 150

  timer = 0

Program_Delay:

  GOSUB Update_Servos
  timer = timer + 20
  IF timer < 300 THEN Program_Delay



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

Main:

timer = 0                                     ' reset timer

Check_Trigger:

  RANDOM lottery                                ' stir random value
  GOSUB Update_Servos                           ' refresh current positions

  timer = timer + 20 * Trigger                  ' update timer
  IF timer < 200 THEN Check_Trigger             ' wait for 0.2 sec input

  timer = 0

Prop_Loop:

  GOSUB Update_Servos
  GOSUB Update_Servos


Check2:
  RANDOM lottery
  IF pos2 <> dest2 THEN Move2
    dest2 = lottery // 101 + 100
    dest2 = dest2 / 3 * 3
    GOTO Check3

Move2:
  IF pos2 > dest2 THEN Move2_CCW
    pos2 = pos2 + 6

Move2_CCW:
  pos2 = pos2 - 3

Check3:
  RANDOM lottery
  IF pos3 <> dest3 THEN Move3
    dest3 = lottery // 101 + 100
    dest3 = dest3 / 3 * 3
    GOTO Check_Timer

Move3:
  IF pos3 > dest3 THEN Move3_CCW
    pos3 = pos3 + 6

Move3_CCW:
  pos3 = pos3 - 3

HIGH LED1
HIGH LED2

Check_Timer:
  timer = timer + 1                             ' add 40ms loop
  IF timer < 300 THEN Prop_Loop                ' run 2 minutes?


  GOTO Reset


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

Update_Servos:


  PULSOUT Servo2, pos2
  PULSOUT Servo3, pos3
  PAUSE 15

RETURN

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


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

Thanks in advance!!

Dennis

JonnyMac

So you just want the LEDs to toggle on and off? Same on and off timing? Both LEDs do the same thing?

What you want to do is tricky, but can be done -- I'm converting it to a state machine now.
Jon McPhalen
EFX-TEK Hollywood Office

dacostasr

The ones on 0 and 3 are fine...they turn on when the servos come on and look like searchlights...4-6 i want to be constant beacons flashing...yes toggle on and off same timing.

Thanks JonnyMac!!

Dennis

JonnyMac

March 26, 2013, 02:43:43 PM #3 Last Edit: March 26, 2013, 08:18:30 PM by JonnyMac
Give this a try. Do note that I changed a couple of your IO definitions!

This program is somewhat advanced -- spend a bit of time with it and it will make sense.  The idea behind this design is that the stuff that always has to happen (refresh servos, update global timer, randomize a value, etc.) is at Main and then a 'state' variable jumps to the section of code that should run now.

In order to accomodate long times each value in 'timer' represents ~20ms -- the length of time to execute a program loop.

This code is fixed and runs! Have fun!

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


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


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


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

SYMBOL  Trigger         = PIN7                  ' SETUP = DN

SYMBOL  LED5            = 6                     ' flashers
SYMBOL  LED4            = 5
SYMBOL  LED3            = 4

SYMBOL  LED2            = 3
SYMBOL  Servo2          = 2

SYMBOL  LED1            = 1
SYMBOL  Servo1          = 0


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

SYMBOL  IS_ON           = 1                     ' for active-high in/out
SYMBOL  IS_OFF          = 0

SYMBOL  YES             = 1
SYMBOL  NO              = 0

SYMBOL  SERVO_TICK      = 2                     ' affects speed


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

SYMBOL  state           = B2                    ' program state
SYMBOL  flasher         = B3                    ' flash timing control

SYMBOL  pos1            = B4                    ' servo 1 position
SYMBOL  dest1           = B5                    ' servo 1 destination

SYMBOL  pos2            = B6                    ' servo 2 position
SYMBOL  dest2           = B7                    ' servo 2 destination

SYMBOL  timer           = W4                    ' timing in 20ms units
SYMBOL  lottery         = W5                    ' random #


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %01111111                              ' set output pins

  pos1 = 150 : dest1 = 150
  pos2 = 150 : dest2 = 150

  timer = 0
  state = 0


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

Main:
  PULSOUT Servo1, pos1                          ' update servos
  PULSOUT Servo2, pos2
  PAUSE 8                                       ' base pad for servo loop
  timer = timer + 1                             ' update loop timer
  RANDOM lottery                                ' stir random #


Handle_State:
  BRANCH state, (Pgm_Hold, Trigger_Hold, Run_Servos)


Pgm_Hold:
  PAUSE 9                                       ' pad for short loop
  IF timer < 500 THEN Main                      ' 500 x 20ms = 10s
    state = 1                                   ' done, advance state
    timer = 0                                   ' reset timer


Trigger_Hold:
  PAUSE 9                                       ' pad for short loop
  timer = timer * Trigger                       ' check input
  IF timer < 5 THEN Main                        ' 5 x 20ms = 100ms
    PINS = PINS | %00001010                     ' searchlights on
    state = 2
    timer = 0


Run_Servos:
  IF timer >= 6000 THEN Reset                   ' 6000 x 20ms = 2m


Check_Flash:
  flasher = flasher + 1
  IF flasher < 10 THEN Run_Servo1               ' 5 x 20ms = 200ms
    PINS = PINS ^ %01110000                     ' togge P6, P5, P4
    flasher = 0                                 ' reset count


Run_Servo1:
  RANDOM lottery                                ' re-stir random #
  IF pos1 <> dest1 THEN Move1_CW                ' skip if not at destination
    dest1 = lottery // 101 + 100                '  set 100-200
    dest1 = dest1 / SERVO_TICK * SERVO_TICK     '  make divisible by speed
    GOTO Run_Servo2                             '  next servo

Move1_CW:
  IF pos1 > dest1 THEN Move1_CCW                ' corect direction?
    pos1 = pos1 + SERVO_TICK
    GOTO Run_Servo2

Move1_CCW:
  pos1 = pos1 - SERVO_TICK


Run_Servo2:
  RANDOM lottery
  IF pos2 <> dest2 THEN Move2_CW
    dest2 = lottery // 101 + 100
    dest2 = dest2 / SERVO_TICK * SERVO_TICK
    GOTO Main

Move2_CW:
  IF pos2 > dest2 THEN Move2_CCW
    pos2 = pos2 + SERVO_TICK
    GOTO Main

Move2_CCW:
  pos2 = pos2 - SERVO_TICK
  GOTO Main
Jon McPhalen
EFX-TEK Hollywood Office

dacostasr

March 26, 2013, 05:14:26 PM #4 Last Edit: March 26, 2013, 05:29:47 PM by dacostasr
It didn't work out...

One spotlight came on...but no servo movement...the other spotlight came on seconds later with the servo but moved once and stopped, but the light stayed on.  Nothing happened on the beacon side.
-----------
Playing with the IO I got the second searchlight to come on...but no servo action.  The other servo is moving with the light on...won't stop....Beacons, nothing.  I'm still playing with the code...



dacostasr

March 26, 2013, 05:36:09 PM #5 Last Edit: March 26, 2013, 05:57:17 PM by dacostasr
I got the second servo working...so far so good.  Now I'm concentrating on the beacons...


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


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


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


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

SYMBOL  Trigger         = PIN7                  ' SETUP = DN

SYMBOL  LED5            = 6                     ' flashers
SYMBOL  LED4            = 5
SYMBOL  LED3            = 4

SYMBOL  LED2            = 3
SYMBOL  Servo2          = 2

SYMBOL  Servo1          = 1
SYMBOL  LED1            = 0


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

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

SYMBOL  YES             = 1
SYMBOL  NO              = 0

SYMBOL  SERVO_TICK      = 1                     ' affects speed


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

SYMBOL  state           = B2                    ' program state
SYMBOL  flasher         = B3                    ' flash timing control

SYMBOL  pos1            = B4                    ' servo 1 position
SYMBOL  dest1           = B5                    ' servo 1 destination

SYMBOL  pos2            = B6                    ' servo 2 position
SYMBOL  dest2           = B7                    ' servo 2 destination

SYMBOL  timer           = W4                    ' timing in 20ms units
SYMBOL  lottery         = W5                    ' random #


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

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %01111111                              ' set output pins

  pos1 = 150 : dest1 = 150
  pos2 = 150 : dest2 = 150

  timer = 0
  state = 0


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

Main:
  PULSOUT Servo1, pos1                          ' update servos
  PULSOUT Servo2, pos2
  PAUSE 17
  timer = timer + 1                             ' update loop timer
  RANDOM lottery                                ' stir random #


Handle_State:
  BRANCH state, (Pgm_Hold, Trigger_Hold, Run_Servo1)


Pgm_Hold:
  IF timer < 500 THEN Main                      ' 500 x 20ms = 10s
    state = 1                                   ' done, advance state
    timer = 0                                   ' reset timer


Trigger_Hold:
  timer = timer * Trigger                       ' check input
  IF timer < 5 THEN Main                        ' 5 x 20ms = 100ms
    PINS = PINS | %00001001                     ' searchlights on
    state = 2
    timer = 0


Run_Servos:
  IF timer >= 60 THEN Reset                   ' 6000 x 20ms = 2m


Check_Flash:
  IF flasher < 10 THEN Handle_State             ' 5 x 20ms = 200ms
    PINS = PINS ^ %01110000
    flasher = 0                                 ' reset count


Run_Servo1:
  RANDOM lottery                                ' re-stir random #
  IF pos1 <> dest1 THEN Move1_CW                ' skip if not at destination
    dest1 = lottery // 101 + 100                '  set 100-200
    dest1 = dest1 / SERVO_TICK * SERVO_TICK     '  make divisible by speed
    GOTO Run_Servo2                             '  next servo

Move1_CW:
  IF pos1 > dest1 THEN Move1_CCW                ' corect direction?
    pos1 = pos1 + SERVO_TICK
    GOTO Run_Servo2

Move1_CCW:
  pos1 = pos1 - SERVO_TICK


Run_Servo2:
  RANDOM lottery
  IF pos2 <> dest2 THEN Move2_CW
    dest2 = lottery // 101 + 100
    dest2 = dest2 / SERVO_TICK * SERVO_TICK
    GOTO Main

Move2_CW:
  IF pos2 > dest2 THEN Move2_CCW
    pos2 = pos2 + SERVO_TICK
    GOTO Main

Move2_CCW:
  pos2 = pos2 - SERVO_TICK
  GOTO Main


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

JonnyMac

Sorry, my fault for posting code that I didn't have time to run on actual hardware -- goes to show me for thinking it wouldn't be too hard.

Actually, this is a fairly sophisticated program, and shows the power of PBASIC1 when you understand programming structures and the features of the language.

I've re-posted working code above.
Jon McPhalen
EFX-TEK Hollywood Office

dacostasr

March 27, 2013, 04:22:14 AM #7 Last Edit: March 27, 2013, 04:45:58 AM by dacostasr
 :) I can't complain...you're always willing to help...I appreciate that!

Servo P1 doesn't move...and the light P0 stays onbefore the program kicks in...the lights BLINK perfect. Playing around with the code...

Dennis


I fixed the searchlight....but no servo movement still.

dacostasr


dacostasr

 ;D

Jon,

It looks awesome.  THANK you for your help!

I'll post a little video in the project section later.

Dennis

JonnyMac

Jon McPhalen
EFX-TEK Hollywood Office