LEDTimedCandle
I did a lot of research on battery powered LED candle lights circuits and code and was unable to find a time controlled LED candle light.
As I have some ATTiny13 left, I decided to use this little chip. As I want the LED to do a candle light for about 4 hours and then stay off for another 20 hours, I need one timer and counter. Another timer/PWM is needed to imitate a candle flickering. But the ATTiny13 has only one timer. On the ATTiny13, you can either use the timer or PWM.
ISR (WDT_vect)
{
sec8_counter++;
#ifdef USE_HEART_BEAT_LED
digitalWrite(LED2, HIGH);
delay(1);
digitalWrite(LED2, LOW);
#endif
if(sec8_counter>=HOUR_INTERVAL){
sec8_counter=0;
if(bLedIsOn==1){
on_hours++;
if(on_hours>=MAX_ON_HOURS){
bLedIsOn=0;
off_hours=0;
}
}else{
off_hours++;
if(off_hours>=MAX_OFF_HOURS){
bLedIsOn=1;
on_hours=0;
}
}
}
wdt_reset();
}
Fortunately, the ATTiny has also a Watchdog timer that can be used to call an interrupt function. The watchdog runs with a separate 128kHz clock, independent from the CPU clock. The largest timeout is 8 seconds. So I need to count this 450 times to have one hour.
void loop ()
{
noInterrupts();
if(bLedIsOn==1){
doCandle();
}else{
digitalWrite (LED, LOW);
goToSleep ();
}
interrupts();
}
The code makes the ATTiny13 sleep for another 8 seconds or light the LED. The ON phase is 4 hours and the sleep phase will be extended to 20 hours. The sequence starts with the ON phase, when power is applied. In the ON phase I measure about 3mA and in the OFF phase, the circuit needs 300µA.

The circuit is documented in the Arduino code file. Running the ATTiny13 at lower clock than 9.6MHz did not change the power usage. But I switched to the 1.2MHz internal clock and disabled BOD (auto power down for low power) to get a longer runtime with two or three AA batteries.
https://github.com/hjgode/LEDTimedCandle/tree/master
CREDITS to all who share their knowledge, especially: