A button is a digital input. This means that it can have two states, you read these as either HIGH
(5V) or LOW
(0V).
Materials
- 1 Arduino Uno board
- 1 Education shield
- 1 Button
- 1 10k ohm resistor
- 3 Jumper wire
Instructions
- Attach the shield onto the top of the Arduino board.
- Connect the button across the breadboard gap.
- Connect one pin of the button to digital pin 9.
- Connect the same pin to a 10k ohm resistor, and the resistor to GND.
- Connect the other end of the button to 5V.
- Connect the board to the computer and upload example ‘Button’.
Code
You should now open the serial monitor and follow the printed instructions. Each time you execute an instruction the serial monitor should print ‘1’.
Commands
Button( digitalPin )
/Button( digitalPin, pressedState)
: creates a button object.- Parameters
- (
int
)digitalPin
: The digital pin to which the button is connected. -
(
boolean
)pressedState
: can beHIGH
orLOW
.HIGH
means that when the button is pressed it will be read asHIGH
, when released it will be read asLOW
.LOW
means the other way around. When pressed it will be read asLOW
and when released it will be read asHIGH
. Default isHIGH
.
- (
- Parameters
begin()
: Initializes the component. Must be called insetup()
pressed( timeout )
: checks if the button is pressed.- Parameters
- (
int
)timeout
: milliseconds within which the button needs to be pressed. Default is 0.
- (
- Return
-
(
boolean
): returns 1 if pressed within time, otherwise 0. Iftimeout
is not used the program pauses here until the button is pressed.
-
- Parameters
released( timeout )
: checks if the button is released.- Parameters
- (
int
)timeout
: milliseconds within which the button needs to be released. Default is 0.
- (
- Return
-
(
boolean
): returns 1 if released within time, otherwise 0. Iftimeout
is not used, the program pauses here until the button is released.
-
- Parameters
doublePressed( timeout, tolerance )
: checks if the button is double pressed.- Parameters
- (
int
)timeout
: milliseconds within which the button needs to be double pressed. - (
int
)tolerance
: milliseconds before the button should be pressed a second time. Default is 500.
- (
- Return
- (
boolean
) Returns 1 if double pressed within time, otherwise 0. Iftimeout
is not used, the program pauses here until the button is double pressed.
- (
- Parameters
Troubleshooting
- First of all make sure that the connections between button, cable and board are correct.
- Make sure that the program is reading from the dame digital pin as you have connected it to.