ANDROID: How to calculate difference between two significant changes in light sensor data?

36 views Asked by At

I am trying to calculate the number of troughs, that is number of times the sensor value goes drastically down, and then up to achieve a down-up (bringing hand near the phone, and then taking it away) hand gesture. This is what I am trying.

protected void onCreate(Bundle savedInstanceState) {
        ..........
        .........

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                printValue();
                new Handler().postDelayed(this, 5000);
            }
        }, 5000);

    }

    public void printValue(){
        textView.setText(" "+arr);
        int count = calculateTrough();
        textView1.setText("Number of troughs: "+count);

        arr.clear();
    }

    public int calculateTrough(){
        int count = 0;

        if(arr == null){
            count = 0;
        }
        else {
            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i) - arr.get(i + 1) > 5.00 && arr.get(i + 2) - arr.get(i + 1) > 5.00)
                    count++;
            }
        }
        return count;
    }

@Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if(sensorEvent.sensor.getType() == Sensor.TYPE_LIGHT){
            arr.add(sensorEvent.values[0]);
        }
    }

Is this the correct approach? The app seems to crash whenever I try to run it in my phone. Upon debugging, it is due to the forloop in the calculateTrough method but I am not able to figure out why. How else should I do it?

0

There are 0 answers