I’m working on a simple garage door sensor with a magnetic switch, and attaching it to an InterruptPort. For my tests, I just have a jumper from IO14 and a jumper from the 3v, and to turn it “on” I cross them.
Anyway, here’s my simple code. The problem is it’s unreliable. Here’s the output where I basically crossed and uncrossed the jumper wires every second or two. My final state was open, so the interrupt should have sent me a 0 state, but it didn’t.
I’m new to this, so I’m sure I’m doing something wrong…
When I do that, I only get either true or false (depending on whether I use InterruptEdgeHigh or InterruptEdgeLow) only. What I really wanted was true for closed, false for open so I needed both.
What I’m trying to do as my first project is a basic garage door sensor project. I have a relay switch hooked up to a wireless transmitter for my door opener, and I have a magnetic switch to tell me when the door is open/closed. I finished the hardware and was on to the software when I noticed this issue.
I was hoping I could use an InterruptPort and it would tell me anytime the door open/closes reliably. But it seems to “miss” events for some reason as I described. It works most of the time but not reliably for every event like I’d hoped. Seems like such a simple thing. Maybe I should use the 5v source instead of 3v? Or use my own pulldown resistor instead of the CPU’s? Any other ideas?
Jason,
Have you tried increasing the GlitchFilterTime?
Try adding this line as first line in Main() would set it to 200 miliseconds. In your scenario, you could probably even set it to 1000. Good luck!
Microsoft.SPOT.Hardware.Cpu.GlitchFilterTime = new TimeSpan(0, 0, 0, 0, 200);
And I got this output. You can see from the times when I crossed/uncrossed my jumpers, but I was doing it rapid-fire, more often than once a second, so the glitch filter seems to be doing its thing but I’m not getting the low edge interrupt reliably at all.
Again my setup is very simple, wire in IO14 and wire from 3v.
I also tried turning off the glitch filter. It would trigger on low edge more often, but in the below I still ended with the wires not touching and never got the low edge interrupt.
With or without an external pulldown resistor and Port.InterruptMode.InterruptEdgeLow, I get interrupts with value 0 when I plug in the 3.3V :o
If I unplug 3.3V, I of course get an interrupt with value 0, as expected. Something weird, here :think:
Edit: it looks like a glitch problem because I’m using the same code on my professionnal project and don’t have this behaviour. The “EdgeHigh” signal comes from a wheel encoder.
Maybe you can post a simple schematic of your wiring? I suspect your seeing bounce events longer than the glitch filter. I have found that triggering my interrupt on both edges and even adding a bit of a delay can help in such cases.
For example
Interrupt->SW on
Record Timestamp-On
Interrupt->SW off
Record Timestamp-Off
If (Timestamp-Off - Timestamp-On > threshold) Then event is valid
Gus - I didn’t have any problems with one edge or the other so that seems to work. As for the polling method, yeah - I’ll do that for now, should work fine - thanks for your help.
Bec - thanks for confirming I’m not missing anything at least too obvious
Jeff - based on my limited understanding I don’t think it’s glitch filter related. If I disable the glitch filter wouldn’t I get multiple events, but still finally get the right value at some point? When I ran without glitch filter, I still completely missed my final event - I had the contact open, but never got a false value from the interrupt. I also tried different glitch filter values, including extremely long ones (1s) and missed some low event deliveries.
Not sure how the guys have covered glitch filter on edgeBoth as this maybe tricky. This can cause one of the edges to be lost between glitches! This can be really tricky.
Can someone try something? Make a small program (5 lines) that reads a button and then debug.print up or down.
Ideally, you should see
up
down
up
down
up
but if there is an error you would see
up
down
up
up <<----------- this shouldn’t happen but if there is glitch filter then it may happen?
down
up
down
I will forward this to our developers to double check
I’ve just tried with a mecanical relay and I have the same result.
But this time, it was longer before it happens. The main reason for this, to me, is the fact the the relay can’t switch very fast, so I wait 1 sec between two switches.
Here are the components I’ve used for those tests :
I get the below by crossing/uncrossing the wires. Without glitch filter I would expect multiple events maybe, but in this case I ended with an open circuit and never got any open circuit interrupt.
01/01/2009 00:00:42: interrupt=False
01/01/2009 00:00:42: interrupt=True
01/01/2009 00:00:43: interrupt=False
01/01/2009 00:00:43: interrupt=True
01/01/2009 00:00:43: interrupt=True
01/01/2009 00:00:43: interrupt=True
01/01/2009 00:00:44: interrupt=True
Is this expected, and if so is there any point at all to ever using InterruptEdgeBoth, as it seems it serves no purpose since there’s no guarantee you’ll receive any given event.