Adding one more button with the same functions

Hey i have been trying to add one more button to this code. I have tried by creating different voids to add them to a loop with out it working. Anyone out there able to help me add another button i this code with the same functions?


 
#include "OneButton.h"                              //we need the OneButton library
 
 
OneButton button(A1, true);                         //attach a button on pin A1 to the library
 
 
 
void setup() {
 
  pinMode(13, OUTPUT);                              // sets the digital pin as output
  pinMode(12, OUTPUT);                              // sets the digital pin as output
  pinMode(11, OUTPUT);                              // sets the digital pin as output
  pinMode(14, OUTPUT);                              // sets the digital pin as output
  pinMode(14, OUTPUT);                              // sets the digital pin as output
  pinMode(16, OUTPUT);                              // sets the digital pin as output
  
    
  button.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  button.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  button.attachLongPressStop(longclick);            // link the function to be called on a longpress event.
} 
  
 
 
void pumpe1() {
  
  button.tick();                                    // check the status of the button
 
 
  delay(10);                                        // a short wait between checking the button
} // loop
 
 
 
void doubleclick() {                                // what happens when button is double-clicked
 digitalWrite(11,HIGH);                             // light the green LED
 delay(1000);                                       // wait one second
 digitalWrite(11,LOW);                              // turn off green LED
} 
 
void singleclick(){                                 // what happens when the button is clicked
  digitalWrite(11,HIGH);                            // light the red LED
 delay(1000);                                       // wait one second
 digitalWrite(11,LOW);                              // turn off the gren led
}
 
void longclick(){                                   // what happens when buton is long-pressed
  digitalWrite(11,HIGH);                            // light the blue LED
 delay(1000);                                       // wait one second
 digitalWrite(11,LOW);                              // turn off the blue LED
}


void loop() {
  pumpe1();
}

You still only have one OneButton object in your code.

You'll need a few more of these and give them appropriate & unique names. Alternatively store them into an array, but let's not go there for a bit (will be beneficial though).
Then work with each unique button object in your code, effectively replicating everything you're now doing with "button." for all your buttons. And that's where working with arrays comes in nicely to reduce redundancy in your code, but try it without arrays first.

Look at @Idahowalker below!

You mean like this?

OneButton button0(A0, true);                         //attach a button on pin A0 to the library

OneButton button1(A1, true);                         //attach a button on pin A1 to the library

OneButton button2(A2, true);                         //attach a button on pin A2 to the library

OneButton button3(A3, true);                         //attach a button on pin A3 to the library

?

Sorry for late reply still waiting for some components to arrive and have been busy at work. Do u have any tips how to integrate it into the current code?

I do not know what you are asking with your question of,

.
Could you elaborate? What is meant by "it"?

Given the repetitive nature of the code (identical statements for button0, button1 etc. etc.) working with arrays would make more sense as I remarked earlier. But I'd suggest to try the brute force repetitive approach first so you get a basic understanding of what you're doing.

1 Like

I am sorry but i still do not totally understand how i will set the code up to work. When i try to link the different buttons to the different click functions i get an error code that says exit status 1 'doubleclick' was not declared in this scope. I am also still uncertain about how i should arrange the Void pump1 with the button ticks. And also the loop function at the end.

#include "OneButton.h"                              //we need the OneButton library
 
 
OneButton button0(A0, true);                         //attach a button on pin A0 to the library
OneButton button1(A1, true);                         //attach a button on pin A1 to the library

  void setup() {
 
  pinMode(13, OUTPUT);                              // sets the digital pin as output
  pinMode(12, OUTPUT);                              // sets the digital pin as output
  pinMode(11, OUTPUT);                              // sets the digital pin as output
  pinMode(14, OUTPUT);                              // sets the digital pin as output
  pinMode(14, OUTPUT);                              // sets the digital pin as output
  pinMode(16, OUTPUT);                              // sets the digital pin as output
  
    
  button0.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  button0.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  button0.attachLongPressStop(longclick);            // link the function to be called on a longpress event.
  button1.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  button1.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  button1.attachLongPressStop(longclick);            // link the function to be called on a longpress event.

//Etc....
} 
  void pumpe1() {
  
  button0.tick();                                    // check the status of the button
  button1.tick();                                    // check the status of the button
 
//Etc.


 
 
  delay(10);                                        // a short wait between checking the button
} // loop

 

This is the guy who wrote the thing you are trying to use, mathertel/OneButton: An Arduino library for using a single button for multiple purpose input. (github.com). Ask her/him how it works and why its not working.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.