Help with creating a delay timer for a limit switch in C++

I have created a limit switch class, which works successfully except that if the limit switch is not just tapped once and has something pressing on it for a few seconds, it will recognize itself as being pressed repeatedly. I want to implement a timer so when it is pressed by something, it will register as just being pressed once even if it is held down for a few seconds. Hopefully this makes sense. Thank you.

You need to debounce the switch - more info here:

This solution implements a timer. We’ve also had acceptable results simply by comparing the current and previous state - and only flagging the press (or release) when the state differs from the previous state.

1 Like

I don’t think that’s the same problem the OP is talking about - I think the OP is looking for “latching” code that returns true only during the program iteration in which the limit switch changes from unpressed to pressed. Of course, good debounce code is essential for this as well.

Are you trying to detect when the switch is first pushed down, or the case where it is pushed and continues to be down?

When the switch is first pushed down.