Arduino 内置示例——控制结构部分示例

  • 数组:一个在For循环的变量举例了怎样使用一个数组。
  • For循环:通过for循环来控制多个LED灯
  • If声明条件:使用一个‘if 声明’,通过改变输入条件来改变输出条件
  • Switch Case:怎样在非连续的数值里选择。
  • Switch Case 2:第二个switch-case的例子,展示怎样根据在串口收到的字符来采取不同的行为
  • While 声明条件:当一个按键被读取,怎样用一个while循环来校准一个传感器。
image.png
数组
  • 在for循环的变量示范了怎么用一个数组。一个数组是一个含多部分的变量。 如果你把一个变量理解为一个装数据的杯,你可以把一个数组理解为制冰格。它就像一系列的粘在一起的杯,上面装着一些最大的数值。

  • for循环示范了怎样点亮连接到Arduino或Genuino开发板上pin2到pin7的一系列的LED灯,这些引脚需要连续标记数字,而LED灯需要按顺序打开。

  • 该例子示范怎样顺序打开一些引脚,这些引脚的序号是不连续而且不按次序。为了实现这个目的,你可以把这些引脚序号放到一个数组里,然后在循环里重申完这个数组。

  • 这个例子充分利用了通过220 ohm电阻连接在pin2到pin7的6个LED灯,就像在for循环里。然而,这里LED灯的顺序由它们在数组里的顺序确定,而不是他们的物理顺序。

  • 这个把引脚放到数组的方式很方便。你不必把这些引脚一个接一个排好顺序,或者同一个顺序。你可以重新排列数组,按任何你想要的顺序。

通过220 ohm电阻串联,连接6个LED灯到数字引脚pin2-pin7上。


image.png

示例代码:

int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = {
  2, 7, 4, 6, 5, 3
};       // an array of pin numbers to which LEDs are attached
int pinCount = 6;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}
数组跑马灯.gif

For循环
  • 通常你想循环一系列引脚,然后每个引脚做一些事情。如这个例子通过for()循环来重复点亮pin2-7上的6个LED灯。这些LED灯通过digitalWrite() 和delay()按顺序点亮和熄灭。

通过220 ohm电阻串联,连接6个LED灯到数字引脚pin2-pin7上。

image.png
int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 8; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 7; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}
for循环跑马灯.gif

If声明条件

这个if()声明是所有程序控制结构里最基本的部分。它允许你来使有些事情是否发生,取决于是否符合一些给定的条件。它看起来像这样:

if (someCondition) {
   // do stuff if the condition is true
}

有一个公共变量叫if-else,看起来像这样:

if (someCondition) {
   // do stuff if the condition is true
} else {
   // do stuff if the condition is false
}

也有else-if,当第一条件为真时你可以检查第二条件:

if (someCondition) {
   // do stuff if the condition is true
} else if (anotherCondition) {
   // do stuff only if the first condition is false
   // and the second condition is true
}

你任何时候都可以用到if声明。下面的例子如果模拟输入引脚读取的值超过阈值,就会打开pin13的LED灯(内置在很多Arduino上)。

image.png

示例代码:

在下面的代码里,一个叫analogValue的变量用来保存从电位计(连接到开发板的模拟引脚pin0)读取的数据。然后对比这个数据和阈值。如果模拟值在设置的阈值上面,打开pin13的内置LED灯。如果低于阈值,LED保持关闭。

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}
if控制led.gif

Switch Case
  • 一个if声明允许你选择两个分开的选项,真或假。当有超过2个的选项,你可以用多个if声明,或者你可以用switch声明。switch允许你选择多个选项。这个教程示范怎样用它在四种光电阻的状态下切换开关:全黑,昏暗,中等,明亮。

  • 这个程序首先读取光敏电阻。然后它用map()函数来使它的输出值符合四个值之一:0,1,2,3。最后,用switch()声明来打印对应的信息到电脑里。

