这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版 | |||
ocrobot:alpha:kitone:tutorial18 [2019/07/15 02:38] – 董凯萍 | ocrobot:alpha:kitone:tutorial18 [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== Smoothing(平滑处理) ====== | ||
+ | <WRAP left round info 100%> | ||
+ | 这个例子演示了不断读取模拟输入的数据,计算连续数据的平均值并且显示到电脑上。这个例子对于平滑不规则的跳跃性的输出值很有用,同样也演示了数组的用法。 | ||
+ | </ | ||
+ | |||
+ | ===== ALPHA MEGA328-U核心 ===== | ||
+ | ==== 硬件 ==== | ||
+ | |||
+ | * [[ocrobot: | ||
+ | * [[ocrobot: | ||
+ | * [[ocrobot: | ||
+ | |||
+ | ==== 搭建电路 ==== | ||
+ | |||
+ | - ALPHA MEGA328-U模块插入并行扩展板1号槽位。 | ||
+ | - ALPHA 电位器模块插入并行扩展版2号槽位。 | ||
+ | - USB线连接计算机与ALPHA MEGA328-U。 | ||
+ | {{ocrobot: | ||
+ | ==== 代码 ==== | ||
+ | |||
+ | <code cpp> | ||
+ | /* | ||
+ | Smoothing | ||
+ | 连续读取模拟输入值,计算连续数据的平均值并且显示在电脑上。 | ||
+ | 把十个数据存到数组中不断地计算他们的平均值。 | ||
+ | 定义数字的样本并且持续监测,数值越高,平滑的数据就越多,对应输入端的数据输出就会越慢。 | ||
+ | 使用常量而不是变量来决定数组的大小 | ||
+ | */ | ||
+ | const int numReadings = 10; | ||
+ | int readings[numReadings]; | ||
+ | int index = 0; // 当前读取的序号 | ||
+ | int total = 0; // 总数 | ||
+ | int average = 0; // 平均数 | ||
+ | int inputPin = A0; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | for (int thisReading = 0; thisReading < numReadings; | ||
+ | readings[thisReading] = 0; | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | total = total - readings[index]; | ||
+ | readings[index] = analogRead(inputPin); | ||
+ | total = total + readings[index]; | ||
+ | index = index + 1; // 增加到数组的下一个位置 | ||
+ | if (index >= numReadings) | ||
+ | index = 0; // 从头开始 | ||
+ | average = total / numReadings; | ||
+ | Serial.println(average); | ||
+ | delay(1); | ||
+ | } | ||
+ | </ | ||
+ | [[ocrobot: |