具備濾波功能的倒車雷達(使用陣列) ver1.2 by 力維

上一個範例中我們使用了移動平滑法來降低超音波感應器本身的雜訊, 今天我們改用陣列來處理。 整個程式就會變得簡潔多囉! 程式可直接複製或按我下載

==============================================

import lejos.nxt.*;
import lejos.util.Delay;
class radar_v1_2
{
   public static void main(String args[])
   {
      Button.ESCAPE. addButtonListener(new ButtonListener()
      {
            public void buttonPressed(Button b){System.exit(1);}
            public void buttonReleased(Button b){}
       });
      
       UltrasonicSensor us = new UltrasonicSensor(SensorPort. S1);
       int[] distance = new int[8]; //宣告一個長度8的整數陣列來存放超音波感應器值
       int fre=0;  //指定頻率初始值為0
       while(true)
       {
          for(int i=distance.length-1;i>0;i–)
          {
              distance[i]= distance[i-1];
          }
        distance[0] = us.getDistance();
       
        for(int j=0; j<distance.length-1;j++)

        {
                            fre += distance[i];
        }
       Sound.playTone(fre/distance. length*10,100);
       LCD.drawInt(fre/distance. length,0,0);
       Delay.msDelay(100);
       LCD.clear();
       fre=0;
      }
   }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *