Threshold detector with hysteresis