Pages: [1]   Go Down
Author Topic: Why does Power Down not work here?  (Read 281 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 443
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <avr/sleep.h>

void setup(){
  // disable ADC
  ADCSRA = 0; 
  pinMode(2,INPUT);
  attachInterrupt (0, gotoSleep, RISING);
  Serial.begin(9600);
  Serial.println("reset");
}

void loop(){

 
}

void gotoSleep(){
  Serial.println("gotoSleep");
  detachInterrupt (0);
  attachInterrupt (0, wake, RISING);
  set_sleep_mode (SLEEP_MODE_IDLE); 
  sleep_enable();
  sleep_cpu ();
 detachInterrupt (0);
 attachInterrupt (0, gotoSleep, RISING);
}

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

This simply doesn't work for me and I don't know why. After "reset", serial wont print anything. I have a multimeter hooked up to it so I know it's not going to sleep. I can get it to sleep fine if I put the sleep stuff in the setup. But if I comment out all of the sleep and interrupt lines in the gotoSleep function, it calls gotoSleep just fine when I press the button.

I must be missing something very obvious.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't do Serial.print() in an ISR. Serial output requires interrupts, which are disabled in an ISR.

Both of your ISRs have Serial.print() statements. They have to go.
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 443
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, ok my problem first was that I didn't realize I was using "IDLE" so I wasn't getting the multimeter readings I was expecting. Then I added the serial out to double check and like you said, that doesn't work. I changed it to power down and everything it behaving now. Thanks.
Logged

Grand Blanc, MI, USA
Online Online
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd do Serial.flush() and Serial.end() before sleeping, and Serial.begin() after waking up.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: