This library provides a simple interface for implementing a shutdown timer in Arduino projects. It allows you to set a countdown timer to power down or shut off a device after a specified period. This library relies on FreeRTOS and is tested on the Seeed XIAO BLE board.
- Set a shutdown timer with a specified duration.
- Simple API for easy integration.
- Debugging support to print messages to the serial monitor.
- Download the latest release from the releases page.
- Unzip the downloaded file.
- Copy the
PDL_Shutdown_Timer
folder to your Arduino libraries directory (usually located inDocuments/Arduino/libraries
). - Install the FreeRTOS library from the Library Manager in the Arduino IDE.
To use the PDL Shutdown Timer library, include it at the top of your sketch:
#include <PDL_Shutdown_Timer.h>
#include <Adafruit_TinyUSB.h>
Create an instance of the PDL_Shutdown_Timer
class and initialize it with the desired shutdown duration (in seconds), the pin to power off the system, and the GPIO state that keeps power enabled:
#define SHUTDOWN_PIN 7
#define SHUTDOWN_DURATION_SEC 10 // 10 seconds
#define POWER_ON_STATE HIGH // GPIO state that keeps power enabled
PDL_Shutdown_Timer shutdownTimer(SHUTDOWN_PIN, SHUTDOWN_DURATION_SEC, POWER_ON_STATE);
Start the shutdown timer in your setup
function:
void setup() {
// Initialize serial communication for debugging purposes
Serial.begin(9600);
// Set the debug level
shutdownTimer.setDebug(PDL_Shutdown_Timer::DEBUG_ON);
// Start the shutdown timer
shutdownTimer.start();
}
Here's a complete example demonstrating how to use the PDL Shutdown Timer library:
#include <PDL_Shutdown_Timer.h>
#include <Adafruit_TinyUSB.h>
// Define the pin to control power and the duration for the shutdown timer
#define SHUTDOWN_PIN 7
#define SHUTDOWN_DURATION_SEC 10 // 10 seconds
#define POWER_ON_STATE HIGH // GPIO state that keeps power enabled
// Create an instance of PDL_Shutdown_Timer with the specified pin, duration, and power on state
PDL_Shutdown_Timer shutdownTimer(SHUTDOWN_PIN, SHUTDOWN_DURATION_SEC, POWER_ON_STATE);
void setup()
{
// Initialize serial communication for debugging purposes
Serial.begin(9600);
// Set the debug level
shutdownTimer.setDebug(PDL_Shutdown_Timer::DEBUG_ON);
// Start the shutdown timer
shutdownTimer.start();
}
unsigned long time_passed = 0;
void loop()
{
delay(1000); // Delay for 1 second
time_passed++;
int timeLeft = SHUTDOWN_DURATION_SEC - time_passed;
Serial.printf("Time left: %d seconds\n", timeLeft);
static bool reset_demo_flag = false;
if (timeLeft == 5 && !reset_demo_flag) {
Serial.println("Resetting the shutdown timer");
shutdownTimer.reset();
time_passed = 0;
reset_demo_flag = true;
}
}
PDL_Shutdown_Timer(uint8_t pin, float shutdown_time_sec = 60.0, bool powerOnState = HIGH)
: Constructor to set the shutdown duration in seconds, the pin to power off the system, and the GPIO state that keeps power enabled.void setDebug(DebugLevel debug)
: Set the debug level for the timer module.int start()
: Start the shutdown timer.int stop()
: Stop the shutdown timer.void reset()
: Reset the shutdown timer.void setShutdownTimeSec(float shutdown_time_sec)
: Set the shutdown duration in seconds.void systemShutdown()
: Shut down the system.void systemSleep()
: Put the system to sleep.
This library is licensed under the MIT License. See the LICENSE
file for more details.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
For questions or suggestions, please open an issue on the GitHub repository or contact Xuteng Lin at xutengl@outlook.com.
Sponsored
ChatGPT for Stock Market Investors