Looping constans in python with MMA8451 Accelerometer on RPi
<p>I would like to continuously retrieve data from device MMA8451 accelerometer. Unfortunately there is no proper workaround available.</p>

<p>The problem is that the code get me the first values of axis, but even though i put the request into a loop it just keep on giving me back the same measurements.</p>

<p>My code:</p>

<pre><code>import smbus
import time
import os

class MyMMA():

        bus = None
        data = None

        def setUp(self):

                self.bus = smbus.SMBus(1)
                # MMA8451 address, 0x1D
                # Select Control register, 0x0B
                #               0x00(00)        StandBy mode
                self.bus.write_byte_data(0x1D, 0x0B, 0x00)

                # MMA8451 address, 0x1D
                # Select Control register, 0x0B
                #               0x01(01)        Active mode
                self.bus.write_byte_data(0x1D, 0x0B, 0x01)

                # MMA8451 address, 0x1D
                # Select Configuration register, 0x0E
                #               0x00(00)        Set range to +/- 2g
                self.bus.write_byte_data(0x1D, 0x0E, 0x00)

                time.sleep(0.5)

        def block_read(self):
                # MMA8451 address, 0x1D
                # Read data back from 0x00(0), 7 bytes
                # Status register, X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MS$

                self.data = self.bus.read_i2c_block_data(0x1D, 0X0E)

       def getValueX(self):
#               self.data = self.bus.read_i2c_block_data(0x1D, 0x00)
                xAccl = self.data[1]
                xAccl = (xAccl * 256 + self.data[2]) / 16
                if xAccl &gt; 2047 :
                        xAccl -=4096
                return xAccl

        def getValueY(self):
#               self.data = self.bus.read_i2c_block_data(0x1D, 0x00)
                yAccl = self.data[3]
                yAccl = (yAccl * 256 + self.data[4]) / 16
                if yAccl &gt; 2047 :
                        yAccl -= 4096
                return yAccl

        def getValueZ(self):
#               self.data = self.bus.read_i2c_block_data(0x1D, 0x00)
                zAccl = self.data[5]
                zAccl = (zAccl * 256 + self.data[6]) / 16
                if zAccl &gt; 2047 :
                        zAccl -= 4096
                return zAccl

mma = MyMMA()
mma.setUp()
mma.block_read()


for a in range(10000):
        # Output data to scree
#       mma = MyMMA()
#       mma.setUp()
#       mma.block_read()
        print ("x: ", mma.getValueX())
        print ("y: ", mma.getValueY())
        print ("z: ", mma.getValueZ())
</code></pre>

<p>The way it is currently coded, you read the device once then loop getting the X/Y/Z values from that single reading.</p>

<p>You need to move the call to block_read() INSIDE the loop, i.e. Below the <code>For a in range(10000):</code> statement.</p>

python raspberry-pi adafruit