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

User avatar
By LeprA
#18545 I try to use the tmr to do a pwm function but either i get a memory is leak or the tmr do not work.
I need a faster pwm than the one that is included that is why i am doing this.


This one is not working when i try to use the "tmr" as local variable, the tmr.delay just don't do anything in this case.
----------------------------------------------------------------------------------------------
Code: Select allfunction forwardandrewind()
      local tmr = tmr
      if(forward == 1) then
         forward = 0
         gpio.write(3,gpio.HIGH)
      else
         forward = 1
         gpio.write(3,gpio.LOW)
      end
         
      for x=1,16000 do
         gpio.write(4,gpio.HIGH)
         tmr.delay(1)
         gpio.write(4,gpio.LOW)
         tmr.delay(1)
         tmr.wdclr()
      end
      tmr = nil

      print(node.heap())
   end


This one is working when i try to use the "tmr" but leak memory.
----------------------------------------------------------------------------------------------

Code: Select allfunction forwardandrewind()
      if(forward == 1) then
         forward = 0
         gpio.write(3,gpio.HIGH)
      else
         forward = 1
         gpio.write(3,gpio.LOW)
      end
         
      for x=1,16000 do
         gpio.write(4,gpio.HIGH)
         tmr.delay(1)
         gpio.write(4,gpio.LOW)
         tmr.delay(1)
         tmr.wdclr()
      end
      tmr = nil

      print(node.heap())
   end



Any suggestions?
User avatar
By TerryE
#18584 The tmr.delay() argument is in µSec, so a delay of 1 µSec is going to look like its not working.

Setting _G.tmr = nil is a bad idea. This will cause tmr to be re-resolved on each reference with the sort of leak that you see.