Advertisement
RuiViana

Testa_Sleep.ino

Jul 19th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <watchdogHandler.h>
  2. #include <avr/power.h>
  3. #include <avr/wdt.h>
  4. #include <avr/sleep.h>
  5.  
  6. volatile unsigned long secondsSinceStartup = 0;
  7. volatile unsigned long secondsLastUpdate = 0;
  8. const unsigned long secondsUpdateFrequency = 60 * 30;
  9. //-----------------------------------------------------
  10. void setup(void)
  11. {
  12.   //  Disable the watchdog timer first thing, in case it is misconfigured
  13.   wdt_disable();
  14.   Serial.begin(115200);
  15.   Serial.println("Hello!");
  16.   //  Just cool our jets for a sec here
  17.   delay(2000);
  18.   //  Run ISR(WDT_vect) every second:
  19.   setup_watchdog(WDTO_1S);
  20.  
  21.   // Valid delays:
  22.   //   WDTO_15MS
  23.   //  WDTO_30MS
  24.   //  WDTO_60MS
  25.   //  WDTO_120MS
  26.   //  WDTO_250MS
  27.   //  WDTO_500MS
  28.   //  WDTO_1S
  29.   //  WDTO_2S
  30.   //  WDTO_4S
  31.   //  WDTO_8S
  32. }
  33. //-----------------------------------------------------
  34. void loop(void) {
  35.   //  Has it been enough time to update?
  36.   if ( secondsSinceStartup - secondsLastUpdate > secondsUpdateFrequency) {
  37.     Serial.print("It's been this many seconds: ");
  38.     Serial.println(secondsSinceStartup);
  39.     Serial.print("and millis() says: ");
  40.     Serial.println(millis());
  41.   }
  42.   //  Go to sleep forever... or until the watchdog timer fires!
  43.   sleeping();
  44. }
  45. // This function will get called every time the watchdog handler fires
  46. //-----------------------------------------------------
  47. ISR(WDT_vect)
  48. {
  49.   //  Anything you use in here needs to be declared with "volatile" keyword
  50.   //  Track the passage of time.
  51.   secondsSinceStartup++;
  52. }
  53. //-----------------------------------------------------
  54. void sleeping()
  55. {
  56.   //  The precise sequence of these instructions is important for the ATMEGA238
  57.   //  SLEEP_MODE_PWR_DOWN: Disable every clock domain and timer except for the watchdog timer.
  58.   //  Only way to wake up the device is the WDT and the external interrupt pins.
  59.   //  The millis() timers will be disabled during sleep, which is why we track seconds in the
  60.   //  watchdog timer routine.
  61.   //  You could shut down your external sensors & hardware here
  62.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  63.   sleep_enable();
  64.   sei();
  65.   sleep_cpu();
  66.   sleep_disable();
  67.   waking();
  68. }
  69. //-----------------------------------------------------
  70. void waking()
  71. {
  72.   digitalWrite(13, !digitalRead(13));
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement