mc-Products => mc-Module => Topic started by: chippyrich on June 15, 2016, 01:45:03 pm

Title: Accelerometer Events
Post by: chippyrich on June 15, 2016, 01:45:03 pm
Hello! Just unpackaged and updated my McThings, and trying to get my head around the accelerometer events. Sorry if this is a basic question :-\

Should I be able to use them in the same way as say Reed Switch changes? So for example, trying to notice a movement:
Code: [Select]
Class knockMon
    Shared Event AccelerometerInt1()
        LedGreen = Not LedGreen   
    End Event
End Class
This doesn't do anything, so assume I am missing something!

Also, there are two interrupts... What is the difference??

Title: Re: Accelerometer Events
Post by: mc-John on June 15, 2016, 02:02:06 pm
You have to study the datasheet of the device. The accelerometer is a high quality low power device that works in the background and when an event happens it wakes-up the processor. The device has two different interrupt-line that can be set for a number of different events, like if there is a shock of more then 3G pull down interrupt line 1 which will then raise event AccelerometerInt1().

Start with your use-case. What do you want to measure? Then configure the accelerometer with I2C command accordingly. When the interrupt fires you communicate with the accelerometer again to get the required information.

There is a link in the mcMod110 datasheet which you can find at
Title: Re: Accelerometer Events
Post by: mc-T2 on June 15, 2016, 02:23:02 pm
@chippyrich - We have created an alpha version project for monitoring when a garage door is open or closed and relaying that information via IFTTT. See attached zipped file.

This project also includes an alpha version of an accelerometer library within the project. This might help you out as you can modify this project to suit your use-case alongside the information that mc-John mentioned above (please be sure to check out the mc-Script Guide and the mc-Mod110 technical specification sheet available on our website)

Let us know if this helps!
Title: Re: Accelerometer Events
Post by: dysharbor on June 24, 2016, 12:39:40 pm
Tried out your accelerometer example but got an error on line 208 in MMA8652--> Left side of the expression data type is 'Byte" and the right side expression returns an Integer.'  Downloaded today 6/24.

I don't know why it's returning an integer, but I changed it to:  dataByte = dataByte & ~0x01.ToByte

Now seems to work.  Am I correct?

Title: Re: Accelerometer Events
Post by: mc-John on June 25, 2016, 04:50:46 pm
You found a bug in the compiler. The not (~) makes it an integer but because all parts of the expression are byte it is still correct. This error was introduced in build 889.
If you look in the original list file (in the zip) you can see that it generated correct code (address 392). Now it does not compile  ???

Code: [Select]
000202      Public Function Standby() As Boolean              383:    0x1400   EntryPoint                  ValTypParams=0, RefTypParams=0
                                                              384:    0xF040   AllocateStackSpace          ValTypeSize=2, RefTypeSize=0

000203          Dim dataByte As Byte
000205          'read status register
000206          dataByte = ReadSingleByte(CTRL_REG1_REG)      385:    0x902A   PushConstU10                42
                                                              386:    0x0081   PushMe                     
                                                              387:    0x2031   BranchSubr                  437
                                                              388:    0xA000   PopLocalI32                 dataByte

000207          'set active bit to put accel in active mode
000208          dataByte = dataByte & ~0x01                   389:    0x9800   PushLocalI32                dataByte
                                                              390:    0x9001   PushConstU10                1
                                                              391:    0x0018   IntNot                     
                                                              392:    0x0062   CvtIntToByt                 
                                                              393:    0x000B   IntAnd                     
                                                              394:    0xA000   PopLocalI32                 dataByte

000209          WriteSingleByte(CTRL_REG1_REG, dataByte)      395:    0x902A   PushConstU10                42
                                                              396:    0x9800   PushLocalI32                dataByte
                                                              397:    0x0081   PushMe                     
                                                              398:    0x2014   BranchSubr                  419
                                                              399:    0xF420   FreeStackSpace              ValTypeSize=1, RefTypeSize=0

000211          Return True                                   400:    0x0076   Return1                     

000212      End Function                                      401:    0x0075   Return0                     
Title: Re: Accelerometer Events
Post by: Conthings on July 04, 2016, 03:12:04 pm
Mh, sorry guys, I'll try my best but I need help.

I want to install the sensors at my driveway gate to track if and how far the gate ist opened.
I think I understand how to set the range.
But what I didn't understand is, can I extract the direction and the force of the direction? And which variables/function do I need?

thanks in advance...

Kind regards
Title: Re: Accelerometer Events
Post by: michal on September 15, 2016, 03:41:40 am

I'm trying to figure out how to (if possible) use the MMA8652 library provided with the garage example to detect like nock (or tap) events. My use case is to stick the mcModule to a floor lamp and then gently tape the lamp (or module if required) to toggle a network connected LIFX light bulb (I will have Node RED handling this).

This is the code (derived from the garage example) that I am trying to use:

Code: [Select]
Class KnockTest
    Shared accel As MMA8652
    Shared Event Boot()
        accel = New MMA8652   
        accel.ConfigureShockInterrupt(0.1, 0)
    End Event
    Shared Event AccelerometerInt1()
        Dim readByte As Byte = accel.ReadSingleByte(MMA8652.PL_STATUS_REG)
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
        If (readByte & 0x80) = 0x80 Then
            'interrupt occured
            payString = readByte.ToString("X#")
        End If
        Lplan.Publish("mcThings/Testing", payload)
    End Event
End Class

I've tried every conceivable value for the threshold and duration with no luck :/

Any tips would be greatly appreciated.

Title: Re: Accelerometer Events
Post by: Nick_W on September 17, 2016, 07:08:55 pm
That code doesn't work for taps.

I have posted a door knock sensor, which has a working tap detector in it, with a totally re-written mma8652 library.

Hopefully you can adapt my code.

Search for "where I'm keeping my code" on these forums for a link to my GitHub.

Edit: here is the link (

Look for doorsensor under projects. This need the re-written mma8652 library, the tempSensor library, and my MQTT library.

This detects a "knock" (two small taps, less than 0.75 seconds apart works) when the door is closed (use a magnet next to the reedswitch to simulate a door being closed).

10 seconds after the door closes, the knock sensor arms.

Events are published to MCThings/# this will show all events. Events are also encoded in the device beacons, you can receive them using, but you don't need this for the basic functionality.

This is tuned to door knocks, so there is a lot of timing in there that you might not need.

The transient sensor set up works, and you can specify force (in g) and duration (in ms). When triggered, it generates a "knock" event.

Good luck,
Title: Re: Accelerometer Events
Post by: michal on September 18, 2016, 05:44:40 am
Thank you Nick!

I'm sure I will be able to adapt the code to my needs and am thankful for your libraries. I also see myself ordering a BME280 module in my immediate future.
I look forward to seeing what you come up with in future :)