Counting push button inputs

Hey, guys, i am making a device which counts a number of a button as input. If no input is given for 2 seconds, It should exit the loop and give me the count which I can use for lighting up the bar graph.

byte count =0;
byte ButtonState;
byte lastState = LOW;
const byte Button = A2;
unsigned long time1=0;
unsigned long time2=0;

void setup(){
  pinMode(Button,INPUT);
  Serial.begin(9600);
}
void loop(){
  while(1){
    
    ButtonState = digitalRead(Button);

    if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed!!!!!
    {
      time1=0;
      time1=millis();
      if(count < 10) {// This will check to see if the count is within a range of 0 - 21, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
       
        count += 1;


        Serial.println(count);
      } // same as count = count + 1;
      else{
        count = 0;
      }
      lastState = ButtonState;
    }
     else if(time1>2000){
          Serial.println("break");
          break;

        }  

  }
}

Alright, you described what you want. Now please describe what goes wrong. Aka, what happens now.

PS, I don't think you want to count buttons because I think you know how many you have... Did you mean button presses? :wink:

Setting a variable to millis() and testing whether it is greater than 2000 means that even if it worked your user would have 2 seconds at most and that the code would only work once.

Better to save the start time from millis() and subtract it from the current time later to determine whether 2000 milliseconds has passed.

thanks guys