Our team is attempting to utilize some of the analog ports for simple limit switches (as we are running low on digital ports) and were dismayed to find out that there is about a 1 second delay when using the standard Get_Analog_Value function provided in WPILib. We attempted to incorporate the adc.h and adc.c files from Kevin.org which reportedly is more efficient, but quickly found a conflict (Timer2). Due to there being no available source code for WPILib, it’s hard to know what timers are used by WPILib and their purpose. Any suggestions?
A 1 second delay does not sound normal.
If you are using these for limit switches, you will likely need a pullup resistor (something like 4.7k should work) to 5V, as I don’t believe the analog ports have any pullup within the RC like the digital ports do, since a switch works in an open-drain configuration rather than driving rail to rail.
That did it. You guessed correctly that we were simply grounding the signal line and did not include a pullup to +5V. The RC guide on IFI Robotics indicates that a 10K resistor is appropriate, so we used that. In PWM wire terms, we wired black(Gnd) to the switch NO (normally open contact), red(+5V) to the switch NC (normally closed), and white(signal) to the switch COM(common). This worked out fine for us, no delays. We now get values near 0 for a closed switch, and values near 1023 for an open switch (we map 0-511 to CLOSED, 512-1023 to OPEN just to be safe).
Thanks for the tip Brian!
I should note that while the above works, it is likely overkill. It was inspired based on the wiring of a potentiometer. However, the resistor (as wired above) doesn’t serve any purpose. It also requires the limit switch to be wired with 3 wires. The pull-up resistor SHOULD be wired from +5V to signal, to keep the signal line from floating when not pulled to ground. Then you wire the signal line and ground line to the limit switches, not the +5V. This should give you a zero (or very low at least) value when the limit switch is closed, and a non-zero (depending on the pullup resistor) value when the limit switch is open. I haven’t verified this yet, but it seems like the more logical approach.