OneButton library help

I downloaded the OneButton library from Arduino OneButton Library and ran and modified the example. It does exactly what I need but I have to implement it on three buttons in the same sketch. I am not competent enough to figure out how to do it. My modified example code is below. In my real project, I will only need the "click" and "press" functions for the three buttons. I am recalling or setting values to/from an array into a variable. "Click" will recall and "press" will set new values.

#include "OneButton.h"

// Setup a new OneButton on pin A1.  
OneButton button(A1, false);

// setup code here, to run once:
void setup() {
  // enable the standard led on pin 13.
  pinMode(13, OUTPUT); // sets the digital pin as output
  pinMode(12, OUTPUT);// sets digital pin as output
  pinMode(11, OUTPUT);
  
  //link the click function to be called on a click event.
  button.attachClick(click);
  
  // link the doubleclick function to be called on a doubleclick event.   
  button.attachDoubleClick(doubleclick);
  
  //link the hold function to be called on a long hold event.
  button.attachPress(press);
  Serial.begin (9600);
} // setup
  
// main code here, to run repeatedly: 
void loop() {
  // keep watching the push button:
  button.tick();

  // You can implement other code in here or just wait a while 
  delay(10);
} // loop

void click(){
  Serial.println("CLICK");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(11,m);
}//click
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {
  Serial.println("DOUBLECLICK");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(13, m);
} // doubleclick
void press(){
  Serial.println("PRESS");
  static int m = LOW;
  m = !m;
  digitalWrite(12,m);
}//press

// End

I am guessing that I need to assign OneButton to button1, button2, and button3 with the appropriate pins.
OneButton button1(9 false);
OneButton button2(10 false);
OneButton button3(11 false);
I think also that I have to attach the function click() and press() to each of the buttons.
button1.attach click;
button1.attach press;
likewise for button2 and button3
I tried the above but got a host of errors when I tried to compile. Too many to list!
I am not sure if I have to use click1, click2 and click3 as well. These cases are not defined in the library.
There is also a function "button.tick()" and I don't know where to place it in my project and If need 3 variants as well. I am using a FSM with a function outside the void loop that reads buttons called ReadButtons(). Do I put the tick() function and all of the declares in that function or do I put it in the main void loop?

I am very new to Arduino's and c++ (about a month) so I am way over my head. My first "real" project has taken on epic proportions. Blinking an LED, rotating a motor, and writing to an LCD was fun but now I want to actually do something meaningful. It's been a real challenge. I can post my project code but this post was long enough. Sorry for being so long winded.

Thanks in advance,

Dave E

This compiles:

#include "OneButton.h"

// Setup a new OneButtons on pin A1, A2 and A3.  
OneButton buttonA(A1, false);
OneButton buttonB(A2, false);
OneButton buttonC(A3, false);

// setup code here, to run once:
void setup() {
  // enable the standard led on pin 13.
  pinMode(13, OUTPUT); // sets the digital pin as output
  pinMode(12, OUTPUT);// sets digital pin as output
  pinMode(11, OUTPUT);
  
  //link the click function to be called on a click event.
  buttonA.attachClick(clickA);
  buttonB.attachClick(clickB);
  buttonC.attachClick(clickC);
  
  // link the doubleclick function to be called on a doubleclick event.   
  buttonA.attachDoubleClick(doubleclickA);
  buttonB.attachDoubleClick(doubleclickB);
  buttonC.attachDoubleClick(doubleclickC);
  
  //link the hold function to be called on a long hold event.
  buttonA.attachPress(pressA);
  buttonB.attachPress(pressB);
  buttonC.attachPress(pressC);
  Serial.begin (9600);
} // setup
  
// main code here, to run repeatedly: 
void loop() {
  // keep watching the push buttonA:
  buttonA.tick();
  buttonB.tick();
  buttonC.tick();

  // You can implement other code in here or just wait a while 
  delay(10);
} // loop

void clickA(){
  Serial.println("CLICK A");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(11,m);
}//click
void clickB(){
  Serial.println("CLICK B");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(11,m);
}//click
void clickC(){
  Serial.println("CLICK C");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(11,m);
}//click

// this function will be called when the buttonA was pressed 2 times in a short timeframe.
void doubleclickA() {
  Serial.println("DOUBLECLICK A");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(13, m);
} // doubleclick
void doubleclickB() {
  Serial.println("DOUBLECLICK B");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(13, m);
} // doubleclick
void doubleclickC() {
  Serial.println("DOUBLECLICK C");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(13, m);
} // doubleclick

