I have wired the switch like GND->switch->pin 7
When the switch is off, digitalRead will for sometime return 1(as expected) and then randomly return a one or two zeroes.
The same thing goes when the switch is on. It returns zeroes(as expected) and then a one or two ones.
I thought of that, and I tried changing the mechanical switch with an optocoupler. The result is the same. I don't know if the problem is because of the big 3-phase transformer nearby.
When the switch is off, digitalRead will for sometime return 1(as expected) and then randomly return a one or two zeroes.
Sounds like dodgy wiring to me. Take the switch and associated wiring out of the circuit leaving a bare Arduino board. What do you see when you run the program ?
UKHeliBob:
Sounds like dodgy wiring to me. Take the switch and associated wiring out of the circuit leaving a bare Arduino board. What do you see when you run the program ?
For sometime returns 1, and at some point return one or two zeroes, then ones again? This behavior is very strange to me.