PhoneGap Sample – Shake to Roll

Sorry for being so quiet lately. I’ve got three presentations this week and two brand new ones in two weeks. Mentally – it has been killing me. As with all things – it will pass. (And I think I’m building some good new presentations as well!) In the meantime, I thought I’d share a simple PhoneGap application I built for my four-hour lab in Ohio a few days back.

The idea behind the application was to demonstrate the Accelerometer API. This returns movement information along three different axes.

By itself, the API is easy enough to use. What I was having difficulty with was coming up with a practical example of it. I thought I’d create a simple application that mimicked rolling a die. I’d start off by selecting a random number between one and six. I’d display this on the screen like so.

(What you’re seeing here is the Ripple emulator. I plan on talking about this in a few days.)

The code for picking and displaying a random number was trivial.

What was more difficult was figuring out how to respond to a shake. I mean, seriously, what is a shake, in code terms?

I did some Googling and discovered that most people simply tracked the X/Y/Z values and then compared them to historical values. I came up with this solution (which is based on what I saw so this isn’t some brilliant discovery of mine).

I then used the Ripple emulator to test, and slowly tweaked the numbers until it “felt” right to me. You can see the result below.

For the full source code (there really isn’t much to it), you can see the code in the GitHub repo for the presentation: DevelopingMobileAppsWithPhoneGap/tree/master/labs/6_accelerometer

  • Ross Rawlins

    have you got a version of this with angular and ngcordova working?

    • http://www.raymondcamdencom/ Raymond Camden

      No, but honestly, it’s one event handler and a func to determine if it was a shake. Fairly trivial, right?

      • Ross Rawlins

        I havent been able to understand how ngCordova plugin works so I know if seems easy for you but I havent grasped this on yet.

        • http://www.raymondcamdencom/ Raymond Camden

          How ngCordova works in general? It’s mainly a Angular-friendly promisey- version of Cordova stuff. If you don’t understand ngCordova in general, I’d suggest their docs of course.

          • Ross Rawlins

            I know how to implement all their plugins. Its just the device motion I cant seem to get anything to fire with it.

          • http://www.raymondcamdencom/ Raymond Camden

            Well when you tried, what error did you get?

          • Ross Rawlins

            thats the thing in ripple it tells me it detect device motion of undefined because ngCordova plugins dont work in ripple. and when I run it on a device I cant see anything that is going on no alerts are firing at all.

          • http://www.raymondcamdencom/ Raymond Camden

            Ok, well first off, I’d remove Ripple from the picture as it is quite old. If you don’t know how to debug, you need to learn about remote debugging. Chrome and Safari make this easy and powerful. If you go to the About Me page on this blog, look at my list of articles, and I’ve got two covering these features. It will help quite a bit.

          • Ross Rawlins

            thanks I will try; I have used the adobe debugger but that doesnt work becuase of angular.

          • http://www.raymondcamdencom/ Raymond Camden

            What Adobe debugger?

          • Ross Rawlins

            with phonegap you can build your app through adobe build and there you can select to allow debugging then when you deploy the app and you can debug it on your computer while it is on the device. But this isnt working with angular yet. its called weinre I think.

          • http://www.raymondcamdencom/ Raymond Camden

            Oh wow, you mean Adobe PhoneGap Builder. Ok. “Adobe Debugger” didn’t mean anything to me. So yes, it uses Weinre, and that was made *before* remote debugging was an option. You only use it if you are using a device not supported by remote debugging. Trust me – check out my articles. :)

  • Aaron Mason

    I see a potential issue with your code:

    if(deltaX + deltaY + deltaZ > 3) {

    That probably won’t add deltaX, deltaY and deltaZ and then see if the value is greater than 3 – it most likely adds deltaX, deltaY and “deltaZ > 3”, which may be 1 or 0 depending on whether or not deltaZ is greater than three.

    The code below will produce the desired result:

    if((deltaX + deltaY + deltaZ) > 3) {

    As a programming lecturer once told me, parentheses are free, never rely on order of operations.