Home Automation blog

Skip to Content

Multi-function buttons

Article image for Multi-function buttons

Posted by Chris

Date posted:

I've written an Arduino library for multi-functional buttons: single-press, double-press, and hold. View code at github.com

I've got it in use with these LED illuminated push buttons created by www.superhouse.tv to control my house's LED downlights:

  • Single Press: toggle on/off.
  • Double Press: toggle warm/cool white.
  • Hold: dim up/down.

Example code

#include <MultiFunctionButton.h>


MultiFunctionButton btn1;
MultiFunctionButton btn2;
MultiFunctionButton btn3;
MultiFunctionButton btn4;



void setup()
{
	// Configure each button
	// Pin number, Pulldown = 0 or Pullup = 1, Press callback, Double press callback, Hold callback methods
	btn1.configure(2, 0, onPress, onDblPress, onHold);
	btn2.configure(3, 0, onPress, onDblPress, onHold);
	btn3.configure(4, 0, onPress, onDblPress, onHold);
	btn4.configure(5, 0, onPress, onDblPress, NULL); // Callback methods are optional
}



void loop()
{
	// Check each buttons state
	btn1.check();
	btn2.check();
	btn3.check();
	btn4.check();
}




/**
 * Single press
 */
void onPress(int pin)
{
	Serial.print(pin);
	Serial.println(" pressed");
}



/**
 * Double press
 */
void onDblPress(int pin)
{
	Serial.print(pin);
	Serial.println(" double pressed");
}



/**
 * Button held
 */
void onHold(int pin)
{
	Serial.print(pin);
	Serial.println(" holding...");
}

View similar posts categorised as: Arduino