光敏电阻通过一个分压电路连接到模拟输入pin0。一个10k ohm电阻补充分压器的另一部分,从模拟输入pin0连到地。analogRead()函数从这个电路返回一个0-600的范围值。

image.png

示例代码:

// these constants won't change. They are the lowest and highest readings you
// get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the range value:
  switch (range) {
    case 0:    // your hand is on the sensor
      Serial.println("dark");
      break;
    case 1:    // your hand is close to the sensor
      Serial.println("dim");
      break;
    case 2:    // your hand is a few inches from the sensor
      Serial.println("medium");
      break;
    case 3:    // your hand is nowhere near the sensor
      Serial.println("bright");
      break;
  }
  delay(1);        // delay in between reads for stability
}
image.png
image.png

Switch Case 2
  • 一个if声明允许你选择两个分开的选项,真或假。当有超过2个的选项,你可以用多个if声明,或者你可以用switch声明。switch允许你选择多个选项。

  • 这个教程示范怎样用switch根据收到的字节数据来打开多个LED灯中的一个。并且根据字符a,b,c,d,e来打开特定的LED灯。

5个LED灯通过串联的220 ohm电阻连接到数字引脚pin 2,3,4,5,6。

为了使程序工作,你的开发板需要连接到电脑。在Arduino IDE上打开串口监视器,并且发送字符 a,b,c,d,e来点亮相应的LED灯,或者其他东西来关闭它们。

image.png

示例代码:

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pins:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    // do something different depending on the character received.
    // The switch statement expects single number values for each case; in this
    // example, though, you're using single quotes to tell the controller to get
    // the ASCII value for the character. For example 'a' = 97, 'b' = 98,
    // and so forth:

    switch (inByte) {
      case 'a':
        digitalWrite(2, HIGH);
        break;
      case 'b':
        digitalWrite(3, HIGH);
        break;
      case 'c':
        digitalWrite(4, HIGH);
        break;
      case 'd':
        digitalWrite(5, HIGH);
        break;
      case 'e':
        digitalWrite(6, HIGH);
        break;
      default:
        // turn all the LEDs off:
        for (int thisPin = 2; thisPin < 7; thisPin++) {
          digitalWrite(thisPin, LOW);
        }
    }
  }
}
Switch Case 2.gif

While 声明条件
  • 有时候当给定的条件为真时,你想程序所有东西都停止。你可以用while循环来做这件事。这个例子示范怎样用while循环来校准传感器的值。

  • 在主循环里,下面程序在模拟引脚pin0里读取光敏电阻的值,并用它来使pin9的LED灯变亮或者变暗。而当一个按键(连接到数字引脚pin2)被按下时,程序运行一个叫 calibrate()的函数,寻找模拟传感值的最大和最小值。当你放开按键时,程序会继续主循环。

  • 这个方法可以使你在光源环境改变的时候更新光敏电阻的最大值和最小值。

把你的模拟传感器(光敏电阻或者其他)通过10k ohm电阻下拉到地,再连接到模拟输入引脚pin0。连接你的按键(通过10k ohm电阻下拉到地)到数字引脚pin2。把你的LED灯串联一个220 ohm电阻连接到数字引脚pin9。

image.png

示例代码:

// These constants won't change:
const int sensorPin = A0;       // pin that the sensor is attached to
const int ledPin = 9;           // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2;        // pin that the button is attached to


// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value


void setup() {
  // set the LED pins as outputs and the switch pin as input:
  pinMode(indicatorLedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  // while the button is pressed, take calibration readings:
  while (digitalRead(buttonPin) == HIGH) {
    calibrate();
  }
  // signal the end of the calibration period
  digitalWrite(indicatorLedPin, LOW);

  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

void calibrate() {
  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin, HIGH);
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // record the maximum sensor value
  if (sensorValue > sensorMax) {
    sensorMax = sensorValue;
  }

  // record the minimum sensor value
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;
  }
}

你可能感兴趣的:(Arduino 内置示例——控制结构部分示例)