How to improve turning ON light for set amout of time.

Our embedded target board needs to support delayed/egress lighting. If the target board is sleeping, the wakeup switch input (when "closed") will be used to wake up the target board and turn ON the light. Once ON, the light would stay ON until either the switch input "opens" OR a 2 minute timer expires.

This will have to function via the wakeup switch input, as well as when the target board is powered up via the traditional means.

Please see Code below. The function I wrote below will be called every

40ms.

How can it be improved?

When switch is "closed", light will turn ON and then may turn OFF. Is it possible to do one or the other in "if' body.

When switch is "open", timer will reset and light will turn OFF. Doing this every 40ms seems to be waste of CPU.

Thank you!

void light_update(void) { static unsigned on_timer = 3000;

/* Is switch closed? */ if ( closed ) { /* Turn ON light */ output = 1; if (on_timer != 0) { on_timer--; } else { /* Turn OFF light */ output = 0; } }

else { /* Reset Timer */ on_timer = 3000;

/* Turn OFF light */ output = 0; }

}

task(40ms) { light_update(); }

--------------------------------------- Posted through

formatting link

Reply to
janii
Loading thread data ...

Yes, it is doing unneeded processing, but that probably doesn't matter. I suspect that is true of your application.

If you want to minimize the processing, use event-driven processing. Interrupt on switch changes. When you need to service something after a given amount of time, generate a timer interrupt when you need to do work. Sleep otherwise.

That said, most interrupt-based delays of long intervals will require counting shorter interrupt intervals.

Thad

Reply to
Thad Smith

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.