校准
    这个例子展示一种矫正传感输入的技巧。这个开发板在启动时让传感器读取5秒钟,记录最高和最低值的轨迹。这些在程序里执行5秒的传感读取数定义为循环下一次读取的期望值的最小值和最大值。
    硬件要求

    • Arduino 或者 Genuino开发板
    • LED
    • 模拟感应器 (光敏电阻可以做到)
    • 10k ohm 电阻
    • 220 ohm 电阻
    • 连接线
    • 面包板

    电路
    模拟传感器(如电位计,光传感器)在模拟输入A2上。LED在数字引脚pin9上。
    模拟-校准 - 图1
    把一个LED灯通过220 ohm限流电阻连接到数字引脚pin9。把一个光敏电阻的一端连接到5V。另一端连接到pin0,并通过10k ohm电阻下拉到地。
    原理图
    模拟-校准 - 图2
    样例代码

    • 在启动函数前,你要为最大值和最小值设置初始值,如:

      1. int sensorMin = 1023; // minimum sensor value
      2. int sensorMax = 0; // maximum sensor value
    • 这些可能看起来反了。开始时,你设置最大的高电平,然后读取任何比它低的值,将它作为新的最小值保存。同样地,你设置最小的低电平,然后读取任何比它高的值,将它作为新的最大值保存。就像这样:

      1. // calibrate during the first five seconds
      2. while (millis() < 5000) {
      3. sensorValue = analogRead(sensorPin);
      4. // record the maximum sensor value
      5. if (sensorValue > sensorMax) {
      6. sensorMax = sensorValue;
      7. }
      8. // record the minimum sensor value
      9. if (sensorValue < sensorMin) {
      10. sensorMin = sensorValue;
      11. }
      12. }
    • 这种方式下,你做的更多读取可能会根据这个最大值和最小值按比例分配。就像这样:

      1. // apply the calibration to the sensor reading
      2. sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
    • 这里有全部程序:

      1. /*
      2. Calibration
      3. Demonstrates one technique for calibrating sensor input. The
      4. sensor readings during the first five seconds of the sketch
      5. execution define the minimum and maximum of expected values
      6. attached to the sensor pin.
      7. The sensor minimum and maximum initial values may seem backwards.
      8. Initially, you set the minimum high and listen for anything
      9. lower, saving it as the new minimum. Likewise, you set the
      10. maximum low and listen for anything higher as the new maximum.
      11. The circuit:
      12. * Analog sensor (potentiometer will do) attached to analog input 0
      13. * LED attached from digital pin 9 to ground
      14. created 29 Oct 2008
      15. By David A Mellis
      16. modified 30 Aug 2011
      17. By Tom Igoe
      18. http://www.arduino.cc/en/Tutorial/Calibration
      19. This example code is in the public domain.
      20. */
      21. // These constants won't change:
      22. const int sensorPin = A0; // pin that the sensor is attached to
      23. const int ledPin = 9; // pin that the LED is attached to
      24. // variables:
      25. int sensorValue = 0; // the sensor value
      26. int sensorMin = 1023; // minimum sensor value
      27. int sensorMax = 0; // maximum sensor value
      28. void setup() {
      29. // turn on LED to signal the start of the calibration period:
      30. pinMode(13, OUTPUT);
      31. digitalWrite(13, HIGH);
      32. // calibrate during the first five seconds
      33. while (millis() < 5000) {
      34. sensorValue = analogRead(sensorPin);
      35. // record the maximum sensor value
      36. if (sensorValue > sensorMax) {
      37. sensorMax = sensorValue;
      38. }
      39. // record the minimum sensor value
      40. if (sensorValue < sensorMin) {
      41. sensorMin = sensorValue;
      42. }
      43. }
      44. // signal the end of the calibration period
      45. digitalWrite(13, LOW);
      46. }
      47. void loop() {
      48. // read the sensor:
      49. sensorValue = analogRead(sensorPin);
      50. // apply the calibration to the sensor reading
      51. sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
      52. // in case the sensor value is outside the range seen during calibration
      53. sensorValue = constrain(sensorValue, 0, 255);
      54. // fade the LED using the calibrated value:
      55. analogWrite(ledPin, sensorValue);
      56. }

      [Get Code]
      更多

    • while()

    • millis()
    • constrain()
    • map()
    • If
    • AnalogInOutSerial - 读取一个模拟输入引脚,按比例划分读数,然后用这个数据来熄灭或者点亮一个LED灯
    • AnalogInput - 用电位计来控制LED灯闪烁
    • AnalogWriteMega - 用一个Arduino或者Genuino Mega开发板来使12个LED灯一个接一个逐渐打开和熄灭
    • Fading - 用模拟输出(PWM引脚)来使LED灯变亮或者变暗
    • Smoothing - 使多个模拟输入引脚的读取值变得平滑