Button press time bug

Hello! I use 2 small pumps, arduino (nano), 3 buttons and 8x2 lcd. I would like to make that when the button is pressed, the pump is turned on and when the button is pressed again, it is turned off. But if the pump is in the on state for more than the preset time, it is automatically turned off. And so that this time can be adjusted using the third button. Here is my code:

#include <Bounce2.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define pump1 A3
#define pump2 A2
#define button1 A0
#define button2 A1
#define calibration A4

Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce();
Bounce calibrate = Bounce();

LiquidCrystal lcd(4, 5, 7, 6, 9, 8);

boolean status1 = false;
boolean status2 = false;

long prevMs1 = 0;
long prevMs2 = 0;
void setup() {
  pinMode(pump1, OUTPUT);
  pinMode(pump2, OUTPUT);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(calibration, INPUT_PULLUP);
  
  debouncer1.attach(button1);
  debouncer1.interval(5);
  debouncer2.attach(button2);
  debouncer2.interval(5);
  calibrate.attach(calibration);
  calibrate.interval(5);

  lcd.begin(8, 2);

  lcd.print("select");
  lcd.setCursor(0, 1);
  lcd.print("juice!");
}

void loop() {
  debouncer1.update();
  debouncer2.update();
  calibrate.update();
  if(!debouncer1.read() && !calibrate.read()) {
    if(!status1) prevMs1 = millis();
    status1 = !status1;
    if(status1) {
      EEPROM.write(1, millis() - prevMs1);
      lcd.clear();
      lcd.print((millis() - prevMs1) / 1000);
    }
  } else if(!debouncer1.read()) {
    status1 = !status1;
  }
  if(!debouncer2.read() && !calibrate.read()) {
    if(!status2) prevMs2 = millis();
    status2 = !status2;
    if(status2) {
      EEPROM.write(2, millis() - prevMs2);
      lcd.clear();
      lcd.print((millis() - prevMs2) / 1000);
    }
  } else if(!debouncer2.read()) {
    status2 = !status2;
  }
  digitalWrite(pump1, status1);
  digitalWrite(pump2, status2);
}

But when set, the display shows zero. Thank you in advance! Sorry for my bad English)

It is not the cause of your problem but the first thing I notice is that your timing variables are declared as long when they should really be unsigned long.

There is no need to use unsigned long, because the time variables are not so large (about 10 seconds). But in any case it does not help)

Not for the intervals but the stuff you read from millis does need to be unsigned long or the whole thing crashes after about three weeks when millis will appear to go negative.

There is a potential problem with your code and how you use Bounce2.

I would replace the .read() statements with .fell() so that you pick up state changes and not multiple events as the code loops while the button is still pressed.

I find your code to be confusing and obscure, as to the logic.

I would be explicit about pumps being on and off and not toggle a state.

I would also recommend the use a pair of brackets for every conditional so that you are explict about what is included.

I'm noob in programming) I realy don't know how to fix it! Help please.

I'm noob in programming) I realy don't know how to fix it! Help please.

Did you write this code?

Take a look at the Bounce2 example "Change" which shows how to use the .fell() on a button pin with INPUT_PULLUP. The example "Duration" shows how to record the value of millis() when the button is pressed.

I would like to make that when the button is pressed, the pump is turned on and when the button is pressed again, it is turned off. But if the pump is in the on state for more than the preset time, it is automatically turned off. And so that this time can be adjusted using the third button.

What is the reason for the EEPROM writes?
What information do you want to display on the lcd?

For the timeout, take a look at the use of millis() timers in this tutorial by Robin 2 Demonstration code for several things at the same time - Project Guidance - Arduino Forum

You will use an expression like

if(millis() - pumpStartTime => presetRunTime && pumpState ==ON )
{ 
//switch pump off
//set pumpState = OFF;
//display what you want 
}

Lowder:
There is no need to use unsigned long, because the time variables are not so large (about 10 seconds)

Wrong, I am afraid.

For 10 seconds in millis you can use unsigned int for timing but you then need to only get the 2 low bytes of millis().

Honest up! I use 8-bit unsigned to do short timing.

What is needed is unsigned variables to get the elapsed_time = end_time - start_time to work.

GoForSmoke:
For 10 seconds in millis you can use unsigned int for timing but you then need to only get the 2 low bytes of millis().

Honest up! I use 8-bit unsigned to do short timing.

The key word there is unsigned.