Go Down

Topic: how can I DISABLE SLEEP MODE (Read 853 times) previous topic - next topic

icom5217

Hello,

I'm using an arduino mega 1280 and am running into a small issue. I enter sleep mode but cannot disable it once I enter sleep mode.

My code is the following:

Code: [Select]
#include <avr/sleep.h>

int counter =0;

void setup()
{
 
Serial.begin(9600);
  attachInterrupt(0,wake, RISING);
 
}


void loop()
{
  counter++;
  delay(1000);
  if (counter == 5)
  {
   sleepNow();
  }
 
}


void wake()
{
  Serial.println("WORKING");
 
}


void sleepNow()
{
 
   Serial.println("SET_SLEEP_MODE");
 
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
 
  Serial.println("SLEEP_ENABLE");
  sleep_enable();          // enables the sleep bit in the mcucr register
                             // so sleep is possible. just a safety pin
 
  attachInterrupt(0,wake, RISING);
 
   
  Serial.println("SLEEP_MODE"); 
                               
  sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
 
  Serial.println("SLEEP_DISABLE");
 
  sleep_disable();         // first thing after waking from sleep:
                             // disable sleep...
   Serial.println("MCU is back in business");
 
}



My output is the following:

WORKING //Press the button to make sure interrupt is working BEFORE entering sleep mode
SET_SLEEP_MODE
SLEEP_ENABLE
SLEEP_MODE

//I press the button as many times as I want but never seem to get any response from the arduino.

Can someone please tell me what I'm doing wrong ?

johnwasser

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

I'm no expert but I think that may be too deep a sleep. You want a sleep level where the timer interrupts don't wake you every millisecond but not so deep that external interrupts (like your button) are ignored.  I don't have the datasheet handy so I leave it to you to figure out what level to use. 
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up