1
\$\begingroup\$

I have an Adafruit Feather ATMega 32u4. I want to put it into sleep mode and wake it up with pressing a switch as an external interrupt. This is what I tried so far and which worked:

  • switching a LED on and off with the switch
  • putting the Adafruit to sleep and let it wake up with a watchdog timer. I tried to combine the code examples that I found in the Internet to make the code fit my board. I manage to put it to sleep but then I have to reset it because the Interrupt doesn't work. I would be very happy for any help and please don't blame me, I am a complete beginner!

Here is my code:

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>

int pin2 = 2;

void pin2Interrupt(void) {
/* This will bring us back from sleep. */

detachInterrupt(digitalPinToInterrupt(2));
}


void enterSleep(void){
/* Setup pin2 as an interrupt and attach handler. */

delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
sleep_enable();

attachInterrupt(digitalPinToInterrupt(2), pin2Interrupt, RISING);

if (digitalRead(pin2)){
sleep_mode();}

/* The program will continue from here  
   First thing to do is disable sleep. */
sleep_disable(); 
}


void setup(){
Serial.begin(9600);

/* Setup the pin direction. */
pinMode(pin2, INPUT_PULLUP);

Serial.println("Initialisation complete.");
}

int seconds=0;


void loop(){
delay(1000);
seconds++;

Serial.print("Awake for ");
Serial.print(seconds, DEC);
Serial.println(" second");

if(seconds == 3){
Serial.println("Entering sleep");
delay(200);
seconds = 0;
enterSleep();
}
}
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.