Arduino Uno
Breadboard
1x LED
2x 220 Ω resistor
1x push button
Wires
To use a button it is recommend to use a library called EasyButton. This library simplifies the process of using a button.
Install the library EasyButton. Install the lastest version (higher number).
Just add the code below on the top of the file.
Create as many object as the number of button needed.
A new fucntion must be created to be executed when the button is pressed. It can have any name.
Initialize the button by calling the method begin() within the setup() function. Also define which function should be called, when the button is pressed.
To assure the button state is updated, the method read(), must be called in the loop() function.
The example below how to turn a led on and off by pressing a button.
It is also possible to use other 2 attach functions, onPressedFor and onSequence, which provide a different behaviour when the button is clicked.
The onPressedFor function detect when a button has been pressed for a given time.
The onSequence function will call a function if the button is pressed a number of times in a period of time.