I encourage neophyte programmers to write what I call single line code, each line stands on its own no matter where they put it in the program and not to do ay branching. That produces a program loop that is fairly uniform in timing. On non critical timing that is relatively slow I just count the program loops and reset counter after so many. Generally the program is so fast that the delay routine has to be used
to slow it down. This is a little clunky but adjusting the delay controls the loop time. I don't know if your loop time is consistent (a number of serial prints can throw it way off), but this explanation may help someone. Most of my programs include a status indicator using the on board LED. The following is an example.
// FIRST BLINK - JUST TO SHOW THAT THE PROGRAM RUNNING
if (blinktime == 1) digitalWrite(13, 1);
// pin #13 is the on board LED, 1 is ON
if (blinktime == 20) digitalWrite(13, 0);
// turns the LED off so it blinks
// SECOND BLINK - IF DRIVE GREATER THAN 35
if (blinktime == 50 && PWM3 >= 35) digitalWrite(13, 1);
if (blinktime == 60) digitalWrite(13, 0);
// sets the LED off so it blinks
// THIRD BLINK - IF DRIVE GREATER THAN 70
if (blinktime == 90 && PWM3 >= 70) digitalWrite(13, 1);
if (blinktime == 100) digitalWrite(13, 0);
if (blinktime == 200) blinktime = 0;
//reset timing loop
blinktime = blinktime + 1;
// increment loop counter