Edit: Looks like I need to type faster if I am going to post in the same threads as Al and Alan. But what I posted may still be usefull so I will leave it up.
Quote:
Originally Posted by Al Skierkiewicz
Yes, You guys are right. I have to stop answering these questions while I am working on interfaces here. Alan, can you add something about programming so that switch bounce doesn't confuse the readings?
|
I am not Alan but I should be able to help here any way. To debounce a switch in software what you want to do is ensure that the press that was detected was a true press, not just noise or bounce. The easiest way to do this is once a press is detected start a counter, if the counter reaaches a certain value (10ms is probably acceptable for FRC Applications) then acknowledge that the switch is pressed. If the switch is released before counter times out the reset the counter and ignore the bounce.
Psuedo Code
Create Counter- Initialize to Zero
Assuming Switch sampling occusr every ~1ms
If (Software_representation of switch = not-pressed)
If switch is not pressed
Reset Counter
Set Software representation of switch to not-pressed
else (switch is pressed)
increment counter
end if
if counter > 10
Set software representation of swtch to pressed
end if
Then you can reverse the switched/not-switched cases for then the butto is pressed if you need to detect the release as well.