Demonstration code for several things at the same time

A word of caution about the basic "mile marker" approach: an overrun will wreak havoc. A while loop overcomes the problem.

An example from CrossRoads' snippet...

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

void loop()
{
  unsigned long currentMicros;
  unsigned long elapsedMicros;
  static unsigned long nextMicros;
  const unsigned long duration = 1000000ul;

  currentMicros = micros();
  elapsedMicros = currentMicros - nextMicros;  
  if ( elapsedMicros >= duration )
  {
    nextMicros = nextMicros + duration;
    Serial.println( currentMicros );
  }
  // Pretend loop takes too long to execute
  delay( 1001 );
}

Change the if to a while.

1 Like