how to control accelerometer and use it in android remot controlled car

628 Views Asked by At

i want to drive a remote controlled car using android phone. i used Bluetooth earphone works as receiver . my phone sends DTMF tones and when its received by the earphone it's analyzed by microcontroller to turn on / off the car motors .

i used accelerometer to detect phone motion ... ( front / left /right / back ) and on each movement it plays a specific DTMF tone .

it all works but there is a problem :

the tone is played alot as the sensors values changes continuously !

the question : how can i make the tone played only one time ?? ( as in the code )

here is the code :

 X = (TextView)findViewById(R.id.x);
    Y = (TextView)findViewById(R.id.y);
    Z = (TextView)findViewById(R.id.z);

    toneGenerator= new ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);



    sel = new SensorEventListener(){  
public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }  

        public void onSensorChanged(SensorEvent event) {  

              float[] values = event.values; 
              float x = values[0];  
              float   xx = Math.round(x) ;
              float y = values[1]; 
              float   yy = Math.round(y) ;
              float z = values[2];  
              float   zz = Math.round(z) ;

              X.setText("x:" + xx);  

              Y.setText("y:" + yy);
              Z.setText("z:" + zz);

              if (xx>-1 & xx<1 & zz>3 & zz<8 ) {
                state.setText ("normal");

                toneGenerator.startTone(ToneGenerator.TONE_DTMF_5);


                   new CountDownTimer(50, 500) { 
                    public void onTick(long millisUntilFinished) { } 
                    public void onFinish() { 
                     toneGenerator.stopTone();
                  }}.start();
              }

              if (xx>-1 & xx<1 & zz>8 ) {

                state.setText ("front");

                     toneGenerator.startTone(ToneGenerator.TONE_DTMF_2);
                       new CountDownTimer(50, 500) { 
                        public void onTick(long millisUntilFinished) { } 
                        public void onFinish() { 
                         toneGenerator.stopTone();
                      }}.start();

any suggestions ???

1

There are 1 best solutions below

3
On

That is a little difficult since humans can't stand still with any part of the body for 100%.

Have you tried placing the phone on a table to see what it happens ?