Home Automation blog

Skip to Content

Multi-function buttons

Posted by Chris

Date posted:

I've written an Arduino library for multi-functional buttons: single-press, double-press, and press-hold events. 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.
  • Press-hold: dim up/down.

Example code

#include <MultiFunctionButton.h>

// Create instance of each button
MultiFunctionButton btn1;
MultiFunctionButton btn2;
MultiFunctionButton btn3;
MultiFunctionButton btn4;



void setup()
{
	// Configure each button. Call-backs are optional per button!
	btn1.configure(2, PULL_UP, onPress, onDblPress, onHold);
	btn2.configure(3, PULL_UP, NULL, onDblPress, onHold);
	btn3.configure(4, PULL_UP, onPress, NULL, onHold);
	btn4.configure(5, PULL_UP, onPress, onDblPress, NULL);

	Serial.begin(9600);
}



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



/**
* Press event call-back function
*
* @param int pin Reports which I/O pin the event occurred on
* @return void
*/
void onPress(int pin)
{
	Serial.print(pin);
	Serial.println(" pressed");
}



/**
* Double press event call-back function
*
* @param int pin Reports which I/O pin the event occurred on
* @return void
*/
void onDblPress(int pin)
{
	Serial.print(pin);
	Serial.println(" double pressed");
}



/**
* Press-hold event call-back function
*
* @param int pin Reports which I/O pin the event occurred on
* @return void
*/
void onHold(int pin)
{
	Serial.print(pin);
	Serial.println(" holding...");
}

View similar posts categorised as: Arduino