you either need to add proper filtering or switch to polling to ensure proper software debouncing is in place
If you have the possibility, put an oscilloscope in there to see what you are battling against.
I agree that polling could potentially cure this but it may affect the operation of the machine as it will have to poll the switch multiple times before it is "sure" it is closed THEN stop the motors moving it towards the switch.
- You add, across the switch, a series Resistor-Capacitor combination of 470 ohms and 4.7 uF (Or other appropriate combination)When you press the button, you will have a discharge time constant of 2.2 ms during which Arduino will 'see' the transition with it's input circuit, which has a small amount of hysteresis and it should get a clean single, 'debounced' value. When you release the button, you will have a charge time constant of 50 ms, which is fast enough that you can't push the button again 'too soon'. Unless you have a 20 Hz finger, in which case you could be a drummer with U2.
http://www.thebox.myzen.co.uk/Tutorial/Protection.htmlAlso adding capacitors at the arduino end can help. Also consider using the analogue input to detect the push switch. That way you can set your own thresholds instead of having them set for you by the digital input pins.
How much trouble is this worth to you...
The switch is ~2m from the Arduino and is connected using 7-core trailer cable (http://www.maplin.co.uk/7-core-trailer-cable-70). Some of the cores in that cable are used to power two DC motors which can typically pull a total of about 4A@12V, other cores are used to carry the signal from a hall sensor which operates at 12V.
Hey MarkT.This sounds pretty convincing to me. I will try to get hold of some shielded twisted-pair cable and add the 100pF capacitor.
Do you think my idea to reduce the resistor value in order to make the problem more reproducible is a good idea? Basically I will swap the 1k for 10k, check that the cap and cable fixes the issue still then switch back to 1k.
Now I just need to get hold of ~2m of this cable here in the UK! I'll get back to you guys once I manage to do so and install it.