Can someone please read this and help find why phone thinks its constantly shaking?

mistermentality

Active Member
Licensed User
Longtime User
I apologise in advance for asking but having spent hours and hours trying this code and various different variations I really am puzzled as to why this code doesn't work on my phone yet works perfectly on the emulator and so am hoping someone can comment.

I'm really not asking any one to rewrite it or anything like that just that if you see an obvious problem in the code to please enlighten me as to what it is.

I've stripped most irrelevant data out, but left the variable declarations intact in case they are part of the problem somehow.

I believe the cause has something to do with the accelerometer usage because the app works great in the emulator (which has no accelerometer) while on the phone the app will often start up, or resume, thinking it has been shaken and then continue acting as if it has been (by calling the sub named Shaken) until it force closes, usually within seconds of starting.

If the phone is shaken even just once it usually keeps calling sub Shaken.

I have tried everything I can think of even trying to compensate by allowing for a few mistaken shakes with the variable called numberoftimesshaken but it has not helped. I did not write the accelerometer routine myself but the gist of it is from an example that was posted in another thread here and the code itself works so it doesn't seem to be that routine.

Without some advice I think I shall have to omit the shake code altogether which is a shame as the app is a kind of magic 8 ball app where you can shake the phone to shake the ball and get an answer which would be a nice touch if I can keep that feature in use.

I could post the full code if needed but it's a lot longer and doesn't reference the accelerometer, just moves the ball around on screen, generates a number and picks an appropriate response to show within the ball on screen. Plus I don't want to make anyone sift through long lines of unrelated code as I feel quite embarrassed being so stuck I have to ask for assistance with finding the error.

So, and hoping I haven't over stepped any boundaries, can anyone see anything that as a new user I may have done a little wrong that could cause the phone to act as if it has been shaken when it hasn't?

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
    Dim MediaPlayer1 As MediaPlayer
   Dim l As Int
   Dim LastX, LastY, LastZ As Float
   Dim LastUpdate, TimeDiff As Long
   Dim Activated As Boolean
   Activated=True
   Dim ShakeThreshold As Double
   ShakeThreshold=1200
   Dim List1_numerology As List
   Dim randomnumber As Int
   Dim highest As Int
        Dim pws As PhoneWakeState
   Dim numberoftimesshaken As Int
   Dim Delay As Long
   Dim waitover As Long
   Dim p As Phone
   Dim Jerk As Double
   Dim JerkX, JerkYX, JerkZ As Double
   Dim MyFont As Typeface
   MyFont = Typeface.LoadFromAssets("transist.ttf")
   MyFont = Typeface.CreateNew(MyFont, Typeface.STYLE_BOLD)
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
    Dim Accelorometer As PhoneAccelerometer
   Dim ImageView1_ball As ImageView
   Dim ImageView1_sky As ImageView
   Dim Spinner1 As Spinner
   Dim Label1_balltext As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main.bal")
If FirstTime Then
' We add the following value because app is starting and for some reason
' acting as if phone was shaken
' so this means it has to be shook a few times to register
numberoftimesshaken = 0
Accelorometer.StartListening("Accelorometer")
LastUpdate=DateTime.Now
End If
End Sub

Sub Activity_Resume
numberoftimesshaken = 0
LastUpdate=DateTime.Now   
pws.keepalive(True)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
pws.ReleaseKeepAlive
End Sub

Sub Label1_balltext_Click
predict
End Sub

Sub predict
   ballmove
End Sub
   
Sub ballmove
   p.vibrate(20)
End Sub
   
   Sub Accelorometer_AccelerometerChanged(x As Float, y As Float, z As Float)
          If Activated=True Then
      TimeDiff=DateTime.Now-LastUpdate
      Jerk=Abs(x-LastX+y-LastY+z-LastZ)/TimeDiff*10000
      If Jerk>=ShakeThreshold Then numberoftimesshaken =     numberoftimesshaken +1
      LastX=x
      LastY=y
      LastZ=z
   End If
   If numberoftimesshaken >=4 Then Shaked
   LastUpdate=DateTime.Now
End Sub

Sub Shaked
predict
ToastMessageShow(numberoftimesshaken, True)
numberoftimesshaken=0
End Sub

Thanks for any advice.

Dave
Dave
 

klaus

Expert
Licensed User
Longtime User
I would suggest you to save the x, y, z values of the accelerometers and the time in a file and look afterwards at the changes.
What are the x, y, z (x+y+z) changes ?
What are the x/timediff, y/timediff, z/timediff (x+y+z)/timediff changes ?
Then you can understand what happens and decide how to handle it.
I tested once a Shake program in B4PPC, the difference with your code is that:
- the checking routine was in a Timer, so the time interval was always the same
- each channel was calculated seperately.

I'm afraid that in your code with a small acceleration change and a very short time interval you get big values, on the other side with a big acceleration chage and a big time interval you won't get big values.

In a first step you could check the Acceleration values instead of the Jerk values that means not dividing by TimeDiff and change perhaps the 10000 value.

Best regards.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The sensors in phones are not of very high quality and are in an electronically noisy environment so they tend to give not very accurate and rather noisy outputs. You will need to program some sort of filtering appropriate for the use to which you want put the sensor outputs.
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Thanks for the help, I have tried again to sort this and no joy so decided to get on with the rest of the app and when that is done try again.

Tried the suggestions, rewrote the code a few times and even tried using the orientation sensor instead but still can't get it to do what I want so will return to it in a day or two.

I think I will try again to use the orientation sensor to somehow detect a slight "flip" of the phone as I agree the accelerometer doesn't seem the ideal choice whereas the orientation sensor data is much more consistent and therefore reliable.

Thanks for the replies, it has actually been helpful (its given me some other ideas) and even though I haven't got it figured out you've pointed me in a clearer direction :)

Dave
 
Upvote 0
Top