Timer

Hello,
I need your help if possible.
If you press the push button the led lights up and a timer is started.
and if you press the led is off for the second time, the number of cycles ( on/off ) ( Op ) and the time the led was on ( Optime ) are displayed on the serial Monitor.
and if you press a third time the led lights up and the timer returns to 0, and so on.
The problem is that it only gives 0 on Optime.
(Wiring and result in attachment )
Thank you for helping me

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to

const int doorAPin = 3; // the pin that the pushbutton is attached to
const int doorBPin = 4; //

const int ledPinbutton = 9; // the pin that the LED is attached to

// Variables will change:

int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

int buttonPushCounter = 0; // counter for the number of button presses

bool singleAction1 = false;
bool singleAction2 = false;

//

int Op = 0; //
unsigned long OptimeBgn = 0; // Exact time the operation Began
unsigned long OptimeEnd = 0; // Exact time the operation End
unsigned long Optime = 0;  // Time of the operation
        
//

unsigned long sOptime = 0;  // Time of the operation

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
 
  
  // initialize the LED as an output:
  pinMode(ledPinbutton, OUTPUT);
 
  
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("**************");
  Serial.println("  Op | OpTime ");
  Serial.println("--------------");
 
} //void setup

void loop() {
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;
    
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
    
     } //if (buttonState == HIGH)
   } //if (buttonState != lastButtonState)
  
 
  // First Press  
      if (buttonPushCounter % 2 == 1) {
       
        // Starting the counter
        
        OptimeBgn = millis(); // Exact time the operation began
        
        //
        Optime = 0;
        
        //turn LED BUTTON ON:
        digitalWrite(ledPinbutton, HIGH);   
        // 
        calcul(); 
        //   
        if (! singleAction1) {
        SerialDisplayloop();
      	singleAction1 = true;
    	}
        
        //
        singleAction2 = false;
     
       
      } // if (buttonPushCounter % 2 == 1)
    
   // Second Press  
   	if (buttonPushCounter % 2 == 0) { 
  
     	OptimeEnd = millis();
      
        //
         Optime = OptimeEnd - OptimeBgn ;
      
    	// turn LED BUTTON OFF:
       	digitalWrite(ledPinbutton, LOW);
      
        calcul();
        //
        if (! singleAction2) {
        SerialDisplayloop();
      	singleAction2 = true;
    		
         }
     	//
        singleAction1 = false;
      
       
     } //(buttonPushCounter % 2 == 0)

 delay(10);
        
} //void loop


void calcul () { 

//
  Op = buttonPushCounter / 2 ; 
//
  sOptime = 0.001 * Optime ;


} // void calcul ()


// Serial Display Function ******************************************************************************

	//SerialDisplayloop
	void SerialDisplayloop() {
         
        // Op:
        if (Op < 10){Serial.print("  ");}
      	else if (Op < 100){Serial.print(" ");}
      	Serial.print(Op);
      	Serial.print(" | ");
        
         //Op Time
      	if ( sOptime < 10){ Serial.print("     ");}
      	else if ( sOptime < 100){ Serial.print("    ");}
      	else if ( sOptime < 1000){ Serial.print("   ");}
      	else if ( sOptime < 10000){ Serial.print("  ");}
      	Serial.print(sOptime);
      
        Serial.println("");
      	Serial.println("--------------");
    }

Capture d’écran du 2020-06-21 11-57-31.png

consider (just easier to show you)

#define ButPin  A1
#define LedPin  10

#define ON  LOW
#define OFF HIGH

byte but;
byte butLst;
byte butCnt;

unsigned long msec;

char s [100];

// -----------------------------------------------------------------------------
void
turnOn (void)
{
    msec = millis();
    digitalWrite (LedPin, ON);
}

// -----------------------------------------------------------------------------
void
turnOff (void)
{
    sprintf (s, " %6ld msec", millis () - msec);
    Serial.println (s);
    digitalWrite (LedPin, OFF);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    but = digitalRead (ButPin);
    if (butLst != but)  {
        butLst = but;

        if (LOW == but)  {
            if (butCnt++ %2)  
                turnOff ();
            else
                turnOn ();
        }
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);

    pinMode (ButPin, INPUT_PULLUP);
    butLst = digitalRead (ButPin);

    pinMode (LedPin, OUTPUT);
    digitalWrite (LedPin, OFF);
}

As a first step to debugging it, I'd print the values of the other three time related variables when you print sOptime.

gcjr:
consider (just easier to show you)

#define ButPin  A1

#define LedPin  10

#define ON  LOW
#define OFF HIGH

byte but;
byte butLst;
byte butCnt;

unsigned long msec;

char s [100];

// -----------------------------------------------------------------------------
void
turnOn (void)
{
    msec = millis();
    digitalWrite (LedPin, ON);
}

// -----------------------------------------------------------------------------
void
turnOff (void)
{
    sprintf (s, " %6ld msec", millis () - msec);
    Serial.println (s);
    digitalWrite (LedPin, OFF);
}

// -----------------------------------------------------------------------------
void
loop ()
{
    but = digitalRead (ButPin);
    if (butLst != but)  {
        butLst = but;

if (LOW == but)  {
            if (butCnt++ %2) 
                turnOff ();
            else
                turnOn ();
        }
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);

pinMode (ButPin, INPUT_PULLUP);
    butLst = digitalRead (ButPin);

pinMode (LedPin, OUTPUT);
    digitalWrite (LedPin, OFF);
}

Thank you
Hello I tried this code it displays the time the light was off not the time the light was on.

it depends on how you define ON and OFF.
ON is LOW for the hardware i tested on