Tried capacitor and pullup resistor combo as shown here (though I skipped the LED and 1k resistor)
Tried pins other than pin 10 and tried different GND pin on Arduino
Tried different USB cables and different USB port on USB hub and PC
Ditched the limit switch (part# here) all together and just used the raw cables and opened and closed them (e.g. didn't touch the leads together, then touched the leads together)
You mention that you push the limit switch. Please identify this limit switch. Most limit switches are micro-switches. Do you have screw or soldered connections for the wires? Are you using a breadboard some where in the circuit?
Thank you so much for the replies everyone! Sorry for not posting an image of my actual setup (doh!)
Below are pictures and a video. I taped down the wires just now so it's easier to see everything in the picture.
To answer a few questions:
Limit switch I'm using is here .
This one has 3 pins, and the way it work is: S and V are connected while the switch is in its default state. (S and V are normally closed).
When the switch is pushed, S and V are disconnected and instead S and G pins become connected.
I am using jumper wires, not soldered connections.
No breadboard (I tried simplifying my circuit because this same issue was occurring in a more complex setup), pictures and video below.
lol I'm going to smack myself if it's that simple...is it though?... .. I can try it. But I thought the whole point of the INPUT_PULLUP in the code is to activate the Arduino's internal pullup resistor, which already connects it to the internal 5V supply line.
Haha aw man, so connecting the white pin to the 5V pin on the Arduino didn't do anything (using the exact same code).
Then I thought, what if I call it pinMode(SIGNAL_PIN, INPUT) instead of INPUT_PULLUP, and I connected pin 10 directly to the 5V line with a jumper cable (completely eliminated the limit switch from the circuit). It still has the same fluttering/flickering between HIGH and LOW.
#$#$#$#$#$#$*$ oh my lord it was not the hardware...
It was a dumb SEMICOLON in the wrong spot of the code AHHHHHHHHHHHHHHHHHHHH!!!!!
I wish the compiler would throw a flag but I guess it just let's that kind of stuff fly...so it seems like the "else if" line was completely ignored and it just kept following the instructions directly below it regardless of the condition of the limit switch....
!!!!!!
I really appreciate everyone's help in trying to diagnose the issue!
NO! You still have problems using that type of switch as a limit switch. There is no magical ZERO time for the switch to make it's move from one position to the other. During that very short interval, your code may be testing the switch several times and what will it see? NOTHING connected at either position of the switch.
Thank you Paul_KD7HB, I think that makes sense.
But what is the solution then?
My understanding is that these mechanical limit switches are fairly common in CNC/robotic type applications and that the limitation you mentioned is acceptable? Or maybe I misunderstood.
Either way, it seems like you have a better way, I would love to better understand it!