Raspberry Pi Pico 2 条件语句
在 Raspberry Pi Pico 2 编程中,条件语句是控制程序执行流程的重要工具。它们允许你根据某些条件的真假来决定执行哪些代码。条件语句的核心是 if、else if 和 else,它们可以帮助你编写更加灵活和智能的程序。
本文将分别使用 Arduino 风格(基于 Arduino-Pico 核心)和 C/C++ 风格(基于官方 Pico SDK)来介绍条件语句的用法,并提供实际案例。
1. 什么是条件语句?
条件语句是一种编程结构,它允许程序根据某些条件的真假来选择执行不同的代码块。在 Pico 2 编程中,最常见的条件语句是 if 语句,它用于检查一个条件是否为真。如果条件为真,则执行 if 语句后的代码块;如果条件为假,则跳过该代码块或执行 else 中的代码。
1.1 基本语法(Arduino 风格)
condition:这是一个布尔表达式,它的值为 true 或 false。
{}:花括号内的代码块将在条件为真时执行。
1.2 基本语法(C/C++ 风格)
C/C++ 风格的条件语句语法与 Arduino 风格完全一致,因为 Arduino 的语法本身就是基于 C/C++ 的。
2. if 语句
if 语句是最简单的条件语句。它用于检查一个条件是否为真,如果为真,则执行相应的代码块。
2.1 Arduino 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| void setup() { pinMode(25, OUTPUT); Serial.begin(115200); }
void loop() { int sensorValue = analogRead(26);
if (sensorValue > 2000) { digitalWrite(25, HIGH); Serial.println("LED ON"); } else { digitalWrite(25, LOW); Serial.println("LED OFF"); } delay(100); }
|
2.2 C/C++ 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include "pico/stdlib.h" #include "hardware/adc.h"
int main() { stdio_init_all(); gpio_init(25); gpio_set_dir(25, GPIO_OUT); adc_init(); adc_gpio_init(26); adc_select_input(0);
while (true) { uint16_t sensorValue = adc_read();
if (sensorValue > 2000) { gpio_put(25, 1); printf("LED ON\n"); } else { gpio_put(25, 0); printf("LED OFF\n"); } sleep_ms(100); } return 0; }
|
3. else 语句
else 语句用于在 if 条件为假时执行另一段代码。它总是与 if 语句一起使用。
3.1 Arduino 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13
| void setup() { pinMode(25, OUTPUT); }
void loop() { int sensorValue = analogRead(26);
if (sensorValue > 2000) { digitalWrite(25, HIGH); } else { digitalWrite(25, LOW); } }
|
3.2 C/C++ 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include "pico/stdlib.h" #include "hardware/adc.h"
int main() { stdio_init_all(); gpio_init(25); gpio_set_dir(25, GPIO_OUT); adc_init(); adc_gpio_init(26); adc_select_input(0);
while (true) { uint16_t sensorValue = adc_read();
if (sensorValue > 2000) { gpio_put(25, 1); } else { gpio_put(25, 0); } sleep_ms(100); } }
|
4. else if 语句
else if 语句用于在多个条件之间进行选择。它允许你检查多个条件,并在第一个为真的条件处执行相应的代码块。
4.1 Arduino 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| void setup() { pinMode(25, OUTPUT); Serial.begin(115200); }
void loop() { int sensorValue = analogRead(26);
if (sensorValue > 3000) { digitalWrite(25, HIGH); Serial.println("Very bright: LED ON"); } else if (sensorValue > 1500) { digitalWrite(25, HIGH); Serial.println("Moderate: LED ON"); } else { digitalWrite(25, LOW); Serial.println("Dark: LED OFF"); } delay(100); }
|
4.2 C/C++ 风格示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include "pico/stdlib.h" #include "hardware/adc.h"
int main() { stdio_init_all(); gpio_init(25); gpio_set_dir(25, GPIO_OUT); adc_init(); adc_gpio_init(26); adc_select_input(0);
while (true) { uint16_t sensorValue = adc_read();
if (sensorValue > 3000) { gpio_put(25, 1); printf("Very bright: LED ON\n"); } else if (sensorValue > 1500) { gpio_put(25, 1); printf("Moderate: LED ON\n"); } else { gpio_put(25, 0); printf("Dark: LED OFF\n"); } sleep_ms(100); } return 0; }
|
注意:Pico 2 的 ADC 是 12 位分辨率,读取值范围为 0–4095(对应 0–3.3V)。使用 analogRead()(Arduino)或 adc_read()(SDK)时,返回值均在 0–4095 之间。
5. 实际应用案例
5.1 案例1:温度控制
假设你有一个模拟温度传感器(如 LM35)连接到 GP26,输出 10mV/℃。当温度超过 30℃ 时,打开风扇(连接到 GP0);否则关闭风扇。
Arduino 风格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const int TEMP_SENSOR = 26; const int FAN_PIN = 0;
void setup() { pinMode(FAN_PIN, OUTPUT); Serial.begin(115200); }
void loop() { int raw = analogRead(TEMP_SENSOR); float voltage = raw * 3.3 / 4095.0; float temperature = voltage * 100.0;
if (temperature > 30.0) { digitalWrite(FAN_PIN, HIGH); Serial.println("Fan ON"); } else { digitalWrite(FAN_PIN, LOW); Serial.println("Fan OFF"); } delay(1000); }
|
C/C++ 风格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include "pico/stdlib.h" #include "hardware/adc.h"
#define TEMP_SENSOR_PIN 26 #define FAN_PIN 0
int main() { stdio_init_all(); gpio_init(FAN_PIN); gpio_set_dir(FAN_PIN, GPIO_OUT); adc_init(); adc_gpio_init(TEMP_SENSOR_PIN); adc_select_input(0);
while (true) { uint16_t raw = adc_read(); float voltage = raw * 3.3f / 4095.0f; float temperature = voltage * 100.0f;
if (temperature > 30.0f) { gpio_put(FAN_PIN, 1); printf("Fan ON\n"); } else { gpio_put(FAN_PIN, 0); printf("Fan OFF\n"); } sleep_ms(1000); } return 0; }
|
5.2 案例2:光线传感器
假设你有一个光线传感器(如光敏电阻分压电路)连接到 GP27(ADC1),当光线强度低于阈值时,打开 LED(连接在 GP25 板载 LED)。
Arduino 风格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const int LIGHT_SENSOR = 27; const int LED_PIN = 25;
void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(115200); }
void loop() { int lightLevel = analogRead(LIGHT_SENSOR); Serial.print("Light level: "); Serial.println(lightLevel);
if (lightLevel < 500) { digitalWrite(LED_PIN, HIGH); Serial.println("Too dark, LED ON"); } else { digitalWrite(LED_PIN, LOW); Serial.println("Bright enough, LED OFF"); } delay(200); }
|
C/C++ 风格:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include "pico/stdlib.h" #include "hardware/adc.h"
#define LIGHT_SENSOR_PIN 27 #define LED_PIN 25
int main() { stdio_init_all(); gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); adc_init(); adc_gpio_init(LIGHT_SENSOR_PIN); adc_select_input(1);
while (true) { uint16_t lightLevel = adc_read(); printf("Light level: %u\n", lightLevel);
if (lightLevel < 500) { gpio_put(LED_PIN, 1); printf("Too dark, LED ON\n"); } else { gpio_put(LED_PIN, 0); printf("Bright enough, LED OFF\n"); } sleep_ms(200); } return 0; }
|
提示:在实际传感器应用中,请根据传感器的数据手册校准阈值和转换公式。
6. 总结
条件语句是 Raspberry Pi Pico 2 编程中非常重要的工具,它们允许你根据不同的条件执行不同的代码。通过使用 if、else if 和 else 语句,你可以编写出更加灵活和智能的程序。
| 语句类型 |
作用 |
if |
条件为真时执行代码块 |
else |
条件为假时执行代码块 |
else if |
检查多个条件,执行第一个为真的代码块 |
编写建议:
- 保持条件逻辑清晰,避免复杂的嵌套。
- 对于多条件判断,优先使用
else if 而不是多个独立的 if,以提高效率。
- 注意 Pico 2 的 GPIO 是 3.3V 逻辑电平,传感器输出应适配此电压范围。
7. 附加资源与练习
- 练习 1:编写一个程序,根据按钮(连接到 GP0)的按下状态来控制 LED(GP25)的开关。按钮按下时读取高电平(3.3V),松开时为低电平(0V)。
- 练习 2:使用多个
else if 语句创建一个多级温度控制系统:温度 < 20℃ 时关闭风扇,20–30℃ 时低速运转,> 30℃ 时全速运转。
- 练习 3:结合 PWM 和条件语句,实现一个自动调光台灯:光线越暗,LED 亮度越高(使用
analogWrite 或 PWM 输出)。
通过不断练习,你将更加熟练地掌握 Pico 2 中的条件语句,并能够将其应用到各种实际项目中。
💡 提示:在编写条件语句时,注意区分赋值运算符 = 和比较运算符 ==,这是初学者常见的错误。