TinkerKit Tutorial: Basics: 09 – Button States

Basic Lessons:

  1. Introduction
  2. Project Setup
  3. Arduino Software
  4. Starting with the code
  5. Sensors and Actuators
  6. Button & IF statement
  7. Serial Communication
  8. Connect Sensors and Actuators
  9. Button States
  10. FOR Loop
  11. Traffic Light


The TKButton class, comes with a different range of methods that detect different states of the button. They are used to see when the button is pressed, released, held and toggled.

There’s an example that comes with the library, called ButtonStates. Let’s open it to see how it works:

The sketch is rather simple, basically we have a set of methods that are true when an event takes place. To verify them we use the “if” operator; let’s say for example that we want to check when the button is pressed, we write:

if (btn.pressed()) {
    //do something

This will execute the instructions between the brackets, only at the very moment when the button is pressed (or released).

An interesting method is getSwitch(), that changes between 0 and 1 every time that the button is released. In this way it’s super easy to use it as a switch! Let’s see an example: we want to turn on and off an LED using the button as a switch.

if (btn.getSwitch() == 0) 

The last method that we see in this tutorial is held(); it becomes true only when the button is being pressed for more than 500 milliseconds (half second). You can easily turn on an LED just when a button is held using a code that looks like this:

if (btn.held())

Leave a Reply

Your email address will not be published. Required fields are marked *