LocoMotion: MMA8453 with Interrupts…



In the previous post about the MMA8453_N0m1 library we talked about how to get the stream of raw XYZ data back from the accelerometer.  In this post we will be getting into some of the more advanced features of the MMA8453.


The two advanced features that the MMA8453_N0m1 library breaks out for easier use are the interrupt motion detection, and the interrupt shake detection.  Now motion and shake sound like the same things, but in fact they are slightly different.  What motion detection does is allows you to detect any type of motion above a certain force threshold.  While shake is more specific and allows for only detecting sharp fast motion, by using a highpass filter to remove gentler smoother rolling motions.  The library includes 1 example for each feature.  However the way the library is designed, the two features are used in a very similar manner so we will only cover one here.


So lets assume you have the library and circuit still setup from the previous post on the MMA8453_N0m1 lib.  The example we will be explaining is the shake example, because its ever so slightly more complex than the motion.  Though really its almost identical.  After making the library object just as before, the first thing you will do is initialize the library in the arduino setup() function, with the following function call:


So the first parameter in this function, which is currently set at “32?  the shake detection acceleration force threshold, which is a value between 0-127.  This value is calculated through a simple formula as follows.  The gravitational threshold you desire divided by 0.063.  So for example in the above line we do this: 2g/ 0.063g = 31.746, and since the parameter is an int, lets round that value to 32.


The next 3 parameters are just enabling each axis for shake detection; listed X, Y, Z.  So if your project only needs to detect a shaking motion over 1 axis you can disable the others. The second last parameter is a boolean to enable one of the INT pins on the MMA8453. So, to put it simply, True = pin 2, False = pin1. The last parameter is to set the Arduino interrupt pin number.  In this example we are setting it to pin 2.  However you could set this to any interrupt pin you have available.  Included in the example folder is an example that uses pin change interrupt to use a non standard interrupt pin.  After that you are pretty much done.  There are just a few simple lines left to add.


You must call the update function at the top of the loop().  The shake function returns true or false if shake is detected, and the shakeAxisX(), shakeAxisY(), shakeAxisZ() functions will return true if a particular axis is shaken. That’s all there is to it!  The motion functions are almost identical except it will only return whether or not there is motion, not which axis is in motion.  Well I tried to make it more complex, then the previous post.  That formula had you sweating, didn’t it?


View the original article here