Shop OBEX P1 Docs P2 Docs Learn Events
loop change for motion detector code — Parallax Forums

loop change for motion detector code

normandjnormandj Posts: 2
edited 2009-10-05 03:30 in BASIC Stamp
I am working with MEMSIC 2125 accelerometer and this program:
MEMSIC2125-Motion.BS2

I set everything up and the hardware and software works great, but I want to modify the
setup a little. The program is setup to be similar to a motion detector alarm: a light flashes until someone resets the alarm. I want to modify the output. Instead of a light continuously flashing, I want to switch a secondary circuit on. The circuit is turn on with a push button switch. I would like for the Basic Stamp to basically act as the push button switch to turn the secondary circuit on. I spliced the wires from the secondary circuit into the correct output pins on the breadboard. Now here's where I need the help: the "Alarm On" subroutine loops continuously. I need to get it to stop after it "pushes the button" so to speak. Then I need the program to revert back to its original state and wait for more motion to send another signal out.


I'm guessing I need a counter and an if/else statement or a loop/until statement, but I can't
write the code correctly. The code is below.

Please help!

Thanks,

Danny Norman
Knoxville, TN

' =========================================================================
'
' File...... MEMSIC2125-Motion.BS2
' Purpose... Detects continuous motion for given period
' Author.... Parallax (based on code by A. Chaturvedi of Memsic)
' E-mail.... support@parallax.com
' Started...
' Updated... 15 JAN 2003
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================


'
[noparse][[/noparse] Program Description ]
'
' Monitors X and Y inputs from Memsic 2125 and will trigger alarm if
' continuous motion is detected beyond the threshold period.


'
[noparse][[/noparse] I/O Definitions ]

Xin PIN 8 ' X pulse input
Yin PIN 9 ' Y pulse input
ResetLED PIN 10 ' reset LED
AlarmLED PIN 11 ' alarm LED


'
[noparse][[/noparse] Constants ]

HiPulse CON 1 ' measure high-going pulse
LoPulse CON 0

SampleDelay CON 500 ' 0.5 sec
AlarmLevel CON 5 ' 5 x SampleDelay

XLimit CON 5 ' x motion max
YLimit CON 5 ' y motion max


'
[noparse][[/noparse] Variables ]

xCal VAR Word ' x calibration value
yCal VAR Word ' y calibration value
xMove VAR Word ' x sample
yMove VAR Word ' y sample
xDiff VAR Word ' x axis difference
yDiff VAR Word ' y axis difference

moTimer VAR Word ' motion timer


'
[noparse][[/noparse] Initialization ]

Initialize:
LOW AlarmLED ' alarm off
moTimer = 0 ' clear motion timer

Read_Cal_Values:
PULSIN Xin, HiPulse, xCal ' read calibration values
PULSIN Yin, HiPulse, yCal
xCal = xCal / 10 ' filter for noise & temp
yCal = yCal / 10

HIGH ResetLED ' show reset complete
PAUSE 1000
LOW ResetLED


'
[noparse][[/noparse] Program Code ]

Main:
DO
GOSUB Get_Data ' read inputs
xDiff = ABS (xMove - xCal) ' check for motion
yDiff = ABS (yMove - yCal)

IF (xDiff > XLimit) OR (yDiff > YLimit) THEN
moTimer = moTimer + 1 ' update motion timer
IF (moTimer > AlarmLevel) THEN Alarm_On
ELSE
moTimer = 0 ' clear motion timer
ENDIF
LOOP
END


'
[noparse][[/noparse] Subroutines ]

' Sample and filter inputs

Get_Data:
PULSIN Xin, HiPulse, xMove ' take first reading
PULSIN Yin, HiPulse, yMove
xMove = xMove / 10 ' filter for noise & temp
yMove = yMove / 10
PAUSE SampleDelay
RETURN


' Blink Alarm LED
' -- will run until BASIC Stamp is reset

Alarm_On:
DO
TOGGLE AlarmLED ' blink alarm LED
PAUSE 250
LOOP ' loop until reset

Comments

  • dev/nulldev/null Posts: 381
    edited 2009-09-29 21:18
    It's a little unclear what you mean by "switching" on the second circuit. I'll assume you need a HIGH signal going on a pin...
    Change this line "IF (moTimer > AlarmLevel) THEN Alarm_On" to
    IF (moTimer > AlarmLevel) THEN GOSUB Alarm_On
    



    Change the Alarm_On subroutine:
    Alarm_On:
       HIGH [noparse][[/noparse]somepin]
       RETURN
    
    



    There might be additional things you need to do with the code, but it's difficult to give recommendations when you don't provide a schematic.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-29 21:44
    Here is one option

    Alarm_On:
    ' DO
    TOGGLE AlarmLED ' blink alarm LED
    PAUSE 250
    ' set SecondAlarm
    SecondAlarm = ' set second alarm to whatever
    GOSUB xx ' process second alarm - include reset ??
    GOTO Main: ' return to main pseudo loop but you loose AlarmLED blinking

    ' LOOP ' loop until reset '

    Second option - or include your whole Main in DO ... LOOP
  • normandjnormandj Posts: 2
    edited 2009-10-05 03:30
    Thanks vaclav_sal and dev/null

    These comments helped greatly. I ended up with this code that works pretty well:

    Alarm_On:
    HIGH AlarmLED
    PAUSE 6000
    LOW AlarmLED
    PAUSE 2000
    RETURN

    I also added

    LOW AlarmLED

    in the else part of MAIN.


    Thanks again.
Sign In or Register to comment.