Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer Questions — Parallax Forums

Accelerometer Questions

Tom PTom P Posts: 97
edited 2007-10-30 19:22 in BASIC Stamp
Have a 3-axis accelerometer and would like to setup an alarm condition with an LED when sensor goes over .5g acceleration. Most interested in the Z axis and positive acceleration. How can I modify the sample code below:
Tnks - help appreciated


' H48C_3-Axis.BS2
' Hitachi H48C 3-Axis Accelerometer Demonstration

' {$STAMP BS2}
' {$PBASIC 2.5}


Dio PIN 15 ' data to/from module
Clk PIN 14 ' clock output
CS PIN 13 ' active-low chip select
XAxis CON 0 ' adc channels
YAxis CON 1
ZAxis CON 2
VRef CON 3
Cnt2Mv CON $CE4C ' counts to millivolts
' 0.80586 with **
GfCnv CON $3852 ' g-force conversion
' 0.22 with **

axis VAR Nib ' axis selection
rvCount VAR Word ' ref voltage adc counts
axCount VAR Word ' axis voltage adc counts
mVolts VAR Word ' millivolts
gForce VAR Word ' axis g-force

dValue VAR Word ' display value
dPad VAR Nib ' display pad


Reset:
HIGH CS ' deselect module
DEBUG CLS, ' paint display
"=========================", CR,
"H48C 3-Axis Accelerometer", CR,
"=========================", CR,
CR,
" Count Volts G ", CR,
"


", CR,
"VRef ", CR,
" X ", CR,
" Y ", CR,
" Z "

Main:
FOR axis = XAxis TO ZAxis ' loop through each axis
GOSUB Get_H48C ' read vRef & axis counts

dValue = rvCount ' display vRef count
DEBUG CRSRXY, 6, 6
GOSUB RJ_Print

dValue = axCount ' display axis count
DEBUG CRSRXY, 6, (7 + axis)
GOSUB RJ_Print

mVolts = rvCount ** Cnt2Mv ' convert vref to mv
DEBUG CRSRXY, 13, 6, ' display
DEC (mVolts / 1000), ".",
DEC3 mVolts

mVolts = axCount ** Cnt2Mv ' convert axis to mv
DEBUG CRSRXY, 13, (7 + axis),
DEC (mVolts / 1000), ".",
DEC3 mVolts

IF (axCount >= rvCount) THEN
gForce = (axCount - rvCount) ** GfCnv ' positive g-force
ELSE
gForce = -((rvCount - axCount) ** GfCnv) ' negative g-force
ENDIF
DEBUG CRSRXY, 20, (7 + axis), ' display g-force
" " + (gForce.BIT15 * 13),
DEC1 (ABS(gForce) / 100), ".",
DEC2 ABS(gForce)
NEXT
PAUSE 200
GOTO Main

Get_H48C:
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3] ' select vref register
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13] ' read ref voltage counts
HIGH CS
PAUSE 1
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts
HIGH CS
RETURN

RJ_Print:
LOOKDOWN dValue, >=[noparse][[/noparse]10000, 1000, 100, 10, 0], dPad
DEBUG REP " "\dPad, DEC dValue
RETURN

Comments

  • Shawn LoweShawn Lowe Posts: 635
    edited 2007-10-30 19:22
    Can you get a reading of 1 G for the Z axis? If so, which variable holds this info?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    Maybe I should have waited to do that......
Sign In or Register to comment.