I'm developing a simple counter app which counts when the user move their phone along the x axis(positive or negative) about 90 degrees.i'm using the accelerometer for measuring the acceleration cause by moving the phone and using this for counting. but there is a problem,the accuracy is not good,sometimes it doesn't count and sometimes it count twice. this is my code,i want to know if there is a way to get good accuracy?
@Override
protected void onResume() {
super.onResume();
SnManager.registerListener(this,acc,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(!stop) {
if (event.values[0] > 14) {
Count++;
txt_count.setText("" + Count);
values.add("OK");
}
values.add(""+event.values[0]);
lst.invalidate();
}
}
You can check the event timestamp to determine if it's an already measured value, but I would suggest that you implement a kind of smoothing for example a rolling average over the last 3-5 values. That makes your values a lot smoother and better to handle. Here is an example with double values, you can change to whatever your want: ´
public class Rolling {
private int size;
private double total = 0d;
private int index = 0;
private double samples[];
public Rolling(int size) {
this.size = size;
samples = new double[size];
for (int i = 0; i < size; i++) samples[i] = 0d;
}
public void add(double x) {
total -= samples[index];
samples[index] = x;
total += x;
if (++index == size) index = 0;
}
public double getAverage() {
return total / size;
}
}
´
Do you need further help?