November 23, 2024, 01:29:55 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.


Need help with some code

Started by widiver, August 29, 2008, 07:00:40 AM

Previous topic - Next topic

widiver

Hi
I am trying to have a program that when someone walks in front of the sensor an Led will turn on and sound will play from the ap-8. Once the sound is finished I would like the led to turn off. I would like the ap-8 to pick a random sound from one of its 7 locations and play it.

I pieced together the code from different responses on the forum but one of the problems I am having is that the led will light and then turn off right away even though the sound is still playing.

If I get time before Halloween I want to  add a 12 volt solenoid to this and this is what will happen

1. trigger activated
2. led lights
3.  random audio is played
4. when audio is finished activate solenoid for 3 seconds
5. turn off led
6. wait 60 seconds before activating again.

This is what I have so far where the led does not stay lit and I don't have the solenoid added in yet either.

Once again
thanks
John

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


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  LED             = PIN0

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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400


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

SYMBOL  timer           = B2
SYMBOL  sfx             = B3                    ' audio segment
SYMBOL  last            = B4                    ' last played
SYMBOL  mask            = B5                    ' bit mask for selection
SYMBOL  check           = B6                    ' for testing play list
SYMBOL  playList        = B7                    ' sounds played
SYMBOL  status          = B8
SYMBOL   playing        = BIT15
SYMBOL  lottery         = W5                    ' random value

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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00111111                              ' make P0-P5 outputs


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

Main:
  timer = 0                                     ' reset timer
  DEBUG "Waiting for PIR...", CR

Check_Trigger:
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * PIR                       ' update timer
  IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input

  DEBUG "Triggered!", CR, CR

LED_ON:
LED = IsOn                                      'turns led on

Select_Sound:
  RANDOM lottery                                ' re-stir
  sfx = lottery // 8                            ' make 0..7
  IF sfx = last THEN Select_Sound               ' no repeats allowed
  READ sfx, mask                                ' get bit mask
  check = playList & mask                       ' check agains play list
  IF check > 0 THEN Select_Sound                ' if played, try again
    playList = playList | mask                  ' mark play list

Play_Sound:
  SEROUT Sio, Baud, ("!AP8", %00, "P", sfx)     ' start audio

Let_Sound_Finish:
  PAUSE 100
  SEROUT Sio, OT2400, ("!AP8", %00, "G")        ' get status
  SERIN  Sio, OT2400, status
  IF playing = Yes THEN Let_Sound_Finish

LED_Off:
  LED = IsOff                                        'led off

  PAUSE 60000
  GOTO Main


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


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


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


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


JonnyMac

How are you connecting the AP-8 to the Prop-1?  Here's a quick check list:

-- ULN2803 modified (pin 1 removed) or replaced with ULN2003
-- Prop-1 P7 header connected to AP-8 Serial header

Sometimes people connect to the AP-8 "P" header; a serial message on this line can cause a start but there is no talking back.

I've updated your program to reset the play list when all files have played and have tossed in the solenoid as well.  Note that I added a RANDOM lottery statement to the trigger loop so that you get a better random selection of audio.

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


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


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


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

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  PIR             = PIN6                  ' SETUP = DN
SYMBOL  Solenoid        = PIN1
SYMBOL  LED             = PIN0


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

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

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  Baud            = OT2400


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

SYMBOL  status          = B0
SYMBOL   playing        =  BIT7

SYMBOL  timer           = B2
SYMBOL  sfx             = B3                    ' audio segment
SYMBOL  last            = B4                    ' last played
SYMBOL  mask            = B5                    ' bit mask for selection
SYMBOL  check           = B6                    ' for testing play list
SYMBOL  playList        = B7                    ' sounds played

SYMBOL  lottery         = W5                    ' random value


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

Reset:
  PINS = %00000000                              ' clear all outputs
  DIRS = %00000011                              ' make P0-P1 outputs

  SEROUT Sio, Baud, ("!!!!!!AP8", %00, "X")     ' re-sync and stop

  playList = %0000000                           ' reset play list


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

Main:
  timer = 0                                     ' reset timer
  DEBUG "Waiting for PIR...", CR

Check_Trigger:
  RANDOM lottery                                ' stir random value
  PAUSE 5                                       ' loop pad
  timer = timer + 5 * PIR                       ' update timer
  IF timer < 100 THEN Check_Trigger             ' wait for 0.1 sec input

  DEBUG "Triggered!", CR, CR

Led_On:
LED = IsOn                                      'turns led on

Select_Sound:
  RANDOM lottery                                ' re-stir
  sfx = lottery // 8                            ' make 0..7
  IF sfx = last THEN Select_Sound               ' no repeats allowed
  READ sfx, mask                                ' get bit mask
  check = playList & mask                       ' check agains play list
  IF check > 0 THEN Select_Sound                ' if played, try again
    playList = playList | mask                  ' mark play list

Play_Sound:
  SEROUT Sio, Baud, ("!AP8", %00, "P", sfx)     ' start audio

Let_Sound_Finish:
  PAUSE 100
  SEROUT Sio, OT2400, ("!AP8", %00, "G")        ' get status
  SERIN  Sio, OT2400, status
  IF playing = Yes THEN Let_Sound_Finish

Led_Off:
  LED = IsOff                                   ' led off

  Solenoid = IsOn
  PAUSE 3000
  Solenoid = IsOff

  PAUSE 60000

  IF playList = %11111111 THEN Reset
    GOTO Main


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


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


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

widiver

Hi Jon
I use the ULN2803 with a modified (pin 1 removed) and it is connected to the serial header.

I am able to hear the sound, and it is fairly random. The one issue I was having was the led would light when the sound started and then turn off right away, even though the sound was still playing.

Will the code you wrote keep the light on the whole time the audio is playing. I am not at home right now so I can't check.

thanks
John

JonnyMac

Yes, it should.  Download the AP-8 test code and make sure it runs properly -- this uses the same feature (wait until audio stops before moving on) that I've employed in your program.
Jon McPhalen
EFX-TEK Hollywood Office