My arduino reads the button selection twice, then it stops reading inputs

hello I am working on a vending machine project.
Basically I have three buttons which are read from the arduino and six products that can be selected.
this means that if you press once the button number one you will get a product, if you press it twice you will get another product.
this method works for every button.
My problem essencially is that the arduino reads my first selection, it reads also the second one , but if I do a third selection it looks as if he was not recieving any input from the buttons. I am sure it is a problem in the coding but I really can't understand what it is.
Thanks for your help

#include <SoftwareSerial.h>
#define cw 1
#define ccw 0

int black = 9;
int orange = 10;
int yellow = 12;
int brown = 11;
int button1= 7;
int button2 =6;
int button3 =5;
int solenoid= 8;
int hb= 3;
int lastpresstime=0;
int buttoncount1=0;
int buttoncount2=0;
int buttoncount3=0;
int currentStep =0;
SoftwareSerial mySerial(1,4);


void setup() {

  pinMode(black, OUTPUT);
  pinMode(orange, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(brown, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(solenoid, OUTPUT);
  pinMode(hb,INPUT);
  mySerial.begin(9600);
  Serial.begin(9600);
}


void loop() {
  
  
  
  
  mySerial.write(254); // CLEAR SCREEN
  mySerial.write(1);

 
  mySerial.write("VAndy Machine!!");
  
  

lastpresstime=millis();
  while(millis()<lastpresstime+2000)
  {
    if (digitalRead (button1))
    {
      delay(10);
    while (digitalRead(button1));
    buttoncount1++;
        Serial.print("buttoncount1");
  Serial.println(buttoncount1);
  lastpresstime=millis();
}
    if (digitalRead (button2))
    {
      delay(10);
    while (digitalRead(button2));
    buttoncount2++;
     Serial.print("buttoncount2 ");
  Serial.println(buttoncount2);
  lastpresstime=millis();
}
    if (digitalRead (button3))
    {
      delay(10);
    while (digitalRead(button3));
    buttoncount3++;
  Serial.print("buttoncount3 ");
  Serial.println(buttoncount3);
  lastpresstime=millis();
}

  }

 if (buttoncount1 == 1)
 {
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(1);
 
  mySerial.write("1"); 
  
 letsStep (cw,1000,20);
 delay(500);
 digitalWrite (solenoid,HIGH);
 delay (500);
 digitalWrite (solenoid,LOW);
 motorhome();
 }
 
 if (buttoncount1 == 2)
  {  
    
    mySerial.write(254); // move cursor to beginning of first line
     mySerial.write(1);

     mySerial.write("11"); 
    
    letsStep (cw,150,20);
    delay(500);
    digitalWrite (solenoid,HIGH);
    delay (500);
    digitalWrite (solenoid,LOW);
    motorhome();
  }
 

 if (buttoncount2 == 1)
 {
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(1);
 
  mySerial.write("2");   
   
 
 letsStep (cw,200,20);
 delay(500);
 digitalWrite (solenoid,HIGH);
 delay (500);
 digitalWrite (solenoid,LOW);
 motorhome();
 }
 
 if (buttoncount2 == 2)
  { 
  
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(1);

  mySerial.write("22"); 
  letsStep (cw,300,20);
  delay(500);
  digitalWrite (solenoid,HIGH);
  delay (500);
  digitalWrite (solenoid,LOW);
  motorhome();
}



 if (buttoncount3 == 1)
 { 
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(1);

  mySerial.write("3"); 
   
 
 letsStep (cw,400,20);
 delay(500);
 digitalWrite (solenoid,HIGH);
 delay (500);
 digitalWrite (solenoid,LOW);
 motorhome();
 }
 
 if (buttoncount3 == 2)
  {
    
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(1);

  mySerial.write("33"); 
  letsStep (cw,500,20);
  delay(500);
  digitalWrite (solenoid,HIGH);
  delay (500);
  digitalWrite (solenoid,LOW);
  motorhome();
}
 }
  


void motorhome(){
 //int state2=digitalRead(hb);
  while (digitalRead(hb)==HIGH)
  {
    letsStep(ccw,1,20);
  }
  if (digitalRead(hb)== LOW)
  { letsStep (cw, 300,20);
  buttoncount1=0;
  buttoncount2=0;
  buttoncount3=0;
  lastpresstime=0;
  currentStep=0;
}}
  
  

void letsStep(int direc, int steps, int time){

  for(int i=0; i<steps; i++)
  {

    if (direc==cw)
    {
      currentStep-=1;
      if (currentStep<0)
      {
        currentStep=3; 
      } 
    }
    else if (direc==ccw)
    {
      currentStep+=1;    
      if (currentStep>90)
      {
        currentStep=0; 
      }
    }
    else
    {
      Serial.println("Error: Direction not set correctly");   
    }

    if( currentStep==0)
    {
      digitalWrite(black,LOW);
      digitalWrite(brown,HIGH);
      digitalWrite(orange,HIGH);
      digitalWrite(yellow,LOW);
    }
    else if( currentStep==1)
    {
      digitalWrite(black,LOW);
      digitalWrite(brown,HIGH);
      digitalWrite(orange,LOW);
      digitalWrite(yellow,HIGH);
    }  
    else if( currentStep==2)
    {
      digitalWrite(black,HIGH);
      digitalWrite(brown,LOW);
      digitalWrite(orange,LOW);
      digitalWrite(yellow,HIGH); 
    }
    else if( currentStep==3)
    {
      digitalWrite(black,HIGH);
      digitalWrite(brown,LOW);
      digitalWrite(orange,HIGH);
      digitalWrite(yellow,LOW);
    }
    else
    {
      Serial.println("Error: out of step # range");   
    }
    delay(time);
    Serial.println(currentStep);

  }
}

It would have been easier for me if you had just posted the button piece of the code.

Without studying it carefully it looks like you have all the buttons mixed up together and all depending on and affecting lastPressTime. I suspect you need to have a separate time interval for each button so they can't affect each other.

I can't figure out how your code is supposed to distinguish between one button press or two. Since you have a display, why not use one button to cycle through the product options and another button to deliver the selected option.

Also, any variable (such as lastPressTime) that holds the value from millis() must be declared unsigned long.

...R

 while(millis()<lastpresstime+2000)

Well that's broken right there. You can't compare millis() with a value since
its a counter that wraps-round. It won't show up for days (but with micros() it well).

The correct idiom is

 while(millis() - lastpresstime < 2000)

You must do the subtraction - then you have a value to compare that's not wrapping-round.

But back to the problem - you haven't said how these buttons are wired, but it looks like
you have them connected from 5V to the pin - I hope there are pull-down resistors too.