Instable input using reed contact

deejay808:
Well, let me give you some additional background on the system. These sensors are an extension of my domotics system (Loxone). The arduino and the loxone system communicate using UDP messages over my local network. Whether that is a good choice may be another discussion. Anyway, it works already for about 3 years. Since the last update of the loxone system, I've seen 'open windows' that are in fact closed pop up almost every day; just 1 or two of them.
What actually happens is, when a read sensor opens, a message is send to the loxone. When it closes, another message is send. I have the impression that the handling of the messages has been changed such that 2 consequent messages in a short time might not be 'seen'. Just a guess ... because that is the only thing that changed in the whole system. Routers and switches have been reset
Of course, the 'sparks' in the circuit from the reed sensors to the arduino were there before. It is only now that they have become a problem.
If the sensors or their placement would be bad, I think I should see a lot more high/low events in my log. So I am quite sure that that is not the problem.

This would have been good information to know at the beginning. I was under the impression that you had just assembled this and were still in the "ironing out the wrinkles" phase.

The fix might be as simple as using a software debounce filter on all of the switch inputs. For slow things like doors and windows you can probably get away with a relatively long settling interval, like 250 ms or so.

dlloyd:
With the long cable run and wireless RF interference, mains AC interference, etc, it would be good to protect the Arduino's power rails as well as the input pin. I suggest using an opto isolator.

To make things simple, I'm quite sure you could use just the Arduino's supply (which defeats power-rail isolation), but the IRLED current limiting resistor helps protect the GND rail and the IRLED itself protects the 5V rail. The input is isolated and would not see any interference or any issues due to voltage drop through the long cable run. Therefore, no filtering is needed.

This solution looks clean to me. Concerning the IR led: there are 18 'devices' (reed sensors / smoke detectors / presence sensors) that are wached over by the Arduino mega; I suppose in that case an external supply might be required?

Maybe not. If you're powering through Vin, then the Vin voltage could be used to power the IRLED and then there'll be no load on the 5V regulated output. For example, if Vin=12V and R=1K, then there'll be about 10mA needed per sensor (180mA for 18 sensors). The regulated 5V will still have its full capacity available for other stuff.