Help with stopwatch delay Millis


Please edit your post to add code tags, as described in the "How to use the forum" post.

Here is a demo-code with a function "TimePeriodIsOver"

I'm trying to write easy to understand code for newbees. Though I'm somehow an advanced programmer
I'm more or less blind for beginners difficulties (caused by the knowledge I have). So I would be very thankful if you write feedback about this democode if it was easy to understand or whatever questions you have.

Newbees are the best experts about the aspect "easy to understand"

// Demo-code showing how to use a function TimePeriodIsOver based on function millis()
// This timer-function reduces the number of code-lines you have to write 
// compared to the "standard" millis()-code
// the function delivers a true if "TimePeriodIsOver" and false if not
// The timer-variable gets updated automatically through the technique of calling
// the variable "by reference" This is the &-character in front of the parameter expireTime
// So just define another timer-variable of type unsigned long with a variable-name of your choice
// and write the if-condition 

// This demo-code shows how to execute code after a certain amount of time defined in the
// second parameter "TimePeriod" of the function 
// this requires to execute the if-condition fast enough before "TimePeriod" has passed by

// for timing that is more like a delayed-oneshot - additional logic is needed

unsigned long MyTimer1;
unsigned long MySecondTimer;

unsigned long MyCounter = 0;  // used to demonstrate the fast execution of the loop itself

boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  else return false;            // not expired

void setup() {
  Serial.begin(115200); // be sure to change baudrate in the serial -monitor to the value inside the brackets
  Serial.println("Setup Start");

void loop() {

  // this loop is running very fast
  // how fast can be seen in the increase of the value of variable MyCounter
  // but the serial output only is executed every 1 Second MyTimer1 
  // and every 5 seconds for MySecondTimer
  if (TimePeriodIsOver (MyTimer1,1000) ) {  // 1000 milliseconds = 1 second
    Serial.print("MyTimer1 has expired MyCounter=");

  if (TimePeriodIsOver (MySecondTimer,5000) ) { // 5000 milliseconds = 5 seconds
    Serial.println("MySecondTimer has expired");

best regards Stefan


Yes there is a programmed function in the code. Any code that is bigger than 30 lines should be divided in functional-modules where each modul does one thing.

I haven't used this stopwatch-library. But it is quite easy to test:
as pseudo-code:

when liquid flowing starts
make snapshot of millis into a variable of type unsigned long for example named "StartPouringOut"

unsigned long StartPouringOut;
unsigned long PouringTime;
unsigned long NinetyPercentOut ;

unsigned long additionalTimeToWait;

StartPouringOut = millis();

if (Sensor == HIGH)
// store snapshot of millis
NinetyPercentOut = millis();

PouringTime = NinetyPercentOut - StartPouringOut;
additionalTimeToWait = PouringTime / 9;

if (sw_millis.elapsed() >= (PouringTime + additionalTimeToWait )

If you increase the times inside a testprogram you can check with the stopwatch-function of your watch or your smartphone
If your code works as expected

best regards Stefan