Skip to content

Commit

Permalink
add define SLEEP_MODE to allow the user to specify the used sleep mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRosenb committed Dec 17, 2016
1 parent afbbf11 commit 171ce86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion DeepSleepScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +45,10 @@
#include <util/atomic.h>

// 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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions examples/PwmSleep/PwmSleep.ino
Original file line number Diff line number Diff line change
@@ -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 <DeepSleepScheduler.h>

// 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();
}

0 comments on commit 171ce86

Please sign in to comment.