Lesson 5

Lesson 5: Using Push Button Events

In this lesson you will learn how to get the state of the user button by using the ApeirosIoExpander class library. You will learn how to use button press events to trigger custom behaviors.

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products".

Introduction

Apeiros provides one user push button that can be used to trigger specific sections of code based upon the state (pressed or unpressed) of the button.

Apeiros Class Library Functions

The ApeirosIoExpander class library contains one function for getting the current state of the user button, BTN-1 .

bool getUserButtonState();

Recall that we need to first declare an instance of the ApeirosIoExpander class library and then call the begin() function from within the setup() function. Calling the begin() function will properly initialize the library.

// Declare Apeiros class instances.//
/***********************************/
ApeirosIoExpander io;
/***********************************/

void setup() {
  // put your setup code here, to run once:

  // Begin ApeirosIoExpander.
  io.begin();
  
}

Now, suppose that we want to toggle the state of LED2 whenever the user button is pressed. We need to make sure that all of the LEDs are initially turned off from within the setup() function.

// Declare Apeiros class instances.//
/***********************************/
ApeirosIoExpander io;
/***********************************/

void setup() {
  // put your setup code here, to run once:

  // Begin ApeirosIoExpander.
  io.begin();

  // Turn off all LEDs.
  io.setAllLEDsState(0);
  
}

Next we need to add code to the main loop and poll for when the user button is pressed. If the user button is pressed (1), then we want to toggle the state of LED2.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Button pressed = 1 and not pressed = 0.
  if (io.getUserButtonState()) // Check for a button press.
  {
    // Debounce for 100ms.
    delay(100);

    // Check to make sure button remains pressed.
    if (io.getUserButtonState())
    {
      // Toggle state of LED2.
      io.toggleLED2State();
    }
  }
  
  delay(50);  // Delay for a specified amount of time in milliseconds
}

You may have noticed that the main loop performs two checks for the user button to be pressed. Mechanical momentary push buttons typically produce noisy signals. This is because a momentary push button relies upon a set of mechanical contacts. During a press event the contacts can intermittently make and break the electrical connection and this results in a noisy signal. The main loop performs an initial check for the user button to be pressed, delays 100ms and then performs a second check. If the button is found to be in the press state during both checks, then the state of LED2 is toggled. Check out this great tutorial if you want to learn more about switches.

EXPERIMENT


 

What happens when you press the user button and hold it in the pressed state for an extended period of time? Why do you think continuously pressing the button produces this result? HINT: You need to think about how the main loop is checking for the button press event.

Can you think of a way to improve how we detect a button press event? HINT: Take a closer look at the advanced push button example from within the Arduino IDE: Files -> Examples -> Apeiros -> Apeiros_Push_Button_Advanced. You can also checkout the following tutorial.