-->
Page 1 of 3

simple delay, wait or pause function

PostPosted: Fri May 15, 2015 3:03 am
by joefly888
Working on webserver with LED control. I only want the LED to be on for 2 seconds when requested.

So is there a simple wait, pause, delay that I can put in after the GPIO=high before I can set it to low again?

Re: simple delay, wait or pause function

PostPosted: Fri May 15, 2015 3:53 am
by ReinholdHiller
Define:

boolean led = false;
unsigned long ticker;

in your "turn it on"-function do a
led=true;
ticker=millis();

in void loop() do:

if (led) {
if (millis()-ticker>2000) {
led=false;
turn led off.
}
}

Re: simple delay, wait or pause function

PostPosted: Fri May 15, 2015 8:31 am
by joefly888
hi , i incorporated your suggestion in this manner
Code: Select all        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
              ticker=millis();
              repeat
                 print ("hello");
                 -- do nothing as it is a delay
              until ( (millis() - ticker) > 2000 )
              gpio.write(led1, gpio.LOW);


if I declared the variable ticker at the beginning as
Code: Select allunsigned long ticker;


i get an error at boot with message of "lua: init.lua:1: '=' expected near 'long' "

if I removed the variable definition line, it boots fine, but when I get to the code of "ticker=millis()"
I get the panic message of "PANIC: unprotected error in call to Lua API (init.lua:29: attempt to call global 'millis' (a nil value))"

I am totally new to LUA so I am not sure if I am missing some syntax or something..

Re: simple delay, wait or pause function

PostPosted: Fri May 15, 2015 8:53 am
by j0hncc
the nodemcu/lua way is to use Timer. No "arduino" loop or millis() stuff necessary

Code: Select all...
function turnOffLed()
    -- you figure out this part
end
...
--  Turn on Led and then turn it off in 2 seconds
turnOnLed()
tmr.alarm(  0, 2000, 1, turnOffLed )
...
... execution continues with your other code immediatly