Created
February 14, 2017 01:57
-
-
Save dgtal/95d29fb59cd4a7665583787e1fbfc7aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import smbus | |
| import time | |
| import os | |
| import math | |
| # Define a class for the accelerometer readings | |
| class MMA7660(): | |
| bus = smbus.SMBus(1) | |
| def __init__(self): | |
| self.bus.write_byte_data(0x4c, 0x07, 0x00) # Setting up MODE to Stand by to set SR | |
| self.bus.write_byte_data(0x4c, 0x06, 0x10) # Calibrate | |
| self.bus.write_byte_data(0x4c, 0x08, 0x00) # Setting up SR register to 120 samples active and auto sleep mode | |
| self.bus.write_byte_data(0x4c, 0x07, 0x01) # Setting up MODE Active to START measures | |
| def getXYZ(self): | |
| val = [0 for i in range(3)] | |
| val[0] = val[1] = val[2] = 64 | |
| count = 0 | |
| val = self.bus.read_i2c_block_data(0x4c, 0x00) | |
| return val | |
| mma = MMA7660() | |
| for a in range(1000): | |
| x,y,z = mma.getXYZ()[:3] | |
| x, y, z = x*4,y*4,z*4 | |
| if x>127: | |
| x = x-256 | |
| if y>127: | |
| y=y-256 | |
| if z>127: | |
| z=z-256 | |
| print("X=", x) | |
| print("Y=", y) | |
| print("Z=", z) | |
| time.sleep(0.2) | |
| os.system("clear") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment