0

I am using Google Nexus 4 and trying to write an app that controls mouse cursor on PC. More spesifically, when i move the phone to the right, mouse cursor should move to the right as well.

I am working on just x-axis right now and following the instructions of the document in this answer.

But i couldn't get the result that i wanted. When i move the phone to the right, the cursor is moved to the right and it moved back to the left. Here is part of my code:

@Override
public void onSensorChanged(SensorEvent event) 
{
    // sampling acceleration 
    do 
    {
        acceleration_x[1] = acceleration_x[1] + (int) event.values[0];
        count_x++;

    } while ( count_x < 64);

    acceleration_x[1] = acceleration_x[1] >> 6;

    count_x = 0;

    //Mechanical Filtering
    if( (acceleration_x[1] <= 3) && (acceleration_x[1] >= -3) )
        acceleration_x[1] = 0;


    // first integration
    velocity_x[1] = velocity_x[0] + acceleration_x[0] + 
            ((acceleration_x[1] - acceleration_x[0]) >> 1);

    // second integration
    position_x[1] = position_x[0] + velocity_x[0] +
            ((velocity_x[1] - velocity_x[0]) >> 1);

    acceleration_x[0] = acceleration_x[1];
    velocity_x[0] = velocity_x[1];


    sendData.sendPacket(position_x[1], 0); // sending data(x,y) to PC

    movement_end_check(event);

    position_x[0] = position_x[1];

}

Integration formula and movement_end_check method were taken from the document.

So,can you help me with this issue ?

Community
  • 1
  • 1
cemm
  • 1
  • 2
  • watch this http://m.youtube.com/watch?v=C7JQ7Rpwn2k – pskink Dec 13 '14 at 02:57
  • Sorry for late answer, I had to log out. Thank you, I've been searching this topic for awhile, so i already watched the video a few times. I agree this presentation is really informative, but there is no code sample to work on. Since i am new with this subject, a little example might be useful. – cemm Dec 13 '14 at 16:37

1 Answers1

0

You're never incrementing the position in your list of velocities/accelerations. Its always writing to 1. That's not how it works- think about it, if always writing to 1 was the answer, why have an array?

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Actually, i am trying to find the change in instant location. Then, sending it PC to set new location of the cursor. To do that, is there any other way that you can suggest ? (by the way, i may use wrong terms to express myself because of my bad English. Really sorry about that.) – cemm Dec 13 '14 at 02:39