初始化提交

This commit is contained in:
王立帮
2024-07-19 10:16:00 +08:00
parent 4c7b571f20
commit 4a2d56dcc4
7084 changed files with 741212 additions and 63 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 841 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 980 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -0,0 +1,13 @@
## 输入/输出
输入/输出所包含的指令主要分为四部分控制管脚的输入输出按信号类型可分为数字信号和模拟信号、中断、脉冲长度及ShiftOut。
- 输入输出:数字输入、数字输出、模拟输入、模拟输出
- 中断控制:定义中断,取消中断
- 脉冲长度
- 移位输出
<img src="{default}/images/inout/input-output.png" alt="输入/输出分类" style="zoom:50%;" />

View File

@@ -0,0 +1,41 @@
## 数字输出
<img src="{default}/images/inout/digital-pin-output.png" alt="数字输出" style="zoom:67%;" />
```arduino
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
```
### 描述
> 给一个数字引脚写入HIGH或者LOW。
### 参数
- 管脚: 引脚编号如1510A0A3
- 值: 高 或 低
### 范例
将13号端口设置为高电平延迟一秒然后设置为低电平再延迟一秒如此往复。
<img src="{default}/images/inout/digital-pin-output-example.png" alt="数字输出示例" style="zoom:67%;" />
```arduino
void setup(){
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">数字13号引脚难以作为数字输入使用因为大部分的控制板上使用了一颗LED与一个电阻连接到他。如果启动了内部的20K上拉电阻他的电压将在1.7V左右而不是正常的5V因为板载LED串联的电阻把他使他降了下来这意味着他返回的值总是LOW。如果必须使用数字13号引脚的输入模式需要使用外部上拉下拉电阻</div>
</div>

View File

@@ -0,0 +1,42 @@
## 数字输入
<img src="{default}/images/inout/digital-input.png" alt="数字输入" style="zoom:67%;" />
```arduino
digitalRead(2);
```
### 描述
> 读取指定引脚的值HIGH或LOW。
### 参数
- 管脚: 引脚编号如1,5,10,A0,A3
### 返回
HIGH 或 LOW
### 范例
读取数字0号引脚的值并通过串口打印出来。
<img src="{default}/images/inout/digital-input-example.png" alt="数字输入示例" style="zoom:67%;" />
```arduino
void setup(){
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(digitalRead(2));
delay(1000);
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">如果引脚悬空digitalRead()会返回HIGH或LOW随机变化。</div>
</div>

View File

@@ -0,0 +1,44 @@
## 模拟输出
<img src="{default}/images/inout/analog-pin-output.png" alt="模拟输出" style="zoom:67%;" />
```arduino
analogWrite(3, 100);
```
### 描述
> 从一个引脚输出模拟值PWM。 可用于让LED以不同的亮度点亮或驱动电机以不同的速度旋转。
### 参数
- 管脚:引脚编号如3,5,6,9,10,11不同的开发板模拟输入引脚数量不一样。
- 赋值:0完全关闭到255完全打开之间。
### 范例
控制LED实现呼吸灯效果。
<img src="{default}/images/inout/analog-pin-output-example.png" alt="模拟输出示例" style="zoom:67%;" />
```arduino
void setup(){
}
void loop(){
for (int i = 0; i <= 255; i = i + (1)) {
analogWrite(10, i);
delay(10);
}
for (int i = 255; i >= 0; i = i + (-1)) {
analogWrite(10, i);
delay(10);
}
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">analogWrite函数与模拟引脚、analogRead函数没有直接关系。 在大多数Arduino板ATmega168或ATmega328只有引脚356910和11可以实现该功能。 在Arduino Mega上引脚2到13可以实现该功能。</div>
</div>

View File

@@ -0,0 +1,43 @@
## 模拟输入
<img src="{default}/images/inout/analog-pin-input.png" alt="模拟输入" style="zoom: 67%;" />
```arduino
analogRead(A0);
```
### 描述
> 从指定的模拟引脚读取数据值。
>
> Arduino板包含一个6通道Mini和Nano有8个通道Mega有16个通道10位模拟数字转换器。这意味着它将0至5伏特之间的输入电压映射到0至1023之间的整数值。
### 参数
- 管脚: 引脚编号如A0,A1,A2,A3不同的开发板模拟输入引脚数量不一样。
### 返回
从0到1023的整数值
### 范例
读取模拟A0引脚的值并通过串口打印出来。
<img src="{default}/images/inout/analog-pin-input-example.png" alt="模拟输入示例" style="zoom: 67%;" />
```arduino
void setup(){
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(analogRead(A0));
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">如果模拟输入引脚没有连入电路由analogRead()返回的值将根据多项因素(例如其他模拟输入引脚,你的手靠近板子等)产生波动。</div>
</div>

View File

@@ -0,0 +1,84 @@
## 硬件中断
<img src="{default}/images/inout/interrupt-pin.png" alt="中断管脚" style="zoom:67%;" />
```arduino
void attachInterrupt_fun_RISING_2() {
}
void setup(){
pinMode(2, INPUT_PULLUP);
}
void loop(){
attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_RISING_2,RISING);
}
```
### 1.1 描述
> 当发生外部中断时,调用一个指定函数。当中断发生时,该函数会取代正在执行的程序。
>
> 大多数的Arduino板有两个外部中断0数字引脚2和1数字引脚3
>
> Arduino Mege有四个外部中断数字2引脚21320针4引脚195引脚18
>
> ESP8266 、ESP32系列有更多中断。
### 1.2 参数
- 管脚: 引脚编号如2,3不同的开发板中断引脚不一样。
- 模式:
> 改变:当引脚电平发生改变时,触发中断
>
> 上升:当引脚由低电平变为高电平时,触发中断
>
> 下降:当引脚由高电平变为低电平时,触发中断
### 1.3 范例
利用2号引脚中断控制13号引脚的LED亮灭。
<img src="{default}/images/inout/interrupt-pin-example.png" alt="中断管脚示例" style="zoom:67%;" />
```arduino
volatile boolean state;
void attachInterrupt_fun_RISING_2() {
state = !state;
digitalWrite(13,state);
}
void setup(){
state = false;
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2),attachInterrupt_fun_RISING_2,RISING);
}
void loop(){
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">当中断函数发生时delay()和millis()的数值将不会继续变化。当中断发生时,串口收到的数据可能会丢失。你应该声明一个变量来在未发生中断时储存变量。</div>
</div>
## 取消硬件中断
<img src="{default}/images/inout/detach-interrupt-pin.png" alt="取消中断" style="zoom:67%;" />
```arduino
detachInterrupt(digitalPinToInterrupt(2));
```
### 2.1 描述
> 关闭给定的中断。
### 2.2 参数
- 管脚: 引脚编号如23不同的开发板中断引脚不一样。

View File

@@ -0,0 +1,17 @@
## 管脚模式
<img src="{default}/images/inout/pinmode.png" alt="管脚模式" style="zoom:67%;" />
```arduino
pinMode(2, INPUT);
```
### 描述
> 设置指定管脚的模式。
### 参数
- 管脚: 引脚编号如23不同的开发板中断引脚不一样。
- 模式: 要将管脚设置成的模式,包括输入、输出、上拉输入。

View File

@@ -0,0 +1,42 @@
## 脉冲长度
<img src="{default}/images/inout/pin-pulseIn.png" alt="脉冲长度" style="zoom:67%;" />
```arduino
pulseIn(0, HIGH);
pulseIn(0, HIGH, 1000000);
```
### 描述
> 读取一个引脚的脉冲HIGH或LOW
>
> 例如如果value是HIGHpulseIn()会等待引脚变为HIGH开始计时再等待引脚变为LOW并停止计时。返回脉冲的长度单位微秒。如果在指定的时间内无脉冲函数返回。 此函数的计时功能由经验决定长时间的脉冲计时可能会出错。计时范围从10微秒至3分钟。1秒=1000毫秒=1000000微秒
### 参数
- 管脚:你要进行脉冲计时的引脚号int
- 状态:要读取的脉冲类型HIGH或LOWint
- 超时 (可选):指定脉冲计数的等待时间单位为微秒默认值是1秒unsigned long
### 返回
脉冲长度微秒如果等待超时返回0unsigned long
### 范例
读取6号引脚脉冲时长。
<img src="{default}/images/inout/pin-pulseIn-example.png" alt="脉冲长度示例" style="zoom:67%;" />
```arduino
void setup(){
pinMode(6, INPUT);
Serial.begin(9600);
}
void loop(){
Serial.println(pulseIn(6, HIGH));
}
```

View File

@@ -0,0 +1,19 @@
## ShiftOut
<img src="{default}/images/inout/shiftout.png" alt="image-20220528150921075" style="zoom:67%;" />
```arduino
shiftOut(2, 3, MSBFIRST, 0); // 高位先入
shiftOut(2, 3, LSBFIRST, 0); // 低位先入
```
### 描述
> 将一个数据的一个字节一位一位的移出。从最高有效位(最左边)或最低有效位(最右边)开始。依次向数据脚写入每一位,之后时钟脚被拉高或拉低,指示刚才的数据有效。
### 参数
- 数据管脚:输出每一位数据的引脚(int)
- 时钟管脚:时钟脚,当数据管脚有值时此引脚电平变化(int)
- 顺序:输出位的顺序,最高位优先或最低位优先
- 数值: 要移位输出的数据(byte)

View File

@@ -0,0 +1,57 @@
## 软件中断
<img src="{default}/images/inout/software-interrupt.png" alt="软件中断" style="zoom:67%;" />
```arduino
#include <PinChangeInt.h>
void attachPinInterrupt_fun_RISING_2() {
}
void setup(){
pinMode(2, INPUT);
PCintPort::attachInterrupt(2,attachPinInterrupt_fun_RISING_2,RISING);
}
void loop(){
}
```
### 1.1 描述
> 当发生外部中断时,调用一个指定函数。当中断发生时,该函数会取代正在执行的程序。
>
> 本模块为模拟中断,支持所有管脚使用。
### 1.2 参数
- 管脚: 引脚编号如2,3不同的开发板中断引脚不一样。
- 模式:
改变:当引脚电平发生改变时,触发中断上升:当引脚由低电平变为高电平时,触发中断下降:当引脚由高电平变为低电平时,触发中断
### 1.3 范例
利用中断控制13号引脚的LED亮灭。
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">当中断函数发生时delay()和millis()的数值将不会继续变化。当中断发生时,串口收到的数据可能会丢失。你应该声明一个变量来在未发生中断时储存变量。</div>
</div>
## 取消软件中断
<img src="{default}/images/inout/software-detachInterrupt.png" alt="取消软件中断" style="zoom:67%;" />
```arduino
PCintPort::detachInterrupt(2);
```
### 2.1 描述
> 关闭给定的中断。
### 2.2 参数
- 管脚: 引脚编号如23不同的开发板中断引脚不一样。

View File

@@ -0,0 +1,56 @@
## 多功能按键
<img src="{default}/images/inout/OneButton.png" alt="多功能按键" style="zoom:67%;" />
```arduino
#include <OneButton.h>
OneButton button2(2, false);
void attachClick2() {
}
void setup(){
button2.attachClick(attachClick2);
}
void loop(){
button2.tick();
}
```
### 描述
> 设置特定管脚连接的按钮为多功能按钮,并确定不同模式下执行不同的程序。
### 参数
- 多功能按键: 引脚编号如1,5,10A0A3
- 模式: 单击 双击 长按开始 长按中 长按结束
### 范例
将2号端口连接的按钮设置为多功能按钮单击时串口提示“one Click”。
<img src="{default}/images/inout/OneButton-example.png" alt="多功能按键示例" style="zoom:67%;" />
```arduino
#include <OneButton.h>
OneButton button2(2, false);
void attachClick2() {
Serial.println("one click");
}
void setup(){
button2.attachClick(attachClick2);
Serial.begin(9600);
}
void loop(){
button2.tick();
}
```

View File

@@ -0,0 +1,5 @@
## 控制
控制类别中包括了时间延迟、条件执行、循环执行、获取运行时间、初始化、Switch执行等 控制模块中主要执行的内容是对程序结构进行的相应控制。
<img src="{default}/images/control/control.png" alt="控制分类" style="zoom:50%;" />

View File

@@ -0,0 +1,61 @@
## 初始化
<img src="{default}/images/control/setup.png" alt="初始化" style="zoom:67%;" />
```arduino
void setup(){
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
}
void loop(){
}
```
### 描述
> 在Arduino中程序运行时将首先调用 setup()函数。 用于初始化变量、设置针脚的输出输入类型、配置串口、引入类库文件等等。
### 范例1
初始化时声明变量。
<img src="{default}/images/control/setup-example1.png" alt="初始化示例1" style="zoom:67%;" />
```arduino
volatile int item;
void setup(){
item = 0;
}
void loop(){
}
```
### 范例2
在初始化时定义中断函数。
<img src="{default}/images/control/setup-example2.png" alt="初始化示例2" style="zoom:67%;" />
```arduino
void attachInterrupt_fun_RISING_2() {
digitalWrite(13, LOW);
}
void setup(){
pinMode(13, OUTPUT);
pinMode(2, INPUT_PULLUP);
digitalWrite(13, HIGH);
attachInterrupt(digitalPinToInterrupt(2), attachInterrupt_fun_RISING_2,RISING);
}
void loop(){
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">每次 Arduino 上电或重启后setup 函数只运行一次。</div>
</div>

View File

@@ -0,0 +1,36 @@
## while 循环
<img src="{default}/images/control/while.png" alt="while" style="zoom:67%;" />
```arduino
while (true) {
}
```
### 描述
> while循环会无限的循环直到括号内的判断语句变为假。 必须要有能改变判断语句的东西要不然while循环将永远不会结束。你可以使用一个传感器的值或者一个变量来控制什么时候停止该循环。
### 参数
- 满足条件:为真或为假的一个条件。
### 范例
当温度高于30度时亮灯否则灭灯。
<img src="{default}/images/control/while-example.png" alt="while示例" style="zoom:67%;" />
```arduino
void setup(){
pinMode(13, OUTPUT);
}
void loop(){
while (analogRead(A0)*0.488 > 30) {
digitalWrite(13, HIGH);
}
digitalWrite(13, LOW);
}
```

View File

@@ -0,0 +1,43 @@
## 延时
<img src="{default}/images/control/delay.png" alt="延时" style="zoom:67%;" />
```arduino
delay(1000);
```
### 描述
> 使程序暂定设定的时间单位毫秒一秒等于1000毫秒
### 参数
- 毫秒:暂停的毫秒数。
### 范例
13号引脚灯亮1秒灭1秒往复循环。
<img src="{default}/images/control/delay-example.png" alt="延时示例" style="zoom:67%;" />
```arduino
void setup(){
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">虽然创建一个使用delay()的闪烁LED很简单并且许多例子将很短的delay用于消除开关抖动。
<br /><br />
但delay()确实拥有很多显著的缺点。在delay函数使用的过程中读取传感器值、计算、引脚操作均无法执行因此它所带来的后果就是使其他大多数活动暂停。大多数熟练的程序员通常避免超过10毫秒的delay(),除非arduino程序非常简单。
<br /><br />
利用定时器就可以解决这个问题可以避免由于delay()带来的CPU暂停也能很好地实现每隔一定时间执行动作。</div>
</div>

View File

@@ -0,0 +1,86 @@
## if 选择
<img src="{default}/images/control/if.png" alt="if" style="zoom:67%;" />
```arduino
if (false) {
}
```
### 描述
> if 语句与比较运算符一起用于检测某个条件是否达成,如某个传感器的值是否等于某个值。
### 参数
- 条件:比较表达式
### 用法
增加条件:如果需要增加条件,可以点开齿轮,然后将左侧的“否则如果”或者“否则”模块拖到右侧的“如果”之中。
<img src="{default}/images/control/if-else.png" alt="if-else" style="zoom:67%;" />
### 范例1
当连接在2号引脚的按键按下时点亮13号引脚的灯。
<img src="{default}/images/control/if-else-example1.png" alt="if-else示例1" style="zoom:67%;" />
```arduino
void setup(){
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if (digitalRead(2) == 1) {
digitalWrite(13, HIGH);
}
}
```
如果判断的条件大于等于1时可以省略等于判断因为只要 该表达式的结果不为0则为真。
所以,上面的写法与下面的写法等效。
<img src="{default}/images/control/if-else-example2.png" alt="if-ese示例2" style="zoom:67%;" />
```arduino
void setup(){
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if (digitalRead(2)) {
digitalWrite(13, HIGH);
}
}
```
### 范例2
当连接在2号引脚的按键按下时点亮13号引脚的灯;当按键松开时,灯灭。
<img src="{default}/images/control/if-else-example3.png" alt="if-else示例3" style="zoom:67%;" />
```arduino
void setup(){
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop(){
if (digitalRead(2)) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
```
<div class="layui-card" style="box-shadow: 1px 1px 4px 1px rgb(0 0 0 / 20%);">
<div class="layui-card-header icon-attention-circled" style="background: #f0b37e;color:#fff;font-size:16px;">注意</div>
<div class="layui-card-body" style="background: #ffedcc;">另外一种进行多种条件分支判断的语句是switch case语句。</div>
</div>

Some files were not shown because too many files have changed in this diff Show More