|
| 1 | +#include <avr/sleep.h> |
| 2 | + |
| 3 | +/* Sleep Demo Serial |
| 4 | + * ----------------- |
| 5 | + * Example code to demonstrate the sleep functions in an Arduino. |
| 6 | + * |
| 7 | + * use a resistor between RX and pin2. By default RX is pulled up to 5V |
| 8 | + * therefore, we can use a sequence of Serial data forcing RX to 0, what |
| 9 | + * will make pin2 go LOW activating INT0 external interrupt, bringing |
| 10 | + * the MCU back to life |
| 11 | + * |
| 12 | + * there is also a time counter that will put the MCU to sleep after 10 secs |
| 13 | + * |
| 14 | + * NOTE: when coming back from POWER-DOWN mode, it takes a bit |
| 15 | + * until the system is functional at 100%!! (typically <1sec) |
| 16 | + * |
| 17 | + * Copyright (C) 2006 MacSimski 2006-12-30 |
| 18 | + * Copyright (C) 2007 D. Cuartielles 2007-07-08 - Mexico DF |
| 19 | + * |
| 20 | + * This program is free software: you can redistribute it and/or modify |
| 21 | + * it under the terms of the GNU General Public License as published by |
| 22 | + * the Free Software Foundation, either version 3 of the License, or |
| 23 | + * (at your option) any later version. |
| 24 | + * |
| 25 | + * This program is distributed in the hope that it will be useful, |
| 26 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | + * GNU General Public License for more details. |
| 29 | + * |
| 30 | + * You should have received a copy of the GNU General Public License |
| 31 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 32 | + * |
| 33 | + */ |
| 34 | + |
| 35 | +int wakePin = 2; // pin used for waking up |
| 36 | +int sleepStatus = 0; // variable to store a request for sleep |
| 37 | +int count = 0; // counter |
| 38 | + |
| 39 | +void wakeUpNow() // here the interrupt is handled after wakeup |
| 40 | +{ |
| 41 | + // execute code here after wake-up before returning to the loop() function |
| 42 | + // timers and code using timers (serial.print and more...) will not work here. |
| 43 | + // we don't really need to execute any special functions here, since we |
| 44 | + // just want the thing to wake up |
| 45 | +} |
| 46 | + |
| 47 | +void setup() |
| 48 | +{ |
| 49 | + pinMode(wakePin, INPUT_PULLUP); |
| 50 | + |
| 51 | + Serial.begin(9600); |
| 52 | + |
| 53 | + /* Now it is time to enable an interrupt. In the function call |
| 54 | + * attachInterrupt(A, B, C) |
| 55 | + * A can be either 0 or 1 for interrupts on pin 2 or 3. |
| 56 | + * |
| 57 | + * B Name of a function you want to execute while in interrupt A. |
| 58 | + * |
| 59 | + * C Trigger mode of the interrupt pin. can be: |
| 60 | + * LOW a low level trigger |
| 61 | + * CHANGE a change in level trigger |
| 62 | + * RISING a rising edge of a level trigger |
| 63 | + * FALLING a falling edge of a level trigger |
| 64 | + * |
| 65 | + * In all but the IDLE sleep modes only LOW can be used. |
| 66 | + */ |
| 67 | + |
| 68 | + attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function |
| 69 | + // wakeUpNow when pin 2 gets LOW |
| 70 | +} |
| 71 | + |
| 72 | +void sleepNow() // here we put the arduino to sleep |
| 73 | +{ |
| 74 | + /* Now is the time to set the sleep mode. In the Atmega8 datasheet |
| 75 | + * http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35 |
| 76 | + * there is a list of sleep modes which explains which clocks and |
| 77 | + * wake up sources are available in which sleep mode. |
| 78 | + * |
| 79 | + * In the avr/sleep.h file, the call names of these sleep modes are to be found: |
| 80 | + * |
| 81 | + * The 5 different modes are: |
| 82 | + * SLEEP_MODE_IDLE -the least power savings |
| 83 | + * SLEEP_MODE_ADC |
| 84 | + * SLEEP_MODE_PWR_SAVE |
| 85 | + * SLEEP_MODE_STANDBY |
| 86 | + * SLEEP_MODE_PWR_DOWN -the most power savings |
| 87 | + * |
| 88 | + * For now, we want as much power savings as possible, so we |
| 89 | + * choose the according |
| 90 | + * sleep mode: SLEEP_MODE_PWR_DOWN |
| 91 | + * |
| 92 | + */ |
| 93 | + set_sleep_mode(SLEEP_MODE_IDLE); // sleep mode is set here |
| 94 | + |
| 95 | + sleep_enable(); // enables the sleep bit in the mcucr register |
| 96 | + // so sleep is possible. just a safety pin |
| 97 | + |
| 98 | + /* Now it is time to enable an interrupt. We do it here so an |
| 99 | + * accidentally pushed interrupt button doesn't interrupt |
| 100 | + * our running program. if you want to be able to run |
| 101 | + * interrupt code besides the sleep function, place it in |
| 102 | + * setup() for example. |
| 103 | + * |
| 104 | + * In the function call attachInterrupt(A, B, C) |
| 105 | + * A can be either 0 or 1 for interrupts on pin 2 or 3. |
| 106 | + * |
| 107 | + * B Name of a function you want to execute at interrupt for A. |
| 108 | + * |
| 109 | + * C Trigger mode of the interrupt pin. can be: |
| 110 | + * LOW a low level triggers |
| 111 | + * CHANGE a change in level triggers |
| 112 | + * RISING a rising edge of a level triggers |
| 113 | + * FALLING a falling edge of a level triggers |
| 114 | + * |
| 115 | + * In all but the IDLE sleep modes only LOW can be used. |
| 116 | + */ |
| 117 | + |
| 118 | + attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function |
| 119 | + // wakeUpNow when pin 2 gets LOW |
| 120 | + |
| 121 | + sleep_mode(); // here the device is actually put to sleep!! |
| 122 | + // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP |
| 123 | + |
| 124 | + sleep_disable(); // first thing after waking from sleep: |
| 125 | + // disable sleep... |
| 126 | + detachInterrupt(0); // disables interrupt 0 on pin 2 so the |
| 127 | + // wakeUpNow code will not be executed |
| 128 | + // during normal running time. |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +void loop() |
| 133 | +{ |
| 134 | + // display information about the counter |
| 135 | + Serial.print("Awake for "); |
| 136 | + Serial.print(count); |
| 137 | + Serial.println("sec"); |
| 138 | + count++; |
| 139 | + delay(1000); // waits for a second |
| 140 | + |
| 141 | + // compute the serial input |
| 142 | + if (Serial.available()) { |
| 143 | + int val = Serial.read(); |
| 144 | + if (val == 'S') { |
| 145 | + Serial.println("Serial: Entering Sleep mode"); |
| 146 | + delay(100); // this delay is needed, the sleep |
| 147 | + //function will provoke a Serial error otherwise!! |
| 148 | + count = 0; |
| 149 | + sleepNow(); // sleep function called here |
| 150 | + } |
| 151 | + if (val == 'A') { |
| 152 | + Serial.println("Hola Caracola"); // classic dummy message |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // check if it should go to sleep because of time |
| 157 | + if (count >= 10) { |
| 158 | + Serial.println("Timer: Entering Sleep mode"); |
| 159 | + delay(100); // this delay is needed, the sleep |
| 160 | + //function will provoke a Serial error otherwise!! |
| 161 | + count = 0; |
| 162 | + sleepNow(); // sleep function called here |
| 163 | + } |
| 164 | +} |
| 165 | + |
0 commit comments