1

im trying to get 100 accelerometer readings in 1 second, but instead of 100 different readings, it gives me 1 same reading 100 times.

I'm trying to display these 100 accelerometer readings when i press a button in my app

my MainActivity has this variable named

float ax;

which is where the accelerometer reading (of x axis) is continuously stored.

and i have this button code in my OnCreate() method

 button = (Button)findViewById(R.id.startbutton); //inits button
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {               
            BuildTable();
        }
    });

which triggers the BuildTable() function (which is displays the output on the app itself)

my BuildTable() has something like this

 public void BuildTable()
{       

    TableRow trow = new TableRow(this);
    TextView text = new TextView(this);       

    int count = 0;

    while (count < 100)
    {

        if(System.currentTimeMillis() - lasttime >= 10)
        {

            lasttime = System.currentTimeMillis();              
            text = new TextView(this);
            Log.d("MyApp","ax is: "+ax);
            text.setText(""+ax);
            trow = new TableRow(this);
            trow.addView(text);
            table.addView(trow,count);
            count++;

        }

    }
}

and the OnSensorChanged() function looks like this

@Override
public void onSensorChanged(SensorEvent event) {     
    ax = event.values[0];      

}

which just updates ax.

How does the button click pause the updating of ax? how do i fix this?

Cheers

Last Man Standing
  • 103
  • 1
  • 1
  • 7
  • so you have verified that if(System.currentTimeMillis() - lasttime >= 10) is firing? Is the reading sensible? Is it the exact same value every time you run the app or is different each time? – Kyle Sweet Dec 13 '16 at 19:20
  • How do you know it is even the accelerometer that is triggering onSensorChanged and not some other sensor? – Kyle Sweet Dec 13 '16 at 19:32
  • The timing code works, it's able to give me a reading 100 times, the real problem is that it's the same reading 100 times – Last Man Standing Dec 13 '16 at 19:36

2 Answers2

0

Try filtering results in the onSensorChanged function using

@Override
public void onSensorChanged(SensorEvent event)
{    
   if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
   { 
       ax = event.values[0];      
   }
}
Kyle Sweet
  • 306
  • 1
  • 3
  • 15
  • uh...I don't know what thread onSensorChanged runs in, though. If it runs on the same thread as BuildTable, you're screwed. Check out this post where they describe registering a listener - http://stackoverflow.com/questions/17513352/acclerometer-sensor-in-separate-thread – Kyle Sweet Dec 13 '16 at 19:47
  • the answer didnt do anything. as already stated before, the sensor is already getting readings, regardless if it's the correct readings or not. the Real problem is that the 'state' of reading sensors by onSensorChanged() freezes when a while loop is entered – Last Man Standing Dec 26 '16 at 01:59
0

I found a way to get around the loop freezing onSensorChanged()!

I essentially had to implement multithreading with the help of runnables and 'flag' variables (note the 'flag' variables must be static).

I placed my BuildTable() function in a new Runnable, which was then inserted into a thread. So there's a main thread (i suppose for the GUI), and a separate thread, which had to be modified to just exclusively receive and store sensor readings.

Then the actual displaying of these variables in a table was executed in the main thread (i think it's called the UIThread), with the help of 'flag' variables which were also modified by my BuildTable() thread (hence the 'flag' variables must be static).

I can add code snippets for more hints, but perhaps later or when someone asks for it, it's a bit early in the morning

Cheers!

Last Man Standing
  • 103
  • 1
  • 1
  • 7