A button is a component that give us information about if it’s pressed or not. This means that it can have two states, HIGH (5 V or 3.3 V depending of the board) or LOW (0 V). When we connect the button to a digital input in our board, we can read these values as 0 or 1.
- 1 x control board
- 1 x Education shield
- 1 x push button module
- 1 x module cable
- Attach the shield onto the top of the board.
- Connect the push button module to D6 using the module connector wire.
- Connect the board to the computer and upload example ‘Button’.
Find the code in File>Example>EducationShield>Help>Button
You should now open the serial monitor and follow the printed instructions. Each time you execute an instruction the serial monitor should print ‘1’.
- Button( digitalPin ) / Button( digitalPin, pressedState): creates a button object.
- begin(): Initializes the component. Must be called in setup()
- pressed( timeout ): checks if the button is pressed.
- (int) timeout: milliseconds within which the button needs to be pressed. Default is 0.
- released( timeout ): checks if the button is released.
- (int) timeout: milliseconds within which the button needs to be released. Default is 0.
- doublePressed( timeout, tolerance ): checks if the button is double pressed.
- (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.
- (boolean) Returns 1 if double pressed within time, otherwise 0. If timeout is not used, the program pauses here until the button is double pressed.
- First of all make sure that the connection between button, wire and board is correct.
- Make sure that the program is reading from the same digital pin as you have connected it to.