<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## **信號平滑** > 本例中程序不斷的讀取模擬信號值,計算平均值,并且將平均值輸出到電腦。本例所展示的方法能夠平滑飄忽不定或忽上忽下的模擬信號。例子中也展示了使用數組存儲數據的方法。 ### **所需硬件** * Arduino板或Genuino板 * 10kΩ電位器 * 連接線 ### **電路搭建** ![圖片來自官網](http://img.blog.csdn.net/20160511153938548)? 將電位器兩側的引腳分別連接到5V和GND,將中間引腳連接到A0。 ### **原理圖** ![圖片來自官網](http://img.blog.csdn.net/20160511153950736) ### **代碼** 下列代碼按照次序存儲10次讀取值到數組,將這十個值求和,并且求取平均數,得到的平均數會起到平滑作用。由于計算平均值的時刻是每次添加新的傳感器值時(每增加一個就計算一次)。因此,每次代碼執行的時間都是相同的。 你可以增加數組的容量,來達到更好的平滑。 ~~~ /* 信號平滑 不斷讀取模擬信號值、求取平均值后輸出到電腦。將10次數據存儲到數組并且不斷計算平均數。 電路連接: * 模擬信號傳感器(電位器即可)連接到A0 代碼是公開的. */ // 保存的數據個數越多,平滑效果就越好,不過每次計算時間就越長。 //注意:應該使用常量來定義數組容量,而非變量 // 我們且將元素個數限制為10 const int numReadings = 10; int readings[numReadings]; // 存儲數組 int readIndex = 0; // 當前讀到的位置(數組下標) int total = 0; // 和 int average = 0; // 平均值 int inputPin = A0; void setup() { // 初始化和電腦的串口連接: Serial.begin(9600); // 初始化數組,用0填充: for (int thisReading = 0; thisReading < numReadings; thisReading++) { readings[thisReading] = 0; } } void loop() { // 總和扣除上次的值: total = total - readings[readIndex]; // 從傳感器讀新值: readings[readIndex] = analogRead(inputPin); // 將新值加到總和: total = total + readings[readIndex]; // 下表加一: readIndex = readIndex + 1; // 如果在數組的最后 if (readIndex >= numReadings) { // 從頭開始: readIndex = 0; } // 計算平均值: average = total / numReadings; // 作為ASCII字符傳到電腦 Serial.println(average); delay(1); // 為穩定,延遲1毫秒。 } ~~~ ### **相關資料** [array](https://www.arduino.cc/en/Reference/Array)? [if](https://www.arduino.cc/en/Reference/If)? [for](https://www.arduino.cc/en/Reference/For)? [serial](https://www.arduino.cc/en/Reference/Serial)?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看