As the title says... Chat on...

User avatar
By eliabieri
#5687 Hi,
I want to write a program that continuously checks if IO Index 3 (GPIO0) is pulled to ground and if it is, "interupt.lua" is called which turns an LED on for some seconds.
Can you say whats wrong with my code? I have boot loops and so on

init.lua
Code: Select allgpio.mode(6, gpio.OUTPUT)
gpio.mode(3,gpio.INT)
while true do
    gpio.trig(3,"high",dofile("interupt.lua"))
    tmr.delay(100)
end



interupt.lua

Code: Select all  gpio.write(6, gpio.HIGH)
  tmr.delay(10000)
  gpio.write(6, gpio.LOW)


thank you!
User avatar
By dpwhittaker
#10636 Argument 3 is expecting a callback function, so unless your file is returning a function, you need to wrap it in a function:

Code: Select allgpio.trig(3,"high",function() dofile("interupt.lua") end)


Alternatively:

Code: Select allgpio.trig(3,"high",loadfile("interupt.lua"))


The first version reparses and compiles the file every time the interrupt happens, but frees the memory when it is done, while the second compiles the file to a function once and re-executes it each time, but keeps the compiled function in memory. So use the top version to optimize memory and the bottom for speed.

Also, tmr.delay is a busy loop, which may not give lua an opportunity to fire the interrupt routine (I'm not sure how hardware interrupts are handled in a single-threaded environment - you usually want to get in and out fast though). You may need to switch to tnr.alarm. It takes a callback function as well, which you would put the second led toggle inside of.

Finally, you don't need to put the trig in a loop: set it once and it should trigger every time the condition is met.
Last edited by dpwhittaker on Tue Feb 24, 2015 7:31 pm, edited 1 time in total.
User avatar
By Mikejstb
#10637 Thanks, that got it working!
Would you know why it seems that network flow (conn:on("receive"...) seems to stop when the calls are within a block, like an if-then-end.
I'm trying to post a cloud event when the interrupt PIO.trig happens. In my int function I set a variable to true, then use that variable in an if block to do the network io.
If I call the network calls in-line they work, in the if block they don't.