arduinoarduino-unoarduino-idearduino-esp8266arduino-c++

How to keep millis() at 0?


I'm making an Arduino reverse stopwatch..but Seems to have a problem with millis() function Whenever I upload the code on Arduino the millis starts running itself..how can I keep it at 0 until I call the millis. or any alternatives to solve it...

#include "SevSeg.h"
int button1 = 11;
int button2 = 12;
int button3 = 13;
int value = 10;
int  timer = 0;
bool n = true;

SevSeg Display;
void setup() {
 Serial.begin(9600);
 byte numDigits = 2;
 byte digitPins[] = {9,8};
 byte segmentPins[] = {10,2, 3, 4, 5, 6, 7,1};
 bool resistorsOnSegments = true;
 bool updateWithDelays = true;
 byte hardwareConfig = COMMON_ANODE;
 Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
 Display.setBrightness(100);


}

void loop() {
   Display.setNumber(value, 1);
   Display.refreshDisplay();
   
 if (digitalRead(11)==HIGH){
   Start(value);
 }  
}


void Start(int value){ 
 while(n){
   unsigned long timerGlobal = millis();
   Display.setNumber(value-timerGlobal/1000, 1);
   Display.refreshDisplay();
   if ((value-timerGlobal/1000) == 0){
     n = false;
   }
   
 }
 
}


Solution

  • This should work

    //#include "SevSeg.h"
                int button1 = 11;
                int button2 = 12;
                int button3 = 13;
                int value = 10;
                int  timer = 0;
                bool n = true;
    
                //Added
                int currentTime = 0;
                long int t0 = 0;
                long int t1 = 0;
    
                //SevSeg Display;
                void setup() {
                 Serial.begin(9600);
                 byte numDigits = 2;
                 byte digitPins[] = {9,8};
                 byte segmentPins[] = {10,2, 3, 4, 5, 6, 7,1};
                 bool resistorsOnSegments = true;
                 bool updateWithDelays = true;
                 //byte hardwareConfig = COMMON_ANODE;
                 //Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
                 //Display.setBrightness(100);
                 
    
    
                }
                void loop() {
                   //Display.setNumber(value, 1);
                  // Display.refreshDisplay();
                   
                 if (digitalRead(11)==HIGH){
                   Start(value);
                 }  
                }
    
    
                void Start(int value){ 
                 t0 = millis();
                 while(n){
                   t1 = millis();
                   if(t1 > (1000+t0)){
                      //has passed 1 second
                      currentTime++;
                      if(value-currentTime == 0){
                    n = false;
                      }
                    
                   }
                   unsigned long timerGlobal = t1-t0;
                   //Display.setNumber(value-timerGlobal/1000, 1);
                   //Display.refreshDisplay();
                   
                   
                 }
                 
                }