Handling Tactile Button Presses.

Usually a programmer's first program prints the string "Hello world" on the screen, while an embedded developer blinks an LED, maybe also adding a button to change the LED's state. It's the button I wanted to talk about: a button that toggles something is usually called a tactile button, and it's connected according to one of the circuits shown below.
Handling Tactile Button Presses.

The two upper circuits don't show pull-up resistors, but that doesn't mean there are none; they are simply inside the chip.

But a much more interesting question is how to poll the button so as not to miss presses. The first option is to poll the pin the button is connected to in a loop; this method will work while the program is small (LED on/off), but as the program grows, presses will start being missed. The second option is to configure a timer to overflow every 30 ms, with an interrupt on overflow in which the pins the buttons (or the button) are connected to are polled. Why a 30 ms polling interval? The thing is, a button is subject to contact bounce, and this time interval between polls lets you not notice it; let's look at this in more detail.
Handling Tactile Button Presses.

The arrows in the picture are the moments when interrupts occur. The upper picture shows the simplest scenario: thanks to the pause between polls, we skip over the bounce.

In the middle picture, the interrupt occurs during the bounce and it's as if the button wasn't pressed, but that doesn't bother us: on the next poll of the button we register the press.

In the lower picture, the interrupt again occurs during the bounce; we register the press and skip over the bounce.
Releasing the button follows the same scenario, and if the button bounces a lot, the time between polls can be increased.

So we're in the interrupt; let's look at the options for handling a press. The first: set a flag that the button is pressed, then check it in the main loop and clear it there. The second: create a variable and increment it in the interrupt while the button is pressed; when the button is released, check how much has accumulated, i.e. how long it was held. If at least one has accumulated, register a short press; if more than 10, register a long press. Let me explain once more the difference between the first and second methods: in the first, the system reacts at the moment the button is pressed; in the second, at the moment it's released, and which method to choose is up to the programmer, depending on the task.

You can also try to implement a double press, and here I see two options. The first: after the button is released, start a timer and wait a certain period for a second press; if it happens, register a double_click. The second method is to keep a log of presses: if the button was pressed and released, our variable will hold 0b00000010; if it was held for 3 polls, 0b00000111. This is easy to implement: in the interrupt, shift the variable's value one position to the left and write the current state into the last bit. Then, experimentally (quickly pressing the button twice), find several values of the variable for a double press, for example we got 0b00110011; write this value to double_click, and now, to detect a double press, compare the value of our variable with double_click. The first option definitely works; the second is just thinking out loud.