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


Postby braves180 » Tue Feb 28, 2012 2:24 am

Hi everyone, i'e been trying to work on a pedometer/exercise counter. I've got everything imported and the program runs. My problem is when I tilt the device the count does increment but, if I leave the device tilted it continuously increments instead of just incrementing by 1. I think its because the device continuously senses a movement(even the VERY slightest) and its running the onSensorChanged function. Any thoughts???

Code: Select all
public void onSensorChanged(SensorEvent event) {
      // TODO Auto-generated method stub

                      //Convert floats to whole numbers
         float x2=Math.round(event.values[0] * 10) /10;
         float y2=Math.round(event.values[1] * 10) /10;
         float z2=Math.round(event.values[2] * 10) /10;
                       //output accelerometer values
         xCoor.setText("X: "+x2);
         yCoor.setText("Y: "+y2);
         zCoor.setText("Z: "+z2);
                       //Verify accelerometer range
         if(y2 >5){
         flag = 0;
         statementcheck.setText("1st if");
         myCount.setText("Your count is: " + count);
         if(y2 < 5){
         flag = 1;
         count ++;
         statementcheck.setText("2nd if");
         myCount.setText("Your count is: " + count);
         //commented code for testing
         /*switch (flag){
         case 0: count = count;
         myCount.setText("Your count is : "+ count);
         case 1:   count ++;
         myCount.setText("Your count is: "+ count);
Posts: 3
Joined: Fri Jan 27, 2012 2:55 am


Re: Accelerometer

Postby Phyll » Tue Feb 28, 2012 12:54 pm

Hi braves,

I think you need to interpret the movement. That is, you need to read the value from the sensor and determine what happened based on previous values. If these values indicate the movement is still going on, you do not increment the count. When the value plus some hysteresis indicates it has turned around, increment the count. Or something like that anyway.

Noise alone will cause the sensor to jitter and give you fluctuating values. You must filter them in some way. The previous paragraph is really nothing more than an overblown filter. Other types of filters may do the same job. Perhaps just an average filter then compute the delta and use the sign of it to operate the counter.

You might also want to study how other pedometers and the like actually work. This may give you some insights into how to make yours.

Hope this helps.

Master Developer
Master Developer
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 15 guests