diff --git a/DeepSleepScheduler.h b/DeepSleepScheduler.h index 1a1fb03..841bafe 100644 --- a/DeepSleepScheduler.h +++ b/DeepSleepScheduler.h @@ -21,6 +21,7 @@ - #define LIBCALL_DEEP_SLEEP_SCHEDULER: This h file can only be included once within a project as it also contains the implementation. To use it in multiple files, define LIBCALL_DEEP_SLEEP_SCHEDULER before all include statements except one. All following options are to be set before the include where no LIBCALL_DEEP_SLEEP_SCHEDULER is defined. + - #define SLEEP_MODE: Specifies the sleep mode entered when doing deep sleep. Default is PWR_DOWN. - #define DEEP_SLEEP_DELAY: Prevent the CPU from entering SLEEP_MODE_PWR_DOWN for the specified amount of milli seconds after finishing the previous task. - #define SUPERVISION_CALLBACK: Allows to specify a callback Runnable to be called when a task runs too long. When the callback returns, the CPU is restarted after 15 ms by the watchdog. The callback method is called directly @@ -44,6 +45,10 @@ #include // values changeable by the user +#ifndef SLEEP_MODE +#define SLEEP_MODE PWR_DOWN +#endif + #ifndef SUPERVISION_CALLBACK_TIMEOUT #define SUPERVISION_CALLBACK_TIMEOUT WDTO_1S #endif @@ -675,7 +680,7 @@ inline void Scheduler::sleepIfRequired() { byte adcsraSave = 0; if (sleepMode == SLEEP) { noInterrupts(); - set_sleep_mode(SLEEP_MODE_PWR_DOWN); + set_sleep_mode(SLEEP_MODE); adcsraSave = ADCSRA; ADCSRA = 0; // disable ADC // turn off brown-out in software diff --git a/README.md b/README.md index c1c2cc1..5af38cf 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ You can also see them in the [Arduino Software (IDE)](https://www.arduino.cc/en/ - [**Supervision**](https://github.com/PRosenb/DeepSleepScheduler/blob/master/examples/Supervision/Supervision.ino): Shows how to activate the task supervision in order to restart the CPU when a task takes too much time - [**SupervisionWithCallback**](https://github.com/PRosenb/DeepSleepScheduler/blob/master/examples/SupervisionWithCallback/SupervisionWithCallback.ino): Shows how to activate the task supervision and get a callback when a task takes too much time - [**SerialWithDeepSleepDelay**](https://github.com/PRosenb/DeepSleepScheduler/blob/master/examples/SerialWithDeepSleepDelay/SerialWithDeepSleepDelay.ino): Shows how to use `DEEP_SLEEP_DELAY` to allow serial write to finish before entering deep sleep +- [**PwmSleep**](https://github.com/PRosenb/DeepSleepScheduler/blob/master/examples/PwmSleep/PwmSleep.ino): Shows how to use analogWrite() and still use low power mode. - [**AdjustSleepTimeCorrections**](https://github.com/PRosenb/DeepSleepScheduler/blob/master/examples/AdjustSleepTimeCorrections/AdjustSleepTimeCorrections.ino): Shows how to adjust the sleep time corrections to your specific CPU ## Reference ## @@ -264,6 +265,7 @@ enum TaskTimeout { - `#define LIBCALL_DEEP_SLEEP_SCHEDULER`: The header file contains definition and implementation. For that reason, it can be included once only in a project. To use it in multiple files, define `LIBCALL_DEEP_SLEEP_SCHEDULER` before all include statements except one. All following options are to be set before the include where **no** `LIBCALL_DEEP_SLEEP_SCHEDULER` is defined: +- `#define SLEEP_MODE`: Specifies the sleep mode entered when doing deep sleep. Default is `PWR_DOWN`. - `#define DEEP_SLEEP_DELAY`: Prevent the CPU from entering SLEEP_MODE_PWR_DOWN for the specified amount of milliseconds after finishing the previous task. - `#define SUPERVISION_CALLBACK`: Allows to specify a callback `Runnable` to be called when a task runs too long. When the callback returns, the CPU is restarted after 15 ms by the watchdog. The callback method is called directly diff --git a/examples/PwmSleep/PwmSleep.ino b/examples/PwmSleep/PwmSleep.ino new file mode 100644 index 0000000..6823abc --- /dev/null +++ b/examples/PwmSleep/PwmSleep.ino @@ -0,0 +1,32 @@ +// do not use SLEEP_MODE_PWR_DOWN to prevent that +// the asynchronous clock is stopped +#define SLEEP_MODE SLEEP_MODE_PWR_SAVE +// show then the CPU is active on PIN 13 +#define AWAKE_INDICATION_PIN 13 +#include + +// the PWM signal can only be used in sleep mode on +// Arduino UNO PINs 3 and 11 because only these two +// use an asynchronous clock that is not stopped +// with SLEEP_MODE_PWR_SAVE +#define PWN_PIN 3 + +void highValue() { + analogWrite(PWN_PIN, 200); + scheduler.scheduleDelayed(lowValue, 5000); +} + +void lowValue() { + analogWrite(PWN_PIN, 100); + scheduler.scheduleDelayed(highValue, 5000); +} + +void setup() { + pinMode(PWN_PIN, OUTPUT); + scheduler.schedule(lowValue); +} + +void loop() { + scheduler.execute(); +} +