|
本帖最后由 topdog 于 2022-1-16 15:26 编辑
以WEMOS D1实验,D3为开关,D4为水泵,esp8266按下按钮测得低电平时水泵开启。系统开始计时5秒(1秒为1000毫秒,5秒为5000毫秒,为防止溢出故采用unsigned int),关闭水泵。
程序如下:
[pre]const int button = D3;
const int WaterPump = D4;
volatile unsigned int starttime;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(WaterPump, OUTPUT);
digitalWrite(WaterPump,LOW);
}
void loop() {
bool state = digitalRead(button);
if (state == LOW) {
starttime = millis();
digitalWrite(WaterPump,HIGH);
}
if (millis() - starttime == 5000) {
digitalWrite(WaterPump,LOW);
}
}[/pre] |
|