Accelerometer - how to differentiate between left and right?

Put your problem here if it does not fit any of the other categories.

Accelerometer - how to differentiate between left and right?

Postby Sigma » Tue Feb 15, 2011 1:49 pm

I'm building a dice roller app. As a nice extra feature I'd like to make a phone shake roll the dice. I've managed to get a shake detect, but in order to mimic a real dice roll I want the application to respond if and only if the phone has been moved a minimum distance right - left - right in about 1,5s.

The minimum distance is no problem (in order to make sure small vibrations doesn't set it of for example), but how do I find out if the move was left or right?

I'm currently only using the x-asis. I had hoped that x - last_x would be negative for one direction, but it doesn't seem like it is.

My current code for onSensorChange:
Code: Select all
    @Override
    public void onSensorChanged(SensorEvent event) {
           Sensor mySensor = event.sensor;
           if (mySensor.getType() == SensorManager.SENSOR_ACCELEROMETER) {
              long curTime = System.currentTimeMillis();

           //if it's been longer than 1,5s, reset the clock   
               if ((curTime - ShakeStart) > 1500 || ShakeStart==0)
               {
                  TimesShaken=0;
                  ShakeStart=curTime;
                  alreadyTriggeredShake=false;
               }
              
               // only allow one update every 100ms.
               if ((curTime - lastUpdate) > 30) {
                 long diffTime = (curTime - lastUpdate);
                  lastUpdate = curTime;
                  
                   x = event.values[0];
                 float speed = Math.abs(x - last_x)/ diffTime * 10000;
                 float signedSpeed = (x - last_x)/ diffTime * 10000;
                 if (speed > SHAKE_THRESHOLD) {
                    float shakeDifference=x - last_x;
                if(shakeDifference>1)
                     {
                      Toast.makeText(this, "Shake detected at signedSpeed:" + Float.toString(signedSpeed) + " Distance:" + Math.abs(shakeDifference),Toast.LENGTH_SHORT).show();
                     }
                    // Here I wish to add a parameter boolean "directionRight" or "directionLeft", otherwise these two if-statements doesn't make much sense. How do I add the code necessary for this?
                    if(!LastShakeWasPositive && !alreadyTriggeredShake && Math.abs(shakeDifference)>1.5 && curTime-ReadyForNextShake>4000)                    
                    {
                       TimesShaken++;
                       LastShakeWasPositive=true;
                    }
                    else if(signedSpeed<0 && LastShakeWasPositive  && !alreadyTriggeredShake && Math.abs(shakeDifference)>1.5 && curTime-ReadyForNextShake>4000)
                    {
                       TimesShaken++;
                       LastShakeWasPositive=false;
                    }
                 }

                        //make a roll and reset all the timers, and the 4s timeout before a new roll can be made
                 if(TimesShaken>=3 && !alreadyTriggeredShake)
                 {
                     RollDice();
                     TimesShaken=0;
                     alreadyTriggeredShake=true;
                     ReadyForNextShake=curTime;
                     ShakeStart=curTime;
                 }
                 last_x = x;
               }
           } 
    }   
Sigma
Developer
Developer
 
Posts: 33
Joined: Thu Dec 16, 2010 12:34 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests