写一个多路继电器驱动
时间: 2023-10-09 12:11:10 浏览: 62
以下是一个简单的多路继电器驱动程序,基于 Arduino 平台。
```C++
const int relayPin1 = 2; // 第1路继电器控制引脚
const int relayPin2 = 3; // 第2路继电器控制引脚
const int relayPin3 = 4; // 第3路继电器控制引脚
void setup() {
pinMode(relayPin1, OUTPUT); // 设置引脚为输出模式
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
}
void loop() {
// 第1路继电器控制
digitalWrite(relayPin1, HIGH); // 打开继电器
delay(1000); // 等待一秒钟
digitalWrite(relayPin1, LOW); // 关闭继电器
delay(1000);
// 第2路继电器控制
digitalWrite(relayPin2, HIGH);
delay(1000);
digitalWrite(relayPin2, LOW);
delay(1000);
// 第3路继电器控制
digitalWrite(relayPin3, HIGH);
delay(1000);
digitalWrite(relayPin3, LOW);
delay(1000);
}
```
这个程序可以控制多达三个继电器,每个继电器都在循环中打开并关闭。如果需要控制更多的继电器,可以添加更多的 `relayPin` 变量,并在 `setup()` 函数中初始化它们。在循环中,只需复制并修改相应的代码块即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![VI](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)