-->
Page 1 of 3

loops for delays or timing

PostPosted: Sun Nov 01, 2015 8:35 am
by Wjflier2
Hi all,
I'm trying to use "while loops" on esp8266 relay switch to turn on for a set period of time then off.
with this code:
gpio.write(led1, gpio.HIGH);
loop=1
while loop<=6000000 do
print(loop)
loop=loop+1
end

--elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);

it doesn't work, no mater what the value is led turns off at about 4 seconds, even if the value is 60000000000 or more.
Right now I'm using tmr.delay which has two problems.
One, I can't interrupt it, and two, shuts down after a long delay.
Anyone have a solution? I'm not a wiz at programing, so be kind.

Thanks,
Bill

Re: loops for delays or timing

PostPosted: Sun Nov 01, 2015 9:08 am
by xtal
From what I have read use tmr.delay only for 8ms or less
use tmr.alarm for larger delays

turn led on
tmr.alarm(0,time ms,1,function() -- in this case set the 1 to 0 1=interval[keeps repeating] 0=timout [run 1 time]
turn led off
tmr.stop(0) -- stop tmr alarm if needed
end)
next inst

This is not verified to work led will turn on,, time delay starts in background, next inst executed ~~~~~~after time ms led turns off ,, if it works properly
keep in mind that you code has been running not pausing anywhere ,, if it works properly

Re: loops for delays or timing

PostPosted: Mon Nov 02, 2015 7:21 am
by Wjflier2
appreciate your response Xtal, but doesn't seem to do the trick.
Here's the code I'm working with. It's a esp8266 web server written by Rui Santos.
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.HIGH);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "ON2")then
gpio.write(led2, gpio.HIGH);
--loop between HIGH and LOW a set amount of time
gpio.write(led2, gpio.LOW);

gpio.write(led3, gpio.HIGH);

gpio.write(led3, gpio.LOW);

gpio.write(led4, gpio.HIGH);

gpio.write(led4, gpio.LOW);
end

I'm trying to cycle the leds a set amount of time, when Led1 goes LOW then LED2 goes HIGH and so on......
the tmr.alarm(0,duration,1,function()

Re: loops for delays or timing

PostPosted: Mon Nov 02, 2015 8:56 am
by xtal
You can use tmr.wdclr() in you loop at some interval, this would stop any restarts, BUT
Interrupting would still be a chore. Tight loops for some reason are not interrupted easily, Must be a nodeMCU issue. Maybe the following could be interrupted . I would think that any tmr function will do some interrupt blocking, tmr.delay most likely the worst. You could make this or similiar code a function, then

led hi, do function, led low do function


x = tmr.now() + millisec
led on
for y = tmr.now(), x do
if y >= x/2 ? then tmr.wdclr()
next y
led off