Hello everyone!
I was trying to edit the example code(WakeInterrupt) from SimpleSleep library for AtTiny13A, but have some problems. When I flashed the original sketch(WakeInterrupt) even LED does not turn on. I replaced sleep.deeply() function with sleep.deeplyFor(). Now LED turns on and AtTiny switches to sleep mode but interrupts don't work.
Can anyone help me with this problem? I attached my code below.
But I think I found the error. This is the implementation of a timed sleep in the library:
volatile uint8_t wdt_triggered = 1;
ISR (WDT_vect)
{
wdt_disable();
wdt_triggered = 1;
}
static void timed_sleep(uint32_t sleepMs, uint8_t mode, uint8_t bod, uint8_t interrupts)
{
do
{
// If we are not waiting on the WDT, and there is time still to sleep, setup the WDT (again)
if (wdt_triggered && sleepMs)
{
wdt_triggered = 0;
wdt_enable(wdt_period_for(&sleepMs));
WDTCSR |= (1 << WDIE);
}
set_sleep_mode(mode);
cli();
sleep_enable();
#ifdef sleep_bod_disable
if(!bod)
{
sleep_bod_disable();
}
#else
(void)(bod); // Silence warning
#endif
// Caution, with interrupts disabled the only way you are likely
// to wake up is with a reset
if(interrupts)
{
sei();
}
sleep_cpu();
sleep_disable();
sei();
} while(!wdt_triggered || sleepMs > 0);
}
Problematic is do-while loop. Even if the CPU awakes the code won't return as long as sleepMS is greater than zero. The ATtiny13a has a maximum WDT period of 8s, if you call this routine with sleepMs = 10000, it will do the first sleep with a sleepMs at the value of 2000, so it won't return if the interrupt would wake the CPU during these 8 seconds. During the last 2 seconds I would expect the button to work.
You might get it to work better if you set the sleepMs value to 8s.