While 循环

    • 有时候当给定的条件为真时,你想程序所有东西都停止。你可以用while循环来做这件事。这个例子示范怎样用while循环来校准传感器的值。
    • 在主循环里,下面程序在模拟引脚pin0里读取光敏电阻的值,并用它来使pin9的LED灯变亮或者变暗。而当一个按键(连接到数字引脚pin2)被按下时,程序运行一个叫 calibrate()的函数,寻找模拟传感值的最大和最小值。当你放开按键时,程序会继续主循环。
    • 这个方法可以使你在光源环境改变的时候更新光敏电阻的最大值和最小值。

    硬件要求

    • Arduino or Genuino开发板
    • 按键或者切换开关
    • 光敏电阻或者其他模拟传感器
    • 2 10k ohm 电阻
    • 面包板

    电路
    把你的模拟传感器(光敏电阻或者其他)通过10k ohm电阻下拉到地,再连接到模拟输入引脚pin0。连接你的按键(通过10k ohm电阻下拉到地)到数字引脚pin2。把你的LED灯串联一个220 ohm电阻连接到数字引脚pin9。
    控制结构-While 声明条件 - 图1
    图是用 Fritzing 软件绘制。
    原理图
    控制结构-While 声明条件 - 图2
    样例代码

    1. /*
    2. Conditionals - while statement
    3. This example demonstrates the use of while() statements.
    4. While the pushbutton is pressed, the sketch runs the calibration routine.
    5. The sensor readings during the while loop define the minimum and maximum
    6. of expected values from the photo resistor.
    7. This is a variation on the calibrate example.
    8. The circuit:
    9. * photo resistor connected from +5V to analog in pin 0
    10. * 10K resistor connected from ground to analog in pin 0
    11. * LED connected from digital pin 9 to ground through 220 ohm resistor
    12. * pushbutton attached from pin 2 to +5V
    13. * 10K resistor attached from pin 2 to ground
    14. created 17 Jan 2009
    15. modified 30 Aug 2011
    16. by Tom Igoe
    17. This example code is in the public domain.
    18. http://www.arduino.cc/en/Tutorial/WhileLoop
    19. */
    20. // These constants won't change:
    21. const int sensorPin = A2; // pin that the sensor is attached to
    22. const int ledPin = 9; // pin that the LED is attached to
    23. const int indicatorLedPin = 13; // pin that the built-in LED is attached to
    24. const int buttonPin = 2; // pin that the button is attached to
    25. // These variables will change:
    26. int sensorMin = 1023; // minimum sensor value
    27. int sensorMax = 0; // maximum sensor value
    28. int sensorValue = 0; // the sensor value
    29. void setup() {
    30. // set the LED pins as outputs and the switch pin as input:
    31. pinMode(indicatorLedPin, OUTPUT);
    32. pinMode(ledPin, OUTPUT);
    33. pinMode(buttonPin, INPUT);
    34. }
    35. void loop() {
    36. // while the button is pressed, take calibration readings:
    37. while (digitalRead(buttonPin) == HIGH) {
    38. calibrate();
    39. }
    40. // signal the end of the calibration period
    41. digitalWrite(indicatorLedPin, LOW);
    42. // read the sensor:
    43. sensorValue = analogRead(sensorPin);
    44. // apply the calibration to the sensor reading
    45. sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
    46. // in case the sensor value is outside the range seen during calibration
    47. sensorValue = constrain(sensorValue, 0, 255);
    48. // fade the LED using the calibrated value:
    49. analogWrite(ledPin, sensorValue);
    50. }
    51. void calibrate() {
    52. // turn on the indicator LED to indicate that calibration is happening:
    53. digitalWrite(indicatorLedPin, HIGH);
    54. // read the sensor:
    55. sensorValue = analogRead(sensorPin);
    56. // record the maximum sensor value
    57. if (sensorValue > sensorMax) {
    58. sensorMax = sensorValue;
    59. }
    60. // record the minimum sensor value
    61. if (sensorValue < sensorMin) {
    62. sensorMin = sensorValue;
    63. }
    64. }

    [Get Code]
    更多

    • while()
    • digitalRead()
    • digitalWrite()
    • analogRead()
    • analogWrite()
    • map()
    • constrain()
    • if()
    • 数组:一个在For循环的变量举例了怎样使用一个数组。
    • IfStatementConditional:通过for循环来控制多个LED灯
    • If声明条件:使用一个‘if 声明’,通过改变输入条件来改变输出条件
    • Switch Case:怎样在非连续的数值里选择。
    • Switch Case 2:第二个switch-case的例子,展示怎样根据在串口收到的字符来采取不同的行为
    • While 声明条件:当一个按键被读取,怎样用一个while循环来校准一个传感器。