hi guys, does anyone know how to code a timer into a switch, say once the switch is activated keep it on for 20 seconds than shut it off?
so far i have
class power_input extends InputPort
{
name = "OPEN"
type = “number”
function set(value)
{
// update the display
server.show("OPEN!");
// set the power pin to whatever was passed in above, on or off
hardware.pin8.write(1);
}
}
i want to be able to set pin 8 to value of 0 after 20 seconds.
That code will make it turn off after 20 seconds, then not do anything else. It won’t come back on unless you call hardware.pin8.write(1) at some point.
The most common pattern that needs these is an inactivity timer. You want to put the imp either from powersave(false) to powersave(true) or even into sleep or deepsleep after (for sake of example) 12 seconds of no event.
Currently I use imp.wakeup(12, checkActivity)
checkActivity() checks a global variable ‘wasActive’. Any activity sets wasActive true. checkActivity() checks ‘wasActive’ at the beginning and always sets it to false before returning.
This results in the imp going to a lower powerstate after anywhere from 12-24 of seconds of inactivity. With the ability to retain a handle to a timer and cancel it, this could be change to a predictable exactly 12 seconds of inactivity. Ideally in addition to the option of cancelling, I could also choose to reset the timer toits original value or to a new value.
P.S. It would also eliminate the need for the global wasActive variable. Any activity would just restart (or cancel then create new) the timer instead of indirectly setting wasActive = true
Canceling wakeups is now implemented, but is in the release after release 23. imp.wakeup() returns a handle that you can use to cancel a wakeup as necessary.