Lesson 8: Programming Single- and Double-Press Buttons

 

Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson, you'll learn how to create a button with two separate actions that you can execute depending on whether you do a single press or a double-press (i.e. double-click).

Here's a peek at how the user interface will look:

Press once or twice to get different responses!

== Upgrade to the latest firmware

Annikken Andee for iOS - V2.3 (16 Mar 2016)

Annikken Andee U - V1.4 (16 Mar 2016) ==


This new upgrading will improve the button widget, now you can get you Arduino projects to respond faster when individual button is pressed.

See the difference in responsiveness with this demo Arduino sketch

Download example Here

DSC01610.JPG


Contents

Top of the Code

Always include these libraries. Annikken Andee needs them to work with the Arduino!

#include <SPI.h>
#include <Andee.h>

// We'll create a display and a button to show you how to
// program a button to do something
AndeeHelper displaybox;
AndeeHelper button;

setup()

The setup() function is meant to tell Arduino what to do only when it starts up.

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
}

setInitialData()

This is the function meant to define the types and the appearance of all the objects on your smartphone

void setInitialData()
{
  // Let's draw a display box!
  displaybox.setId(0);  // Each object must have a unique ID number
  displaybox.setType(DATA_OUT);  // This defines your object as a display box
  displaybox.setLocation(0, 0, FULL); 
  displaybox.setTitle("Single and Double Press Buttons"); 
  displaybox.setData("Waiting for button press"); 
  
  button.setId(1); // Don't forget to assign a unique ID number
  button.setType(BUTTON_IN); // Defines object as a button
  button.setLocation(1,0,FULL);
  button.setTitle("Press me!");
  button.requireAck(false); // You need this line to allow for multiple button presses
  // You can't use setData() and setUnit() for buttons.
}

loop()

Arduino will run instructions here repeatedly until you power it off.

void loop()
{
  // Get the number of button presses
  int numPresses = button.getButtonPressCount();
  
  // Here's how you code with a multi-press button:
  if( numPresses == 1 )
  {
    // Add single press action here!
    displaybox.setData("Single Press");
    displaybox.update();
    delay(500);
    displaybox.setData("Waiting for button press");
    displaybox.update();
  }
  if( numPresses == 2 )
  {
    // Add double press action here!
    displaybox.setData("Double Press");
    displaybox.update();
    delay(500);
    displaybox.setData("Waiting for button press");
    displaybox.update();    
  }
  // You can potentially code actions for triple press and quadruple press.
  
  displaybox.update(); // Don't forget to call update() or else your objects won't show up
  button.update();
  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus