Reset millis();

Good morning,
I use a push button...
I'd like if we press the first the millis() counter starts.
If I press a second time reset millis ( millis() counter becomes = 0 )

If I press it a third time, millis() starts again over from 0;

Wiring in attachment.
Thank you very much.

// 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 = 7; // the pin that the LED is attached to

const int ledPindoorA = 8; //
const int ledPindoorB = 9; //


// Variables will change:

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

int DeviceState = 0; // current state of DEVICE

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

int doorAState = 0;         // current state of the Door A
int doorBState = 0;         // current state of the Door B

int lastdoorAState = 0;     // previous state of the Door A
int lastdoorBState = 0;     // previous state of the Door B

int Operation = 0; //

unsigned long Dtemps = 0; // time Of the operation

unsigned long DAtimeO = 0; // Exact time the door A is opened
unsigned long DOtimeO = 0; // Exact time the door B is Opened

unsigned long DAtimeC = 0; // Exact time the door A is closed
unsigned long DBtimeC = 0; // Exact time the door B is closed

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the door pin as a input:
  pinMode(doorAPin, INPUT);
  // initialize the LED as an output:
  pinMode(doorBPin, INPUT);
  
  // initialize the LED as an output:
  pinMode(ledPinbutton, OUTPUT);
  // initialize the LED as an output:
  pinMode(ledPindoorA, OUTPUT);
   // initialize the LED as an output:
  pinMode(ledPindoorB, OUTPUT);
  
  // initialize serial communication:
  Serial.begin(9600);
        
  Serial.println( " Starting the program: Hello ... ");
  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 ( millis();)
        
        //turn LED BUTTON ON:
        digitalWrite(ledPinbutton, HIGH);
        Dtemps = millis(); // Exact time the operation began
        
		// DOOR A **************************************
         // read the pushbutton input pin:
    	 doorAState = digitalRead(doorAPin);
      		// compare the DoorState to its previous state
      		if (doorAState != lastdoorAState) {
   
        		if (doorAState == LOW) {// if the door A is Open
      				// turn LED DOOR A ON:
					digitalWrite(ledPindoorA, HIGH);
                  	DAtimeO = millis();
                  	
     
         		} //if (doorState == LOW)

         		else { // door A is closed
      	 	
           			// turn LED DOOR A OFF:
 					digitalWrite(ledPindoorA, LOW);
                    DAtimeC = millis();
             
         		} //else

     		}//if (doorAState != lastdoorAState)
        
         // save the current state as the last state, for next time through the loop
     	 lastdoorAState = doorAState;
    	// *********************************************
        
        // DOOR B **************************************
         // read the pushbutton input pin:
    	 doorBState = digitalRead(doorBPin);
  
      		// compare the DoorState to its previous state
      		if (doorBState != lastdoorBState) {
       
        		if (doorBState == LOW) {// if the door A is Open
        			// turn LED DOOR B ON:
 					digitalWrite(ledPindoorB, HIGH);
                  	DBtimeO = millis();
         
        		} //if (doorState == LOW)

         		else { // door B is closed
      	 			// turn LED DOOR B OFF:
 					digitalWrite(ledPindoorB, LOW);
                  	DBtimeC = millis();
          
         			} //else

      		}//if (doorBState != lastdoorBState)
  
         // save the current state as the last state, for next time through the loop
      	 lastdoorBState = doorBState;
        // *********************************************      
       
      } // if (buttonPushCounter % 2 == 1)
    
   // Second Press  
   if (buttonPushCounter % 2 == 0) { 
     
     
     // Reset Millis = 0 ( Dtemps , DAtimeO , DAtimeC ,  DBtimeO , DBtimeC) = 0
       
     
     // turn LED BUTTON OFF:
       digitalWrite(ledPinbutton, LOW);
       // turn LED DOOR A OFF:
       digitalWrite(ledPindoorA, LOW);
       // turn LED DOOR B OFF:
       digitalWrite(ledPindoorB, LOW);
       // 
       
       
     
     } //(buttonPushCounter % 2 == 0)

 
 delay(10);
        
} //void loop

Capture d’écran 2020-05-28 à 16.44.38.png

You never need to reset millis(), just save its value when an action happens and compare its value later with the value previously saved to determine how much time has passed.

The start and end values do not matter, rather it is the difference between them that you are interested in.

UKHeliBob:
You never need to reset millis(), just save its value when an action happens and compare its value later with the value previously saved to determine how much time has passed.

The start and end values do not matter, rather it is the difference between them that you are interested in.

Thank you. Thank you;
Only I would like that if I turn it off and back on again (with the push-button), that all the values return to 0;

It is not possible to change the value of millis(). Nobody worries very much about that, because you can always compare with an offset as described in reply #1. However, you do need to keep an offset variable to store the millis() value at the time you want to start counting. After that, you just subtract it from millis() to get the elapsed time.

aarg:
It is not possible to change the value of millis().

Of course it is possible. No need to lie about it, it's not like a secret or anything.

However, I do agree generally that it is unnecessary.

It comes in handy if you want, for example, to test your roll over handling without waiting 55 days or however long it takes for milliseconds() to roll.

I imagine a similar approach is available for micros().

This is a the example from the URL above w/ a little logic to show it working.

extern volatile unsigned long timer0_millis;
unsigned long new_value = 1000;

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

void loop(){

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

  if (millis() > 4000)
    setMillis(new_value);

  delay(333);
}

void setMillis(unsigned long new_millis){
  uint8_t oldSREG = SREG;
  cli();
  timer0_millis = new_millis;
  SREG = oldSREG;
}

HTH

a7

mondoha:
all the values return to 0;

All what values?