不用 delay() 让电机一开一关
Arduino C++让电机开 1 s、关 1 s,一直循环,用 millis() 代替 delay(),这样程序在间隙里仍然可以读取按钮或传感器。
检查
- 尚未运行引脚 9 以 2 s 的周期翻转
- 尚未运行1.5 s 时电机是开着的
- 尚未运行2.5 s 时电机是关着的
- 尚未运行使用 millis(),不用 delay()
提示
- 记住你上次切换的时间:unsigned long last = 0;
- 在 loop() 里,当 millis() - last >= 1000 时,给 last 加上 1000,并翻转引脚。
- digitalWrite(MOTOR, on ? HIGH : LOW) 把一个 bool 标志写到引脚上。