Author Topic: Reed Switch Problems  (Read 627 times)

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: Reed Switch Problems
« on: January 12, 2017, 11:58:18 pm »
The

Code: [Select]
Thread.Sleep(10000)
Thread.ClearHardwareEvent()

In inSync() won't do anything (except for a small delay), so you should remove that.

The second occurance in ReedSwitchChanged() should come first, ie before if ReedSwitch = True Then....

What this is doing is compensating for switch bounce. Because the reedswitch is a physical piece of metal, it can "bounce" so you get several interrupts while it is "bouncing". If you read the ReedSwitch while it is bouncing, you can get false readings. The idea behind Thread.sleep(10000) is to wait 10ms for it to stop bouncing, then read it, hence the delay has to come before you read the switch.

The Thread.ClearHardwareEvent() clears any waiting interrupts in the routine that called it, in this case probably caused by the switch bouncing (otherwise the interrupt may trigger twice - the queue is only 2 deep).

This means that you can't read events that happen faster than every 10ms, but given that this is a physical switch, that is probably not possible anyway.

Hope this explains!
« Last Edit: January 13, 2017, 12:01:56 am by Nick_W »