I am working on a project that keeps track of the number of times a contactor switches back and forth. To track the state the contactor is in, limit switches are used. Originally I was using the Arduinos internal pull-ups with digital pins and had the limit switches connected as pictured below.
The problem came when I noticed some of the pins were false triggering...contactor two would switch but also would display contactor 1 and 4 switched, even though they didn't.
I thought the internal pull-ups were experiencing some noise, so I tried the same setup pictured with external 10K pull up resistors, but encountered the same problem. My code is attached below and I am struggling to find a solution.
I guess my main question is, does it seem possible noise is still the problem, or does my code need improvement?
Yes it needs improvement. First of all, eliminate the 4x repetition of code by using arrays. Your variable naming is strange - you call a button a "cycle" (from my understanding of what you said, it's not even a button). I suggest changing:
int cycleState1 = 0; //initial state the button is in
to
int switchState1 = 0; //initial state the button is in
You can lower the resistance of the pullups to make the inputs less susceptible to noise. 1K is probably the practical minimum resistance. A 0.1uF (or maybe higher) cap across the switch can also help to filter noise away from the switch input (AKA hardware debounce).
Shizzle53:
Product is in test phase and I do not own the rights to it.
Generally speaking, this is an open-source community where people try to help you -- for free. If that model is not suitable, you might consider paying someone to help you -- under NDA.