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
Archived Comments
Thanks - I do find that if I use a frequency of 200 then the shake event fires multiple times - e.g. 3 times on Ripple Emulator and on my Android S4. I set it to 500 and it seems to only fire once. Is it possible to adapt the code to only fire once and then not fire again if it is within a small period of time (after a few seconds it should keep listening)
It is kind of cheesy, but use a global var: paused
When you begin the shake do:
paused=true;
window.setTimeout(clearPause, 2000);
clearPause will set paused to false. And before you do your shake. check if paused is true.
Thanks, that worked. It is very sensitive!
have you got a version of this with angular and ngcordova working?
No, but honestly, it's one event handler and a func to determine if it was a shake. Fairly trivial, right?
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.
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.
I know how to implement all their plugins. Its just the device motion I cant seem to get anything to fire with it.
Well when you tried, what error did you get?
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.
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.
thanks I will try; I have used the adobe debugger but that doesnt work becuase of angular.
What Adobe debugger?
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.
I also looked at your about me page and I know of edge, and I dont have a make. I will have to look at the code again and try see how I can get a response of some sorts.
http://ngcordova.com/docs/p...
thats the example code that I cant get to fire off any time let alone after a shake.
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. :)
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.
From what I see here, https://developer.mozilla.o..., it looks like addition takes priority. Did you test? You are right though that parenthesis wouldn't hurt.
I stand corrected. Even in C it seems addition takes priority. Thanks, I learned something new today.