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

User avatar
By TerryE
#33912 This was also for others to see an example of how to approach this . The sort of response that this will give is:
Code: Select all The pin value has changed to 1  with last event 1 after these contacts: 20  -25 40 -50 75
So you will see the bounces and their timing.

The tmr.now() glitch is less in SDK 1.4 AFAIK, and all this does is to introduce a very occasional perverse time offset in the logged open/close events. The point is that this type of logging will allow you to set a sensible dwell time for your actual relay. It can be commented out in production once the relay has been calibrated.

IMO, the 50µS delay is far too short for a mechanical device . You could comment it out and it will make little difference. What is adding a backoff delay is the print statement between the cb entry and the new trigger statement. (How long does it take to print 13 chars at 9600 baud?). I can't remember if this is synchronous or done by the UART driver asynchronously, but I do know that UART output does block possibly after a buffer element and doing too much can trip the software watchdog.

In general we always need to be aware of the bastardised version of the Heisenberg Uncertainty Principle: the observation always effects the experiment. (This isn't really anything to do with quantum physics hence bastard). But if you add UART-based logging to a time-critical process, then you will change the timing of what you are trying to instrument. Appending to an array will typically take a lot less time than the mechanical up/down of a bouncing relay and so give you a valid read-out.

Try my example and let us know what is being logged.
User avatar
By GbrBrs
#61389 Hi there,

Just to share what I did. Basically I used bounce_blocker, which I set to 1 after the first button push, and then I start a timer, which will change bounce_blocker to 0 after 1 second. The bounce_blocker is used in the button's function. It'll allow state change only if bounce_blocker is 0.
Code: Select all-- setting up the button
button = 6
gpio.mode(button, gpio.INPUT, gpio.PULLUP)
local bounce_block = 0
-- toggle switch
gpio.trig(button, "both", function(level)
    print (bounce_block.. " bounce block") 
    if bounce_block == 0 then
        tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function()
            bounce_block = 0
            print ("bounce block has expired")
        end)
        if gpio.read(led1) == 0 then
              print("Button toggle, power is off, turning on");
              gpio.write(led1, gpio.HIGH);
              print("LED ON");
              gpio.write(relay, gpio.HIGH);
           
        else
              print("Button toggle, power is on, turning off");
              gpio.write(led1, gpio.LOW);
              print("LED OFF");
              gpio.write(relay, gpio.LOW); 
        end
        bounce_block = 1
    end
end)


I hope this helps.

Cheers
g.
User avatar
By NullPointer
#61516 *EDIT* Looking at the post above and it's verbatim what I did.

Bit late to the party but perhaps someone will find this useful. I have a microswitch I needed to debounce and found that the following works perfectly;

Code: Select allmodule("switch", package.seeall)

switched = false

--set the switch as interrupt, with debounce
function initialise()
    ignorebounce = false
    gpio.mode(8, gpio.INT, gpio.PULLUP)
   
    gpio.trig(8, "both", function(level)
        if level == 0 and ignorebounce == false then
            ignorebounce = true
            print("Switch pressed.")
            switched = not switched

            tmr.alarm(0, 1000, tmr.ALARM_SINGLE,
                function()
                    ignorebounce = false
                end)
        end
    end)
end


I'm pretty new to Lua, so perhaps the above has issues (certainly you can't press more than once per second) but it works for me :D