Use a schmitt trigger.
It's probably as simple as a resistor between the output of the LM339 and it's non-inverting input.
https://nl.wikipedia.org/wiki/Schmitt-trigger (https://nl.wikipedia.org/wiki/Schmitt-trigger)
Or, better: do it in software.
contact bounce is very common for mechanical switches, and usually it's taken care of by reading the input at regular intervals (for example between 1 to 10ms) and only accept an input change when multiple consecutive logic levels have the same value.
"State" is a uint8_t counter variable.
SWITCH_HIGH and SWITCH_LOW are defined constants. when "State" becomes equal to one of these, then the state of the switch has changed.
void TThreadSwitch::Debounce( void) {
// Debounce for the switch.
if( SWITCH_PIN & SWITCH_BIT) {
if( State < SWITCH_HIGH) {
++State;
}
}
else {
if( State > SWITCH_LOW) {
--State;
}
}
}