NewPing.h - timer_ms(frequency, function) - Wrong values

Hello,

The original example Sketch from NewPing Libary = correct values

But the sketch with timer_ms method = wrong values

the original Code:

include <NewPing.h>
 
#define TRIGGER_PIN  18
#define ECHO_PIN     19
#define MAX_DISTANCE 200
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  delay(50);
  int uS = sonar.ping();
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");
}

Here i got 80cm

The other Code:

#include <NewPing.h>

#define TRIGGER_PIN  18
#define ECHO_PIN     19
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


void int_messung()
{
int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");
}

void setup() 
{
  Serial.begin(9600);
  NewPing::timer_ms(50, int_messung);
}

void loop()
{

}

here i got 7 cm

The same result I get when I us the Libary TimerOne.h

Can someone help me?

Joe

I suspect that the problem is using Serial.print() inside an ISR. Each output byte takes about 1 ms at 9600 baud.