Loading...
Pages: [1]   Go Down
Author Topic: Why does Power Down not work here?  (Read 130 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 2
Posts: 425
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: 336
Posts: 36476
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: 2
Posts: 425
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: 47
Posts: 2573
"We're a proud service of the Lost Electricity Reclamation Agency"
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

Get the infamous "One Million Ohms" board at tINDIE.com: http://tinyurl.com/BuyMohms

Pages: [1]   Go Up
Print
 
Jump to: