九鼎创展论坛

标题: arduino ibox项目实战25-Smoothing实验 [打印本页]

作者: armeasy    时间: 2014-10-28 14:23
标题: arduino ibox项目实战25-Smoothing实验
硬件平台: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,运行程序,观察调试窗口上的打印信息。






欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) Powered by Discuz! X3.2