# mma8451 Motion Detection and RaspberryPi 2b+

Question asked by Luigi Sechi on Nov 29, 2018
Latest reply on Jan 11, 2019 by Tomas Vaverka

Goodmornig,

This is my main code:

##############################"

xOffset = -0.400 # m/s^2
yOffset =  0.200 # m/s^2
zOffset =  9.400 # m/s^2
while True:
while sensor.Motion():
x, y, z = sensor.acceleration
time_now = datetime.datetime.now()
Calcx = -xOffset
Calcy = -yOffset
Calcz = -zOffset
X = x + Calcx
Y = y + Calcy
Z = z + Calcz

sensor.MotionRegister()

print('Time={0}   X={1:0.3f} m/s^2  Y:{2:0.3f} m/s^2  Z:{3:0.3f} m/s^2'.format(time_now, X, Y, Z))
time.sleep(0)

IntSourceMFF = sensor.MotionRegister()

#####################"

And I have setup my MMA8451 into a class like this:

###################""

def Motion (self): #, IntSourceSystem,IntSourceMFF):
reg1 &= 0xFE #clear active bit (standby)
self._write_u8(_MMA8451_REG_CTRL_REG1, reg1)
self._write_u8(REG_FF_MT_CONFIG, 0x48) #Moto detection X axe after the debounce time (100)ms is reached
self._write_u8(REG_FF_MT_THS, 0x10)    #threshold 1g: es 1g/0.063g = 16 counts = 10 (hexadecimal)
self._write_u8(REG_FF_MT_COUNT, 0x0A) #Debounce counter, set at 12.5ms timer for 800 output data rate
self._write_u8(_MMA8451_REG_CTRL_REG4, 0x04)
self._write_u8(_MMA8451_REG_CTRL_REG5, 0x04)
# self._write_u8(_MMA8451_REG_CTRL_REG3, 0x04)
self._write_u8(_MMA8451_REG_CTRL_REG1, reg1 | 0x01) #activated
print("It will work")
if ((IntSourceSystem & 0x04) == 0x04):
print("You are into if")
return True
else:
print("You are into else")
return False

def MotionRegister (self):

##################"

Something strange happens, it works properly only if I print something in my setup module (in this case "It will work").

Why it does it? I don't want that print.

Maybe it is a problem with the Count register setup..I didn't well realized how to use it, I want to use a 100ms timer for 800 ODR but with 0x50 didn't works..

Can someone help me pls?