Posts tagged ‘timer’

LED candle light with timer

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 4hours and then stay of for another 20 hours, I need on timer and counter. Another timer/PWM is needed to immitate a candle flickering. But the ATTiny13 has only one timer. On the ATTiny13 you can either use the timer or PWM.

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.

// watchdog interrupt
ISR (WDT_vect)
{
  sec8_counter++;

#ifdef USE_HEART_BEAT_LED  
  //flash 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; //switch to OFF mode
        off_hours=0;
      }
    }else{
      off_hours++;
      if(off_hours>=MAX_OFF_HOURS){
        bLedIsOn=1; //switch to ON mode
        on_hours=0;
      }
    }
  }
  wdt_reset();
}  // end of WDT_vect

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.

void loop ()
{
  //sleep 20 hours and work 4 hours
  noInterrupts();
  if(bLedIsOn==1){
    doCandle();
  }else{
    digitalWrite (LED, LOW); //ensure LED is OFF
   goToSleep ();
  }
  interrupts();
}  // end of loop

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.

UPDATE 2019/03/10: The wdt calculation is wrong and I changed the 450 cycles for one hour to 388. See github README for full update. The main issue is that the ATTiny13 datasheet says 128kHz but means 131072Hz and not 128000Hz and the wdt oscilator never cycles that fast but more or less at around 113000Hz.

https://github.com/hjgode/LEDTimedCandle/tree/master

CREDITS to all that share their knowledge, especially:

https://github.com/MCUdude/MicroCore

http://forum.arduino.cc/index.php?topic=200590.0

https://electronics.stackexchange.com/questions/74840/use-avr-watchdog-like-normal-isr

http://www.ece.cmu.edu/~koopman/lfsr/index.html

Howto run an application periodically

Hello

for some reason you might want to rerun an application very day or every hour. In commercial applications, one may want to let the device sync data every 4 hours, So you need an application that runs periodically at specified times or intervals. As this first sounds easy, it is not as simple as you mean.You cannot simply use timers, timers will not continue to run within Suspend mode.

To have an application run at specific times, Windows Mobile supports a notification database. Your device will already wake up periodically to reschedule events and to clean up the notification database. Normally, Windows Mobile Phone devices will awake every night at 0:00.

There are also some other possible notifications that can launch an application, see my old web site.

How can we do a scheduled application ourself? At first run, the scheduler application has to delete all previous notification entries of the notification database. Then it has to create a new timed notification and in our sample, it will then launch another application. The scheduler application itself should be small and only care about the schedules. The worker application can be what you want and will be started by the scheduler application.

Continue reading ‘Howto run an application periodically’ »