Sensors (Memsic 2125 Dual-axis Accelerometer)
Tony123
Posts: 2
I have A Memsic 2125 Dual-axis Accelerometer Sensor and it is hooked up
to the Board of Education Development Board (Stamp BS2). What I am looking for, is some help with the code. What I want the code, sensor and BS2, to do is be A Motion Detector.
#1 I want to use one box fan. When the box fan is turned on the BS2 will read the motion with the Memsic 2125 Dual-axis Accelerometer Sensor.
#2 Then as long as the box fan is on and running and making motion, the BS2 will sit and wait till the box fan is turned off.
#3 Then when the box fan is off, the BS2 will know that and make one led turn on and off.
There maybe and easy way of doing this, but what I am A hard time is with the code to know when the box fan is on and running, then when the box fan stops, that is where I am having hard time.
Here is the code.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
Xin PIN 8
Yin PIN 9
ResetLED PIN 10
AlarmLED PIN 11
'
[ Constants ]
HiPulse CON 1
LoPulse CON 0
SampleDelay CON 500
AlarmLevel CON 5
XLimit CON 5
YLimit CON 5
'
[ Variables ]
xCal VAR Word
yCal VAR Word
xMove VAR Word
yMove VAR Word
xDiff VAR Word
yDiff VAR Word
moTimer VAR Word
'
[ Initialization ]
Initialize:
LOW AlarmLED
Read_Cal_Values:
PULSIN Xin, HiPulse, xCal
PULSIN Xin, HiPulse, yCal
xCal = xCal / 10
yCal = yCal / 10
HIGH ResetLED
PAUSE 1000
LOW ResetLED
'
[ Program Code ]
Main:
DO
GOSUB Get_Data
xDiff = ABS (xMove - xCal)
yDiff = ABS (yMove - yCal)
IF (xDiff > XLimit) OR (yDiff > YLimit) THEN moTimer = moTimer + 1
IF (moTimer > AlarmLevel) THEN Motion_1
ELSE
moTimer = 0
ENDIF
LOOP
END
'
[ Subroutines ]
' Sample and filter inputs
Motion_1:
GOSUB Get_Data
xDiff = ABS (xMove - xCal)
yDiff = ABS (yMove - yCal)
IF (xDiff < XLimit) OR (yDiff < YLimit) THEN moTimer = moTimer + 1
IF (moTimer > AlarmLevel) THEN Alarm_On
ELSE
moTimer = 0
ENDIF
LOOP
END
' Blink Alarm LED ' -- will run until BASIC Stamp is reset
Alarm_On:
DO
TOGGLE AlarmLED
PAUSE 250
LOOP
Get_Data:
PULSIN Xin, HiPulse, xMove
PULSIN Yin, HiPulse,yMove
xMove = xMove / 10
yMove = yMove / 10
PAUSE SampleDelay
RETURN
to the Board of Education Development Board (Stamp BS2). What I am looking for, is some help with the code. What I want the code, sensor and BS2, to do is be A Motion Detector.
#1 I want to use one box fan. When the box fan is turned on the BS2 will read the motion with the Memsic 2125 Dual-axis Accelerometer Sensor.
#2 Then as long as the box fan is on and running and making motion, the BS2 will sit and wait till the box fan is turned off.
#3 Then when the box fan is off, the BS2 will know that and make one led turn on and off.
There maybe and easy way of doing this, but what I am A hard time is with the code to know when the box fan is on and running, then when the box fan stops, that is where I am having hard time.
Here is the code.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ I/O Definitions ]
Xin PIN 8
Yin PIN 9
ResetLED PIN 10
AlarmLED PIN 11
'
[ Constants ]
HiPulse CON 1
LoPulse CON 0
SampleDelay CON 500
AlarmLevel CON 5
XLimit CON 5
YLimit CON 5
'
[ Variables ]
xCal VAR Word
yCal VAR Word
xMove VAR Word
yMove VAR Word
xDiff VAR Word
yDiff VAR Word
moTimer VAR Word
'
[ Initialization ]
Initialize:
LOW AlarmLED
Read_Cal_Values:
PULSIN Xin, HiPulse, xCal
PULSIN Xin, HiPulse, yCal
xCal = xCal / 10
yCal = yCal / 10
HIGH ResetLED
PAUSE 1000
LOW ResetLED
'
[ Program Code ]
Main:
DO
GOSUB Get_Data
xDiff = ABS (xMove - xCal)
yDiff = ABS (yMove - yCal)
IF (xDiff > XLimit) OR (yDiff > YLimit) THEN moTimer = moTimer + 1
IF (moTimer > AlarmLevel) THEN Motion_1
ELSE
moTimer = 0
ENDIF
LOOP
END
'
[ Subroutines ]
' Sample and filter inputs
Motion_1:
GOSUB Get_Data
xDiff = ABS (xMove - xCal)
yDiff = ABS (yMove - yCal)
IF (xDiff < XLimit) OR (yDiff < YLimit) THEN moTimer = moTimer + 1
IF (moTimer > AlarmLevel) THEN Alarm_On
ELSE
moTimer = 0
ENDIF
LOOP
END
' Blink Alarm LED ' -- will run until BASIC Stamp is reset
Alarm_On:
DO
TOGGLE AlarmLED
PAUSE 250
LOOP
Get_Data:
PULSIN Xin, HiPulse, xMove
PULSIN Yin, HiPulse,yMove
xMove = xMove / 10
yMove = yMove / 10
PAUSE SampleDelay
RETURN
Comments
the same table, not trying to sense the air moving, trying to sense
the table moving.