Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()

This code-version is a new idea to visualise what the code is doing

If you open the serial monitor you see three lines:

left  Z/A changes every 0,5 seconds
right X/O changes every 2,0 seconds
Z:1455X

in the third line there is on the left letter 'A' or 'Z' which change once every 0,5 seconds
then there is a number that is counting up fast
on the right you see the letter 'X' or 'O' which change once every 2 seconds

This means there is happening a change 'A' / 'Z' / 'A' / 'Z' / 'A' / 'Z' .... every 0,5 seconds
The number is incremented once every 0,05 seconds
there is happening a change 'X' / 'O' / 'X' / 'O' / 'X' / 'O'..... every 2 seconds

this means three things change at different frequencies.

at first concentrate on the code written in void loop()

void loop() {
  blink_AZ(); // change character every  500 milliseconds between 'A' and 'Z'
  blink_XO(); // change character every  2000 milliseconds between 'X' and 'O'
  countUpPrintEach50ms(); // once every 50 milliseconds count up and print
}

these three lines are called 10000 times per second !
remark: there is no delay()

but

there are "delayed" actions delayed by non-blocking timing

only once every 500 milliseconds change 'A' / 'Z'
only once every 50 milliseconds increment number
only once every 2000 milliseconds change 'X' /'O'

to make it even more clear
function blink_AZ(); is executed 10000 times per second but only 2 times per second the character changes between A and Z

function blink_XO() is executed 10000 times per second but only 0,5 times per second the character changes between X and O

function countUpPrintEach50ms(); is executed 10000 times per second but only 20 times per second the counter is incremented by 1 and character left: number character right are printed to the serial monitor

the number counting up very fast represents the fast repeating loop (though the incrementing of the number is slowed down too, to make the increasing number readable)
the A / Z X / O represents doing things slowed down - doing a change just from time to time

After having understood this fact that all three functions are executed 10000 times per second look at the other details of the other functions below void loop()

Here is the complete code

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
unsigned long myBlink_XO_Timer;
unsigned long myBlink_AZ_Timer;

char X_or_O = 'X';
char A_or_Z = 'A';

long myCounter = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
}

void loop() {
  blink_AZ(); // change character every  500 milliseconds between 'A' and 'Z'
  blink_XO(); // change character every  2000 milliseconds between 'X' and 'O'
  countUpPrintEach50ms(); // once every 50 milliseconds count up and print
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

void blink_AZ() {
  if ( TimePeriodIsOver(myBlink_AZ_Timer, 500) ) { // check if 500 milliseconds have passed by
    // if 500 milliseconds HAVE passed by
    if (A_or_Z == 'A') { // check if character is an 'A'
      A_or_Z = 'Z';      // change to 'Z'
    }
    else {
      A_or_Z = 'A';      // change to 'A'
    }
  }
}

void blink_XO() {
  if ( TimePeriodIsOver(myBlink_XO_Timer, 2000) ) { // check if 2000 milliseconds have passed by
    // if 2000 milliseconds HAVE passed by
    if (X_or_O == 'X') { // check if character is an 'X'
      X_or_O = 'O';      // change to 'O'
    }
    else {
      X_or_O = 'X';      // change to 'X'
    }
  }
}

void clearSerialMonitor() {
  for (int i = 0; i < 50; i++) { // print 50 empty lines to clear screen
    Serial.println();
  }
}


void countUpPrintEach50ms() {
  if ( TimePeriodIsOver(MyTestTimer, 50) ) { // check if 50 milliseconds have passed by
    // if 50 milliseconds HAVE passed by
    myCounter++;
    clearSerialMonitor();
    Serial.println("left  Z/A changes every 0,5 seconds");
    Serial.println("right X/O changes every 2,0 seconds");
    Serial.print(A_or_Z);
    Serial.print(":");
    Serial.print(myCounter);
    Serial.println(X_or_O);
  }
}

best regards Stefan

1 Like