Hi guys, I ám really new at this stuff (reading arduino for dummies and all… ).
I would like to control some lights (yeah, nothing new here!!!) but I´m stuck.
At first I learned how to turn LEDs on/off
Getting more “complicated” I could use a relay to active eletrical lights.
Then looking around I found that I could use a single push-button to do more than on/off, by making it understand the “double click” and “hold”.
Since all that used just one button, I tried to make the same with 2 or 3 buttons and that´s where I am stuck.
Looking futher I could find a multi-button boucing script that would help me read all buttons that I want, but by using it, I will lose the “4-way” function!!!
In other words, I would like to light up a room with a push-button, but this button need to light on/off other rooms if I long press it (for example). And the other rooms could make the same (or not!) with their one push-buttons.
The 4-way button I find out, and which is working like a charm is:
/* 4-Way Button: Click, Double-Click, Press+Hold, and Press+Long-Hold Test Sketch
By Jeff Saltzman
Oct. 13, 2009
To keep a physical interface as simple as possible, this sketch demonstrates generating four output events from a single push-button.
1) Click: rapid press and release
2) Double-Click: two clicks in quick succession
3) Press and Hold: holding the button down
4) Long Press and Hold: holding the button for a long time
*/
#define buttonPinA0 A0 // analog input pin to use as a digital input
#define ledPin13 13 // digital output pin for LED 1
#define ledPin8 8 // digital output pin for LED 2
// LED variables
boolean ledVal13 = false; // state of LED 1
boolean ledVal8 = true; // state of LED 2
//=================================================
void setup() {
// Set button input pin
pinMode(buttonPinA0, INPUT_PULLUP);
pinMode(ledPin13, OUTPUT);
digitalWrite(ledPin13, ledVal13);
pinMode(ledPin8, OUTPUT);
digitalWrite(ledPin8, ledVal8);
}
void loop() {
// Get button event and act accordingly
int b = checkButton();
if (b == 1) clickEvent();
if (b == 2) doubleClickEvent();
if (b == 3) holdEvent();
/* MANTER APERTADO POR MUITO TEMPO
if (b == 4) longHoldEvent();
*/
}
//=================================================
// Events to trigger
void clickEvent() {
ledVal13 = !ledVal13;
digitalWrite(ledPin13, ledVal13);
}
void doubleClickEvent() {
ledVal8 = !ledVal8;
digitalWrite(ledPin8, ledVal8);
}
void holdEvent() {
ledVal13 = !ledVal13;
ledVal8 = !ledVal13;
digitalWrite(ledPin13, ledVal13);
digitalWrite(ledPin8, ledVal8);
}
/* MANTER APERTADO POR MUITO TEMPO
void longHoldEvent() {
ledVal13 = !ledVal13;
ledVal8 = ledVal13;
digitalWrite(ledPin13, ledVal13);
digitalWrite(ledPin8, ledVal8);
}
*/
//----------------------------------------------------------------------------
//=================================================
// MULTI-CLICK: One Button, Multiple Events
// Button timing variables
int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250; // max ms between clicks for a double click event
int holdTime = 1000; // ms hold period: how long to wait for press+hold event
int longHoldTime = 3000; // ms long hold period: how long to wait for press+hold event
// Button variables
boolean buttonVal = HIGH; // value read from button
boolean buttonLast = HIGH; // buffered value of the button's previous state
boolean DCwaiting = false; // whether we're waiting for a double click (down)
boolean DConUp = false; // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true; // whether it's OK to do a single click
long downTime = -1; // time the button was pressed down
long upTime = -1; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false; // when held, whether to wait for the up event
boolean holdEventPast = false; // whether or not the hold event happened already
boolean longHoldEventPast = false;// whether or not the long hold event happened already
int checkButton() {
int event = 0;
buttonVal = digitalRead(buttonPinA0);
// Button pressed down
if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
{
downTime = millis();
ignoreUp = false;
waitForUp = false;
singleOK = true;
holdEventPast = false;
longHoldEventPast = false;
if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true) DConUp = true;
else DConUp = false;
DCwaiting = false;
}
// Button released
else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
{
if (not ignoreUp)
{
upTime = millis();
if (DConUp == false) DCwaiting = true;
else
{
event = 2;
DConUp = false;
DCwaiting = false;
singleOK = false;
}
}
}
// Test for normal click event: DCgap expired
if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
{
event = 1;
DCwaiting = false;
}
// Test for hold
if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
// Trigger "normal" hold
if (not holdEventPast)
{
event = 3;
waitForUp = true;
ignoreUp = true;
DConUp = false;
DCwaiting = false;
//downTime = millis();
holdEventPast = true;
}
/* MANTER APERTADO POR MUITO TEMPO
// Trigger "long" hold
if ((millis() - downTime) >= longHoldTime)
{
if (not longHoldEventPast)
{
event = 4;
longHoldEventPast = true;
}
}
*/
}
buttonLast = buttonVal;
return event;
}