I Need help with a project I am creating with the Onebutton library

I am trying to make a menu with the Onebutton library, I have a button that is connected to 2 pins, but I am trying to implement the going back function. If you keep pressing the button, it recognizes it as 2 long presses. What should I do?

Here is my code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include "OneButton.h"

OneButton Button1(0, true);
OneButton Button2(12, true);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     -1
#define SCREEN_ADDRESS 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int buttonpress = 0;

int test = 1;

int  testok = 0;

int stop3 = 0;

int stop = 0;

int buttonState = 0;

int button = 13;

int temp = 0;

int lol = 0;

int bro = 1;

const char *ssid = "Dice D1";
const char *password = "10203040";

void setup() {

 Serial.begin(9600);
  
Button1.attachClick(click1);
Button1.attachDuringLongPress(click3);

Button2.attachClick(click10);
Button2.attachDuringLongPress(click13);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.clearDisplay();

Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);

pinMode(button, INPUT);

}

void loop() {
Button1.tick();
delay(10);
Button2.tick();
delay(10);

temp = digitalRead(button);

if(temp == LOW && test == 6 && bro == 1){
  lol = 1;
} 
if(temp == LOW  && test == 6){
  lol = 2;
} 

if (test == 1){
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(21,28);
  display.println("Press Button 1");  
  display.setCursor(25,37);
  display.println(test);  
  display.display();
  delay(0);
  display.clearDisplay();
  }
if (test == 2){
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(21,28);
  display.println("Press Button 2");  
  display.setCursor(25,37);
  display.println(test); 
  display.display();
  delay(0);
  display.clearDisplay();
}
if (test == 3){
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(25,28);
  display.println("Test finished");
  display.setCursor(25,37);
  display.println(test);   
  display.display();
  delay(0);
  display.clearDisplay();
}        
  if(test == 4){
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(43,28);
  display.println("Wifi On");  
  display.setCursor(43,37);
  display.println(buttonpress); 
  display.setCursor(43,46);
  display.println(lol); 
  display.display();
  delay(0);
  display.clearDisplay();
  }             
  if(test == 5 ){
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(43,28);
  display.println("Wifi Off");  
  display.setCursor(43,37);
  display.println(buttonpress); 
  display.setCursor(43,46);
  display.println(lol);  
  display.display();
  delay(0);
  display.clearDisplay();
  }
  if(test == 6){
     display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(43,28);
  display.println("Ir Remote"); 
  display.setCursor(43,37);
  display.println(buttonpress);  
  display.setCursor(43,46);
  display.println(lol); 
  display.display();
  delay(0);
  display.clearDisplay();
  }                                                                                                                                                                                                                                                                                                                                          
}

void click1() {
  if(test >= 3){
 test = test + 1;
  }  
if(test == 7){
   test = 4;  
  }
if (test == 2 && stop3 == 1){
  test = test + 1;
  }    
}



void click3() { 
  if(test == 5 && lol == 2){
  test = 4;
  buttonpress = 1;   
  lol == 1;
  }
  if(test == 6 && lol == 1){
  test = 5;
  bro = 1;
}
}

void click10(){
  if(test <= 3 && stop3 == 0){
   delay(100);
   test = test + 1;
   stop3 = 1;
  }
}

void click13(){

}

Which Arduino are you using? On an UNO/NANO/MEGA you should keep pin 0 and 1 for Serial outputs - which are necessary to debug a program.

which button exactly?

change button on pin 0 to another button and use Serial.print to follow your program flow and check on Serial Monitor what is causing the error.

by the way, the author has a nice homepage:
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx

may be you are just using the wrong click event. May be you want just one event ... use longPressEnd instead..

You can increase the probability to get helpful answers if you rename your variables to self-explaining names

names like "test", "bro", "lol" are not lol they are annoying and force your potential helpers to do a lot of translation-work what does "bro" etc. stand for?

Do yourself a favor and rename the variables right now as long as your code is short and you don't have to rename too many places.
Give them meaningful names that your code becomes self-explaining.
If you come back in 2 months to modify this code even you yourself will have trouble to understand what "lol" stands for.

Same thing with function-names like click1, click10, click13
these names do not say which button ? in the sense of is it the enter /up / down button and does not specify the correct kind of press

click3 is not a click it is a long press
So the name of the function should say exactly this

ButtonEnter.attachDuringLongPress(longPressEnter);

or if you prefer shorter names something like
ButtonEnter.attachDuringLongPress(LP_Enter); // where "LP" is the short for long press

best regards Stefan

Hello zxalilh

Did you run some tutorials using a.m. libary to gain the knowledge?

Thank you, but I figured out the problem. And the variable names were so I wouldn’t get confused. Sorry if you got confused because of the variable names. The problem was that I connected the button to Gnd but in the code, I did it the other way

I bet
if you come back in 3 month to modify your code you will need an extra hour to analyser what variable is what. But maybe this is the extra fun for you.

Hello zxalilh

Take this overview to your records.

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