Arduino 復位

Ammar Ali 2023年10月12日 Arduino Arduino Reset
  1. 使用復位按鈕復位 Arduino
  2. 使用 Softwarereset 庫復位 Arduino
  3. 使用 Adafruit SleepyDog 庫復位 Arduino
Arduino 復位

本教程將討論三種復位 Arduino 的方法。第一種方法是 Arduino 上的復位按鈕。第二種方法是 Softwarereset 庫,第三種是 Adafruit 的 SleepyDog 庫。

使用復位按鈕復位 Arduino

如果你使用的是 Linux,則存在一個錯誤,該錯誤會阻止 Arduino IDE 與 Arduino 開發板對話。結果,你無法在 Arduino 中上傳程式碼,它將給出一個錯誤。在這種情況下,你可以使用此方法復位 Arduino。

首先,請確保沒有集線器將 Arduino 直接連線到計算機。使用集線器有時會給你帶來錯誤。現在關閉 Arduino 的電源,按住復位按鈕,同時再次開啟它的電源。這將復位你的 Arduino,並且你可以輕鬆上傳其他程式碼而不會出現任何錯誤。

使用 Softwarereset 庫復位 Arduino

如果你想使用 sketch 復位 Arduino,則可以使用 Softwarereset輕鬆復位。該庫與 AVR 架構相容,因此你可以將其與 Arduino Uno、Mega、Yun、Nano 和 Leonardo 開發板一起使用。要使用此庫,你需要使用 Arduino IDE 中提供的庫管理器進行安裝。

該庫有兩種復位 Arduino 的方法。一種是 standard 方法,該方法將使用看門狗計時器復位 Arduino。另一種方法是 simple 方法,該方法將僅重新啟動程式。

void loop() {
  // All of your code
  softwareReset::standard();  // Reset using the standard method
  softwareReset::simple();    // Restart the program
}

請注意,將不會執行在復位程式碼下方寫入的任何程式碼行。因此,請確保在完成程式碼後使用 reset。有關更多資訊,請閱讀庫文件

使用 Adafruit SleepyDog 庫復位 Arduino

上面的庫僅適用於五個 Arduino 開發板。如果你的 Arduino 不是其中之一,那麼你可以使用此庫,因為它支援幾乎所有的 Arduino 開發板。使用此連結檢查 Arduino 是否與此庫相容。

#include <Adafruit_SleepyDog.h>

void setup() {
  // Make sure to reset the watchdog before the countdown expires or
  // the Arduino will reset!
  int countdownMS = Watchdog.enable(4000);
}
void loop() {
  // All of your code
}

在上面的程式碼中,Arduino 將在 4 秒鐘內復位。你可以使用復位方法復位看門狗。有關更多資訊,請閱讀庫文件

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook