|
本例程应用ESPDUINO开发板结合4路继电器模块,用开发板上面的4个IO口控制4路继电器的通断从而间接控制4颗LED灯的亮灭,实现流水灯的简单功能演示。
- #define Relay1 0 //继电器1
- #define Relay2 4 //继电器2
- #define Relay3 5 //继电器3
- #define Relay4 12 //继电器4
- void setup()
- {
- pinMode(Relay1, OUTPUT);
- pinMode(Relay2, OUTPUT);
- pinMode(Relay3, OUTPUT);
- pinMode(Relay4, OUTPUT);
- digitalWrite(Relay1, HIGH);
- digitalWrite(Relay2, HIGH);
- digitalWrite(Relay3, HIGH);
- digitalWrite(Relay4, HIGH);
- }
- void loop()
- {
- digitalWrite(Relay1, LOW);
- digitalWrite(Relay2, HIGH);
- digitalWrite(Relay3, HIGH);
- digitalWrite(Relay4, HIGH);
- delay(800);
- digitalWrite(Relay1, HIGH);
- digitalWrite(Relay2, LOW);
- digitalWrite(Relay3, HIGH);
- digitalWrite(Relay4, HIGH);
- delay(800);
- digitalWrite(Relay1, HIGH);
- digitalWrite(Relay2, HIGH);
- digitalWrite(Relay3, LOW);
- digitalWrite(Relay4, HIGH);
- delay(800);
- digitalWrite(Relay1, HIGH);
- digitalWrite(Relay2, HIGH);
- digitalWrite(Relay3, HIGH);
- digitalWrite(Relay4, LOW);
- delay(800);
- }
复制代码 #温馨提示:由于继电器本身的延迟效应,程序中用到的延时函数时间不能太短,否则将导致运行出错。
硬件连接及实验效果图:
|
|