初始化提交
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 841 B |
|
After Width: | Height: | Size: 980 B |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 8.9 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 7.2 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 104 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 146 KiB |
|
After Width: | Height: | Size: 7.8 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 103 KiB |
|
After Width: | Height: | Size: 91 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 156 KiB |
|
After Width: | Height: | Size: 4.7 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 37 KiB |
|
After Width: | Height: | Size: 45 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
|
After Width: | Height: | Size: 103 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 6.1 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 5.5 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 4.1 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 27 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 8.2 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 7.0 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 7.3 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 6.5 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 137 KiB |
|
After Width: | Height: | Size: 39 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 8.1 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 7.0 KiB |
|
After Width: | Height: | Size: 10 KiB |
@@ -0,0 +1,13 @@
|
||||
## 输入/输出
|
||||
|
||||
输入/输出所包含的指令主要分为四部分:控制管脚的输入输出(按信号类型可分为数字信号和模拟信号)、中断、脉冲长度及ShiftOut。
|
||||
|
||||
- 输入输出:数字输入、数字输出、模拟输入、模拟输出
|
||||
|
||||
- 中断控制:定义中断,取消中断
|
||||
|
||||
- 脉冲长度
|
||||
|
||||
- 移位输出
|
||||
|
||||
<img src="{default}/images/inout/input-output.png" alt="输入/输出分类" style="zoom:50%;" />
|
||||
@@ -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。
|
||||
|
||||
### 参数
|
||||
|
||||
- 管脚: 引脚编号(如1,5,10,A0,A3)
|
||||
- 值: 高 或 低
|
||||
|
||||
### 范例
|
||||
|
||||
将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>
|
||||
@@ -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>
|
||||
@@ -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),只有引脚3,5,6,9,10和11可以实现该功能。 在Arduino Mega上,引脚2到13可以实现该功能。</div>
|
||||
</div>
|
||||
@@ -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>
|
||||
@@ -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(引脚21),3(20针),4(引脚19),5(引脚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 参数
|
||||
|
||||
- 管脚: 引脚编号(如2,3)不同的开发板中断引脚不一样。
|
||||
@@ -0,0 +1,17 @@
|
||||
## 管脚模式
|
||||
|
||||
<img src="{default}/images/inout/pinmode.png" alt="管脚模式" style="zoom:67%;" />
|
||||
|
||||
```arduino
|
||||
pinMode(2, INPUT);
|
||||
```
|
||||
|
||||
### 描述
|
||||
|
||||
> 设置指定管脚的模式。
|
||||
|
||||
### 参数
|
||||
|
||||
- 管脚: 引脚编号(如2,3)不同的开发板中断引脚不一样。
|
||||
- 模式: 要将管脚设置成的模式,包括输入、输出、上拉输入。
|
||||
|
||||
@@ -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是HIGH,pulseIn()会等待引脚变为HIGH,开始计时,再等待引脚变为LOW并停止计时。返回脉冲的长度,单位微秒。如果在指定的时间内无脉冲,函数返回。 此函数的计时功能由经验决定,长时间的脉冲计时可能会出错。计时范围从10微秒至3分钟。(1秒=1000毫秒=1000000微秒)
|
||||
|
||||
### 参数
|
||||
|
||||
- 管脚:你要进行脉冲计时的引脚号(int)。
|
||||
- 状态:要读取的脉冲类型,HIGH或LOW(int)。
|
||||
- 超时 (可选):指定脉冲计数的等待时间,单位为微秒,默认值是1秒(unsigned long)。
|
||||
|
||||
### 返回
|
||||
|
||||
脉冲长度(微秒),如果等待超时返回0(unsigned 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));
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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)
|
||||
@@ -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 参数
|
||||
|
||||
- 管脚: 引脚编号(如2,3)不同的开发板中断引脚不一样。
|
||||
@@ -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,10,A0,A3)
|
||||
- 模式: 单击 双击 长按开始 长按中 长按结束
|
||||
|
||||
### 范例
|
||||
|
||||
将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();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
## 控制
|
||||
|
||||
控制类别中包括了时间延迟、条件执行、循环执行、获取运行时间、初始化、Switch执行等 控制模块中主要执行的内容是对程序结构进行的相应控制。
|
||||
|
||||
<img src="{default}/images/control/control.png" alt="控制分类" style="zoom:50%;" />
|
||||
@@ -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>
|
||||
@@ -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);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||