Skip to content

Instantly share code, notes, and snippets.

@iori-yja
Created July 23, 2013 11:06
Show Gist options
  • Save iori-yja/6061589 to your computer and use it in GitHub Desktop.
Save iori-yja/6061589 to your computer and use it in GitHub Desktop.
Arduino/AVR sleep function kicked by Pin Change Interrupt(GPIO interrupt).
#include <avr/sleep.h>
void setup()
{
pinMode(6, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
digitalWrite(13, HIGH);
}
void pm_sleep() // here we put the arduino to sleep
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
* http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
* there is a list of sleep modes which explains which clocks and
* wake up sources are available in which sleep mode.
*
* In the avr/sleep.h file, the call names of these sleep modes are to be found:
*
* The 5 different modes are:
* SLEEP_MODE_IDLE -the least power savings
* SLEEP_MODE_ADC
* SLEEP_MODE_PWR_SAVE
* SLEEP_MODE_STANDBY
* SLEEP_MODE_PWR_DOWN -the most power savings
*
* For now, we want as much power savings as possible, so we
* choose the according
* sleep mode: SLEEP_MODE_PWR_DOWN
*
*/
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
sei();
sleep_mode();
/*- long time passed -*/
sleep_disable();
}
ISR(PCINT2_vect) {
cli();
digitalWrite(13, HIGH);
sei();
Serial.println("fuga");
}
void loop()
{
Serial.println("hoge");
sei();
digitalWrite(13, LOW);
delay(1000);
/*- PCINT22 is Arduino pin6.
* This is corresponding to PCI2.-*/
PCMSK2 = 1 << 6;
PCICR = 1 << 2;
Serial.println("hoge");
delay(100);
pm_sleep();
while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment