Using millis for counting and cummulating a button push time

I am trying to find a simple way to accumulate the time a button is pushed.
Here is what I came up with:

unsigned long BeforeMillis = 0;
unsigned long CumulatedUpTime;
#define BUTTON_PIN 14 // Blue - pluse of the timer comes from the cord
#define Second_Button 15 //Wire to the Reject switch

void setup() {
Serial.begin(9600);

Serial.print("Ready ");
pinMode(BUTTON_PIN, INPUT_PULLUP);

}

void loop() {

unsigned long NowMillis = millis();
while (digitalRead (BUTTON_PIN) ==0) // "0" means button Pushed
{

if (NowMillis - BeforeMillis>=1000 )
{ BeforeMillis = NowMillis;
CumulatedUpTime++;
}
//
}

Serial.print("CumulatedUpTime = ");
Serial.println(CumulatedUpTime);

Serial.print("digitalRead (BUTTON_PIN) = ");
Serial.println(digitalRead (BUTTON_PIN));

}

What happens: When I push button, cummulated time increments by one and the entire loop stops, including serialPrint.

What I wish for: As button is pushed, the CumulatedUpTime would increase every second and be shown in serial Print as CumulatedUpTime= 2 , 3, 4..etc

Please help

thanks

NowMillis doesn't change in your while loop. So your while loop is frozen in time constantly comparing to the same Now.

That doesn't need to be a while loop. The loop function is already repeating. Take advantage of that.

Pseudocode (untested)

loop
{
 if button is depressed
 {
    if millis - lastmillis > 0
    {
       cumulativeCount += 1
       lastmillis = millis
     }
  }
}

Thanks

I came up with this and it works

void loop() {

unsigned long NowMillis = millis();

if (NowMillis - BeforeMillis >= 1000 )

{
BeforeMillis = NowMillis;

if (digitalRead (BUTTON_PIN) == 0) // Pressed
{ CumulatedUpTime++;
}

else
{
CumulatedUpTime = CumulatedUpTime;
}

}

Serial.print("CumulatedUpTime = ");
Serial.print(CumulatedUpTime);

Serial.print("digitalRead ( BUTTON_PIN) = ");
Serial.println(digitalRead (BUTTON_PIN));
}

    else
    {
      CumulatedUpTime = CumulatedUpTime;
    }

Really? What do you think this does?

And here it is better formatted and in code tags. OP please note

void loop()
{
  unsigned long NowMillis = millis();
  if (NowMillis - BeforeMillis >= 1000 )
  {
    BeforeMillis = NowMillis;
    if (digitalRead (BUTTON_PIN) == 0) // Pressed
    {
      CumulatedUpTime++;
    }
    else
    {
      CumulatedUpTime = CumulatedUpTime;
    }
  }
  Serial.print("CumulatedUpTime = ");
  Serial.print(CumulatedUpTime);
  Serial.print("digitalRead (   BUTTON_PIN) = ");
  Serial.println(digitalRead (BUTTON_PIN));
}

A question for you

    else
    {
      CumulatedUpTime = CumulatedUpTime;
    }

Why do this ?

Wouldn't it be a lot simpler to record the value of millis() when the button is pressed and record it again to a different variable when the button is released? The difference between the two values will be the time it was pressed.

...R