九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 5161|回复: 0
打印 上一主题 下一主题

arduino ibox项目实战25-Smoothing实验

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-28 14:23:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,电位器一个
软件平台:WIN7操作系统,android4.0android4.4系统
实验目标:通过ADC读取电压值,每次取十次读到的电压值的平均值。
打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog->Smoothing,Smoothing的示例程序将会被打开,其源码如下:
  1. const int numReadings = 10;

  2. int readings[numReadings];      // the readings from the analog input
  3. int readIndex = 0;              // the index of the current reading
  4. int total = 0;                  // the running total
  5. int average = 0;                // the average

  6. int inputPin = A0;

  7. void setup()
  8. {
  9.   // initialize serial communication with computer:
  10.   Serial.begin(9600);
  11.   // initialize all the readings to 0:
  12.   for (int thisReading = 0; thisReading < numReadings; thisReading++)
  13.     readings[thisReading] = 0;
  14. }

  15. void loop() {
  16.   // subtract the last reading:
  17.   total = total - readings[readIndex];
  18.   // read from the sensor:
  19.   readings[readIndex] = analogRead(inputPin);
  20.   // add the reading to the total:
  21.   total = total + readings[readIndex];
  22.   // advance to the next position in the array:
  23.   readIndex = readIndex + 1;

  24.   // if we're at the end of the array...
  25.   if (readIndex >= numReadings)
  26.     // ...wrap around to the beginning:
  27.     readIndex = 0;

  28.   // calculate the average:
  29.   average = total / numReadings;
  30.   // send it to the computer as ASCII digits
  31.   Serial.println(average);
  32.   delay(1);        // delay in between reads for stability
  33. }
复制代码
       将电位器的两端分别接5VGND,中间的端口接A0,运行程序,观察调试窗口上的打印信息。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-6-15 03:48 , Processed in 0.019085 second(s), 18 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表