# Arduino 中按钮闪烁会中断步进电机运行

> 原文链接：https://robotics.hellobike.com/pitfalls/2018-12-11-robotics-se-2028
> 频道：pitfalls | 更新日期：2018-12-11 | 标签：auto, stackexchange, arduino, stepper-motor, c
> 核心摘要：问题由 delay(5) 阻塞循环导致，改用 millis() 按时间非阻塞更新按钮、LED和步进电机。

# Arduino 中按钮闪烁会中断步进电机运行

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[Is it possible to run multiple loops at the same time? (Arduino)](https://robotics.stackexchange.com/questions/2028/is-it-possible-to-run-multiple-loops-at-the-same-time-arduino)（Robotics Stack Exchange #2028）

**状态**：已解决

## 问题

Arduino 程序中步进电机往返运行，同时读取按钮控制扫描器信号；按下按钮时会导致电机停止或被覆盖，无法并行执行。

## 环境

嵌入式控制器 / 串口通信

## 解决办法

1. 不要在按钮处理里使用 `delay(5)`，因为它会暂停代码执行。
2. 在每次 `loop()` 中调用 `millis()` 获取当前时间。
3. 用基于时间的函数计算 `pulseopto1` 的状态，例如按下按钮后在 `interval` 时间内为 `HIGH`，之后为 `LOW`。
4. 每次循环都读取按钮并直接更新 `startScan`，同时调用 `stepper1.moveTo(...)` 和 `stepper1.run()`，避免条件延时阻塞。

原问题作者：hollander；采纳答案作者：Ian。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #2028：Is it possible to run multiple loops at the same time? (Arduino)](https://robotics.stackexchange.com/questions/2028/is-it-possible-to-run-multiple-loops-at-the-same-time-arduino)