Androidフォンを使ってリモコンカーを運転したいです。私はBluetoothイヤホンをレシーバーとして使用しました。私の電話は DTMF トーンを送信し、それがイヤホンで受信されると、マイクロコントローラーによって分析され、車のモーターのオン/オフが行われます。
私は加速度計を使用して電話の動きを検出しました...(前/左/右/後ろ)、動きごとに特定のDTMFトーンを再生します。
それはすべて動作しますが、問題があります:
センサーの値が連続的に変化するため、トーンがたくさん再生されます。
質問: トーンを 1 回だけ再生するにはどうすればよいですか?? (コードのように)
コードは次のとおりです。
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();
助言がありますか ???