void pressA(){
  Serial.println("PRESS A");
  static int m = LOW;
  m = !m;
  digitalWrite(12,m);
}//press
void pressB(){
  Serial.println("PRESS B");
  static int m = LOW;
  m = !m;
  digitalWrite(12,m);
}//press
void pressC(){
  Serial.println("PRESS C");
  static int m = LOW;
  m = !m;
  digitalWrite(12,m);
}//press

Thanks dxw00d!

The sketch runs. It has some unexpected behavior that I need to investigate. Looks like some spillover to other button functions.
I guess I was not too far off with my assumptions.

Dave E

The multiple buttons work fine as verified with a whole bunch of Serial.print's. I believe the problems I was encountering were related to the "static int m" values being retained from one button to another. I also changed the inputs to digital pins because I thought the analog pins may have been experiencing some crosstalk. Don't know if that is true or not.

Thanks again, I'm one step closer in my overall project.

Dave E

Hey guys, does anyone here know how one can change the delay between the actual click and response?

Would appreciate your help!

Thanks

Anyone?

Hey guys, does anyone here know how one can change the delay between the actual click and response?

Write the code properly. Let's see what you have, along with a better explanation of the problem.

You don't have delay()s in your code, do you? I'll bet you do.

You just lost whatever it was you placed on the table PaulS 8)

Here's the code.
What I'm referring to is the delay that is built in the library itself, and is visible: the time it takes from the actual event (click/double/press) to the function.

/*
 This is a sample sketch to show how to use the OneButtonLibrary
 to detect click events on a button. 
 The library internals are explained at
 http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
  
 

#include "OneButton.h"

// Setup a new OneButton on pin A1.  
OneButton realTimebutton(A1, true);


// setup code here, to run once:
void setup() {
  Serial.begin (9600);
  // enable the standard led on pin 13.
  pinMode(13, OUTPUT);      // sets the digital pin as output
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  // link the doubleclick function to be called on a doubleclick event.   
  realTimebutton.attachClick(Click);
  realTimebutton.attachDoubleClick(doubleclick);
  realTimebutton.attachPress(Press);
} // setup
  

// main code here, to run repeatedly: 
void loop() {
  // keep watching the push button:
  realTimebutton.tick();
 } // loop


// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {
  Serial.println ("realTime doubleClick");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(13, m);
} // doubleclick

void Click() {
   Serial.println ("realTime Click");
   static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(12, m);
}

void Press() {
  Serial.println ("realTime Press"); 
 static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(11, m); 
}
// End

The OneButton class has default values for how long the switch has to be held down to detect a click or a press.

You can change those times, using the setClickTicks() and setPressTicks() methods.

You don't override the defaults, so the click time is 0.6 seconds and the press time is 1.0 seconds.

Knowing what library you were using, where you got it, and how you were using it were important to know in order to tell why you were having the problem you were having. The subject line defines the library, but only when I see the code can I actually confirms that.

Thank you very much PaulS !
So actually if in my setup () I'll write something like setClickTicks(400) the click time is 0.4 Seconds?

So actually if in my setup () I'll write something like setClickTicks(400) the click time is 0.4 Seconds?

Yes. The value specified is the time in milliseconds. The default values of 600 and 1000 are too long, and too close, in my opinion. I'd use values like 50 and 250, if I was using the library. A 0.05 second hold down time to be recognized as a click and a hold down time of 0.25 seconds to be recognized as a press seem more reasonable to me.

Think about how long you have to hold a key on a keyboard down for it to be recognized. 6/10 of a second is an eternity.

PaulS:

The default values of 600 and 1000 are too long, and too close, in my opinion. I'd use values like 50 and 250, if I was using the library. A 0.05 second hold down time to be recognized as a click and a hold down time of 0.25 seconds to be recognized as a press seem more reasonable to me.

Think about how long you have to hold a key on a keyboard down for it to be recognized. 6/10 of a second is an eternity.

My Thoughts exactly.
Thanks again :slight_smile:

Hi
I´ve tested dxw00d code, and work nice, but in my case I would like to had one IF and can´t figure it out how! :!

Like when we have 3 or 4 buttons and :
1 ON
1 PLUS
1 MINUS
1 OFF

The buttons always respond before (in my case) to force to press the ON button,
Where can we write the code for? in he void click or press.. or in loop??

many tks

Like when we have 3 or 4 buttons and :
1 ON
1 PLUS
1 MINUS
1 OFF

How would you do that with three switches? Looks to me like you need 4.

but in my case I would like to had one IF

For what?

and can´t figure it out how!

Understandable.

Where can we write the code for? in he void click or press.. or in loop??

Yes, it should go in one of those places.

You'll need to work on your description of what you want to do, before we can help. And, post some code.

tks PaulS

my code is from dxw00d, from second post, and i would like to have 4 press button or doubleclick action response (with action D)
but imagine a fine state machine, when press..

pressA - it will Serial.println("PRESS A"); and start the scheme,

but I would like to know where can I write the IF ... so when pressB or clickB, will be ONLY available after had pressA or clcikA

understand?

BR
TY PaulS

KJMM, if I understand correctly you want a "B" button to work only if "A" button was pressed prior to it?
In that case you could use a Boolean.

---Edited---

something along the lines of:
boolean AClickedOnce = false;

and than -

void BClick() {

if (AClickedOnce == false )
{
do your action here
}
else
{
don't do anything
}

HI Soffer and PaulS

My code is:

/*
 This is a sample sketch to show how to use the OneButtonLibrary
 to detect double-click events on a button. 
 The library internals are explained at
 http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
 http://arduino.cc/forum/index.php/topic,110286.0.html
 Setup a test circuit:
 * Connect a pushbutton to pin A1 (ButtonPin) and ground.
 * The pin 13 (StatusPin) is used for output attach a led and resistor to ground
 or see the built-in led on the standard arduino board.
 
 The Sketch shows how to setup the library and bind a special function to the doubleclick event.
 In the loop function the button.tick function has to be called as often as you like.
 */

