kuenyls
03-26-2010, 06:46 AM
··
··I have a Basic Stamp 2pe with the Memsic Accelerometer on a daughter board. I am trying to write code to compare averaged values of the accelerometer readings in ranges. I can not get the code to work because I do not understand how the computer see the values that I am calculating. I only know how to output in such a way to read the accelerometer values in " gs". This makes it difficult to debug the program.·Please note that the ranges I want to set are greater than 0.79 gs, greater than 0.68 gs, and greater than 0.60 gs. I do not have decimals in the program because it does not allow me too. At the end of the program I am trying to display a percentage on an eight segment led also on a daughter board. Please look at the code below and let me know if you have any suggestions. I am also including the debug terminal output. Thank you very much.
'{$STAMP BS2pe}
'{$PBASIC 2.5}
' -----[ DATA Directives ]----------------------------------------------------
Reset········· DATA··· 0
Records······· DATA··· (50)
RecordsEnd···· DATA
' -----[ I/O Definitions ]-------------------------------------------------
Xin············ PIN···· 8······················ ' X input from Memsic 2125
Yin············ PIN···· 9······················ ' Y input from Memsic 2125
' -----[ Constants ]-------------------------------------------------------
' Set scale factor for PULSIN
#SELECT $STAMP
· #CASE BS2PE
··· Scale······ CON···· $1E1··················· ' 1.88 us per unit
#ENDSELECT
HiPulse········ CON···· 1······················ ' measure high-going pulse
LoPulse········ CON···· 0
DegSym········· CON···· 176···················· ' degrees symbol
' -----[ Variables ]----------------------------------------------------------
char·········· VAR···· Byte
eeIndex······· VAR···· Word
value········· VAR···· Word
yRaw··········· VAR···· Word
ymG············ VAR···· Word
ymG1············ VAR···· Word··················· ' g force (1000ths)
ymGtotal········ VAR···· Word
ymGaverage······ VAR···· Word
expercentage···· VAR··· Word
rpoints········· VAR··· Word
jpoints········· VAR··· Word
wpoints········· VAR··· Word
count1·········· VAR··· Word
' -----[ Initialization/ Main Routine ]-----------------------------------------------------
Main:
· GOSUB Take_Data
· GOSUB Calculate_Data
· GOSUB Display_Data
END
'-----[ Subroutine - Take_Data ]-------------------------------------------
Take_Data:
· PAUSE 200
·FOR eeIndex = Records TO RecordsEnd STEP 2
···· PULSIN Yin, HiPulse, yRaw
····· yRaw = yRaw */ Scale
····· ymG = ((yRaw / 10) - 500) * 8
··· ymGtotal = ymGtotal + ABS ymG
··· count1 = count1 + 1
·· DEBUG· CRSRX, 7,(ymG.BIT15 * 13 + " "),
······ DEC (ABS ymG / 1000), ".", DEC3 (ABS ymG), " g·· ymG ", CR
·· DEBUG· CRSRX, 7,(ymGtotal.BIT15 * 13 + " "),
······ DEC (ABS ymGtotal / 1000), ".", DEC3 (ABS ymGtotal), "···· total", CR
····· DEBUG· CRSRX, 7,(count1.BIT15 * 13 + " "),
······ DEC (ABS count1 / 1000), ".", DEC3 (ABS count1), "··· count ", CR
· NEXT
· PAUSE 950
·· RETURN
'------[ Subroutine -Calculate_Data
·Calculate_Data:
·ymGaverage = (ABS ymGtotal / (count1))
· DEBUG· CRSRX, 7,(ymGaverage.BIT15 * 13 + " "),
······ DEC (ABS ymGaverage / 1000), ".", DEC3 (ABS ymGaverage), "···· average", CR
······ DEBUG ymGaverage
· IF ( ymGaverage >· 79) THEN
·· jpoints = jpoints + 1
· ELSEIF ( ymGaverage >· 68)· THEN
· rpoints = rpoints + 1
· ELSEIF ( ymGaverage >· 60)···· THEN
· wpoints = wpoints + 1
· 'Insert exercise credit equation here· ( decide variable for x,y,z )
· expercentage· = 3*(jpoints) + 2*(rpoints) + 1*(wpoints)
· DEBUG· CRSRX, 7,(expercentage.BIT15 * 13 + " "),
······ DEC (ABS expercentage / 1000), ".", DEC3 (ABS expercentage), " %··· expercentage ", CR
· ENDIF
· RETURN
' -----[ Subroutine - Display_Data ]------------------------------------------
Display_Data:
OWOUT 6, 1, [DEC4 expercentage]······ ' read DS1822 scratchpad
OWIN· 6, 2, [Xin, Yin]··············· ' get raw temp data
· RETURN
output:
··· 1.240 g·· ymG
······· 1.240···· total
······· 0.001··· count
······ -0.064 g·· ymG
······· 1.304···· total
······· 0.002··· count
······· 0.448 g·· ymG
······· 1.752···· total
······· 0.003··· count
······· 1.384 g·· ymG
······· 3.136···· total
······· 0.004··· count
······· 0.712 g·· ymG
······· 3.848···· total
······· 0.005··· count
······ -0.288 g·· ymG
······· 4.136···· total
······· 0.006··· count
······· 1.176 g·· ymG
······· 5.312···· total
······· 0.007··· count
······· 1.216 g·· ymG
······· 6.528···· total
······· 0.008··· count
······ -0.056 g·· ymG
······· 6.584···· total
······· 0.009··· count
······· 0.368 g·· ymG
······· 6.952···· total
······· 0.010··· count
······· 1.560 g·· ymG
······· 8.512···· total
······· 0.011··· count
······· 0.592 g·· ymG
······· 9.104···· total
······· 0.012··· count
······ -0.408 g·· ymG
······· 9.512···· total
······· 0.013··· count
······· 1.488 g·· ymG
······· 11.000···· total
······· 0.014··· count
······· 1.096 g·· ymG
······· 12.096···· total
······· 0.015··· count
······ -0.448 g·· ymG
······· 12.544···· total
······· 0.016··· count
······· 0.952 g·· ymG
······· 13.496···· total
······· 0.017··· count
······· 1.472 g·· ymG
······· 14.968···· total
······· 0.018··· count
······ -0.104 g·· ymG
······· 15.072···· total
······· 0.019··· count
······· 0.216 g·· ymG
······· 15.288···· total
······· 0.020··· count
······· 1.808 g·· ymG
······· 17.096···· total
······· 0.021··· count
······· 0.640 g·· ymG
······· 17.736···· total
······· 0.022··· count
······ -0.576 g·· ymG
······· 18.312···· total
······· 0.023··· count
······· 1.424 g·· ymG
······· 19.736···· total
······· 0.024··· count
······· 1.232 g·· ymG
······· 20.968···· total
······· 0.025··· count
······ -0.360 g·· ymG
······· 21.328···· total
······· 0.026··· count
······· 0.820···· average
4
··I have a Basic Stamp 2pe with the Memsic Accelerometer on a daughter board. I am trying to write code to compare averaged values of the accelerometer readings in ranges. I can not get the code to work because I do not understand how the computer see the values that I am calculating. I only know how to output in such a way to read the accelerometer values in " gs". This makes it difficult to debug the program.·Please note that the ranges I want to set are greater than 0.79 gs, greater than 0.68 gs, and greater than 0.60 gs. I do not have decimals in the program because it does not allow me too. At the end of the program I am trying to display a percentage on an eight segment led also on a daughter board. Please look at the code below and let me know if you have any suggestions. I am also including the debug terminal output. Thank you very much.
'{$STAMP BS2pe}
'{$PBASIC 2.5}
' -----[ DATA Directives ]----------------------------------------------------
Reset········· DATA··· 0
Records······· DATA··· (50)
RecordsEnd···· DATA
' -----[ I/O Definitions ]-------------------------------------------------
Xin············ PIN···· 8······················ ' X input from Memsic 2125
Yin············ PIN···· 9······················ ' Y input from Memsic 2125
' -----[ Constants ]-------------------------------------------------------
' Set scale factor for PULSIN
#SELECT $STAMP
· #CASE BS2PE
··· Scale······ CON···· $1E1··················· ' 1.88 us per unit
#ENDSELECT
HiPulse········ CON···· 1······················ ' measure high-going pulse
LoPulse········ CON···· 0
DegSym········· CON···· 176···················· ' degrees symbol
' -----[ Variables ]----------------------------------------------------------
char·········· VAR···· Byte
eeIndex······· VAR···· Word
value········· VAR···· Word
yRaw··········· VAR···· Word
ymG············ VAR···· Word
ymG1············ VAR···· Word··················· ' g force (1000ths)
ymGtotal········ VAR···· Word
ymGaverage······ VAR···· Word
expercentage···· VAR··· Word
rpoints········· VAR··· Word
jpoints········· VAR··· Word
wpoints········· VAR··· Word
count1·········· VAR··· Word
' -----[ Initialization/ Main Routine ]-----------------------------------------------------
Main:
· GOSUB Take_Data
· GOSUB Calculate_Data
· GOSUB Display_Data
END
'-----[ Subroutine - Take_Data ]-------------------------------------------
Take_Data:
· PAUSE 200
·FOR eeIndex = Records TO RecordsEnd STEP 2
···· PULSIN Yin, HiPulse, yRaw
····· yRaw = yRaw */ Scale
····· ymG = ((yRaw / 10) - 500) * 8
··· ymGtotal = ymGtotal + ABS ymG
··· count1 = count1 + 1
·· DEBUG· CRSRX, 7,(ymG.BIT15 * 13 + " "),
······ DEC (ABS ymG / 1000), ".", DEC3 (ABS ymG), " g·· ymG ", CR
·· DEBUG· CRSRX, 7,(ymGtotal.BIT15 * 13 + " "),
······ DEC (ABS ymGtotal / 1000), ".", DEC3 (ABS ymGtotal), "···· total", CR
····· DEBUG· CRSRX, 7,(count1.BIT15 * 13 + " "),
······ DEC (ABS count1 / 1000), ".", DEC3 (ABS count1), "··· count ", CR
· NEXT
· PAUSE 950
·· RETURN
'------[ Subroutine -Calculate_Data
·Calculate_Data:
·ymGaverage = (ABS ymGtotal / (count1))
· DEBUG· CRSRX, 7,(ymGaverage.BIT15 * 13 + " "),
······ DEC (ABS ymGaverage / 1000), ".", DEC3 (ABS ymGaverage), "···· average", CR
······ DEBUG ymGaverage
· IF ( ymGaverage >· 79) THEN
·· jpoints = jpoints + 1
· ELSEIF ( ymGaverage >· 68)· THEN
· rpoints = rpoints + 1
· ELSEIF ( ymGaverage >· 60)···· THEN
· wpoints = wpoints + 1
· 'Insert exercise credit equation here· ( decide variable for x,y,z )
· expercentage· = 3*(jpoints) + 2*(rpoints) + 1*(wpoints)
· DEBUG· CRSRX, 7,(expercentage.BIT15 * 13 + " "),
······ DEC (ABS expercentage / 1000), ".", DEC3 (ABS expercentage), " %··· expercentage ", CR
· ENDIF
· RETURN
' -----[ Subroutine - Display_Data ]------------------------------------------
Display_Data:
OWOUT 6, 1, [DEC4 expercentage]······ ' read DS1822 scratchpad
OWIN· 6, 2, [Xin, Yin]··············· ' get raw temp data
· RETURN
output:
··· 1.240 g·· ymG
······· 1.240···· total
······· 0.001··· count
······ -0.064 g·· ymG
······· 1.304···· total
······· 0.002··· count
······· 0.448 g·· ymG
······· 1.752···· total
······· 0.003··· count
······· 1.384 g·· ymG
······· 3.136···· total
······· 0.004··· count
······· 0.712 g·· ymG
······· 3.848···· total
······· 0.005··· count
······ -0.288 g·· ymG
······· 4.136···· total
······· 0.006··· count
······· 1.176 g·· ymG
······· 5.312···· total
······· 0.007··· count
······· 1.216 g·· ymG
······· 6.528···· total
······· 0.008··· count
······ -0.056 g·· ymG
······· 6.584···· total
······· 0.009··· count
······· 0.368 g·· ymG
······· 6.952···· total
······· 0.010··· count
······· 1.560 g·· ymG
······· 8.512···· total
······· 0.011··· count
······· 0.592 g·· ymG
······· 9.104···· total
······· 0.012··· count
······ -0.408 g·· ymG
······· 9.512···· total
······· 0.013··· count
······· 1.488 g·· ymG
······· 11.000···· total
······· 0.014··· count
······· 1.096 g·· ymG
······· 12.096···· total
······· 0.015··· count
······ -0.448 g·· ymG
······· 12.544···· total
······· 0.016··· count
······· 0.952 g·· ymG
······· 13.496···· total
······· 0.017··· count
······· 1.472 g·· ymG
······· 14.968···· total
······· 0.018··· count
······ -0.104 g·· ymG
······· 15.072···· total
······· 0.019··· count
······· 0.216 g·· ymG
······· 15.288···· total
······· 0.020··· count
······· 1.808 g·· ymG
······· 17.096···· total
······· 0.021··· count
······· 0.640 g·· ymG
······· 17.736···· total
······· 0.022··· count
······ -0.576 g·· ymG
······· 18.312···· total
······· 0.023··· count
······· 1.424 g·· ymG
······· 19.736···· total
······· 0.024··· count
······· 1.232 g·· ymG
······· 20.968···· total
······· 0.025··· count
······ -0.360 g·· ymG
······· 21.328···· total
······· 0.026··· count
······· 0.820···· average
4