1

For my attiny85, I'm having trouble getting it to sleep and wake up cleanly.

I have a sleep mode with a watchdog timer that wakes every 8 seconds, or when interrupted. And then when that has repeated 4 times, a second sleep mode that doesn't wake unless interrupted. Interruptions are via a button press. But this waking up isn't happening they way I'd like.

I'm unsure how the ISR() is meant to used instead of my main loop.

#include <OneButton.h>

#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer


ISR (PCINT0_vect)
 {

 }  // end of PCINT0_vect

// watchdog interrupt
ISR (WDT_vect)
{
   wdt_disable();  // disable watchdog
}  // end of WDT_vect



void setup ()
  {
     pinMode(0, OUTPUT);
  button.attachDoubleClick(doubleClick);
  button.attachPress(held);

  button.setClickTicks(250); //300
  button.setPressTicks(500); //800

   DDRB =  0b00000011;          // set PB1 (= OCR1A) to be an output
   setFrequency(38000);

  resetWatchdog ();  // do this first in case WDT fires

  // pin change interrupt (example for D4)
  PCMSK  = bit (PCINT2);  // want pin D2 / pin 7
  GIFR  |= bit (PCIF);    // clear any outstanding interrupts
  GIMSK |= bit (PCIE);    // enable pin change interrupts
  }  // end of setup

void loop ()
  {
    button.tick();

    sleepTimer++;

    if (sleepTimer > 240000)
    {
      goToSleepA ();
    }
    if (inactiveTick > 3) //
    {
     goToSleepB ();
    }
  }

////////////////////////////////////////////////////////////////
// Button click settings
//////////////////////////////////////////////////////////////

void doubleClick()

{

  inactiveTick = 0;

      four();
      delay(interPulsedelay);

  //checker light
   for(int a =0; a < 6;)
  {
    digitalWrite(0, HIGH);
  delay(50);
  digitalWrite(0,LOW);
  delay(50);
  a++;
  }

  clickFound = true;
  sleepTimer = 0;
}

void held()
{
  inactiveTick = 0;

      nine();
      delay(interPulsedelay);    

  //checker light
  for(int a =0; a < 3;)
  {
  digitalWrite(0, HIGH);
  delay(100);
  digitalWrite(0,LOW);
  delay(100);
  a++;
  }

  clickFound = true;
  sleepTimer = 0;
}

////////////////////////////////////////////////////////////////
// sleeping functions
//////////////////////////////////////////////////////////////

///http://www.gammon.com.au/forum/?id=11497

void resetWatchdog ()
  {
  // clear various "reset" flags
  MCUSR = 0;    
  // allow changes, disable reset, clear existing interrupt
  WDTCR = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  WDTCR = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay
  // pat the dog
  wdt_reset();  
  }  // end of resetWatchdog

void goToSleepA ()
  {

  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  noInterrupts ();       // timed sequence coming up
  resetWatchdog ();      // get watchdog ready
  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep                
  sleep_disable ();      // precaution
  power_all_enable ();   // power everything back on
  inactiveTick++;
  }  // end of goToSleep

void goToSleepB ()
  {

    inactiveTick = 0;
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu();                            
  sleep_disable();  
  power_all_enable();    // power everything back on

  }  // end of goToSleep

That's the vitals of the sleeping code.

I'd be very grateful if anyone could shed a little light on the matter.

closed as unclear what you're asking by Nick Gammon Feb 4 '16 at 20:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please avoid linking to code in pastebin: your code won't be available in anymore in 1 month! Rather try to reduce it to its important parts (e.g. setup, loop and your 2 sleep functions) and add it directly to your question. – jfpoilpret Nov 17 '14 at 6:00
  • I'd try using the external interrupt, INT0, instead of PCINT2 (just change GIMSK and the ISR). Also, what do you mean by isn't happening they way I'd like.? Is it not working at all, or partially? – Gerben Nov 17 '14 at 19:18
  • But this waking up isn't happening they way I'd like. - please clarify. Is it not sleeping? Not waking? Waking too soon? Not waking on the external interrupt? – Nick Gammon Aug 22 '15 at 2:31
  • As there has been no response to my question I'm going to close this question as "unclear what you are asking". Feel free to edit it with new information. Thanks for your understanding and cooperation! – Nick Gammon Feb 4 '16 at 20:34

Browse other questions tagged or ask your own question.