LED and button on same digital pin

I want to have a momentary button and an LED on the same digital pin. What I want to be able to do is detect a press of the button and yet also periodically flash the LED for a short time.

The idea is that the pin is configured to be an input (with an internal pullup) most of the time. With the button open the LED will be off and the pin will read high. If the button is pressed the pin will read low (and the LED will turn on). Periodically, for a short period of time, the pin will be configured as an output and set to low. Then the LED will be on. If the button is pressed and released during this short time it would be missed.

Will it work? Is there a better way to do this?

You have to guard against putting a high level on the pin in output mode, then the button would short it and draw too much current. If you follow the right software sequence, you should be alright. When you want to blink, first set the pin output level LOW, then set it to output mode. To release, set the pin mode back to input. It should be okay.

Thank you, I hadn't seen that possibility. Would it make sense to put a small resistor in series with the button as a safeguard?