// 03.03.2011 created by Matthias Hertel
// 01.12.2011 extension changed to work with the Arduino 1.0 environment

#include "OneButton.h"


// Setup a new OneButton on pin A1.  
OneButton buttonA(A1, false);
OneButton buttonB(A3, false);


// setup code here, to run once:
void setup() {
  pinMode(2, OUTPUT);      // sets the digital pin as output LED
  pinMode(3, OUTPUT);      // sets the digital pin as output LED
  pinMode(4, OUTPUT);      // sets the digital pin as output LED
  pinMode(6, OUTPUT);      // sets the digital pin as output FAN

  //link the click function to be called on a click event.
  buttonA.attachClick(clickA);
  buttonB.attachClick(clickB);

  // link the doubleclick function to be called on a doubleclick event.   
  buttonA.attachDoubleClick(doubleclickA);
  buttonB.attachDoubleClick(doubleclickB);

  //link the hold function to be called on a long hold event.
  buttonA.attachPress(pressA);
  buttonB.attachPress(pressB);


// as Soffer ?
  boolean clickB = false;
  boolean doubleclickB = false;
  boolean pressB = false;
    
    
  Serial.begin (9600);
  Serial.println("Program startet");
  
  


  
} // setup


// main code here, to run repeatedly: 
void loop() {

  Click();
  
  delay(10);
} // loop



// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CLICK 
void clickA(){
  Serial.println("CLICK A");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(2,m);
}//click


void clickB(){
  Serial.println("CLICK B");
  static int m = LOW;//reverse the led
  m = !m;
  digitalWrite(2,m);
}//click


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ DOUBLECLICK 
// this function will be called when the buttonA was pressed 2 times in a short timeframe.

void doubleclickA() {
  Serial.println("DOUBLECLICK A");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(3, m);
} // doubleclick

void doubleclickB() {
  Serial.println("DOUBLECLICK B");
  static int m = LOW;
  // reverse the LED 
  m = !m;
  digitalWrite(3, m);
} // doubleclick


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PRESS
void pressA(){
  Serial.println("PRESS A");
  static int m = LOW;
  m = !m;
  digitalWrite(4,m);
}//press
void pressB(){
  Serial.println("PRESS B");
  static int m = LOW;
  m = !m;
  digitalWrite(4,m);
}//press


// +++++++++++++++++++++++++++ http://arduino.cc/forum/index.php/topic,110286.15.html
void Click() {
  if (clickB == false )
  {
  Serial.println("PRESS A1");
  delay(50);
  buttonA.tick();
  }
  else
  {
  Serial.println("PRESS A3");   
  delay(50); 
  buttonB.tick();

  }  }

but I not getting this working, i think it as a simple solution, but not for me :slight_smile:

TKS

That code does something. You want it to do something. Clearly those two somethings are not the same thing. What either one is, though, is a mystery.

LOL

i laugh :slight_smile: really!!

maybe the problem is to trying rest a little and try harder after :slight_smile: meanwhile any suggestion?
I would be very appreciated

will start posting more, not alone is better :wink:

Best regards

KJMM - take a moment - try to tell us what it is you are trying to do because right now the main
problem is figuring this out. Your questions aren't clear at all.

Also - do a search here in the forum for OneButton lib and you'll see other sketches.
Look at those and use them to figure out what they do and how.

If the only thing you seek is how to use an "If" statement, check the learning section here at the site.