NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

Hello Teckel.

First of all thanks a mil for your library I think is a lot of job and you are really kind sharing it with us.

I'm new to arduino and I have recently acquired a HC-SR04 sensor and I have donwloaded and installed your library for a test that I'm doing.

This is the little sketch that I'm running and I don't know if it's because I did something wrong but from time to time the sensor returns a 0 value.

#include <NewPing.h>

#define TRIGGER_PIN  7  
#define ECHO_PIN     6  
#define MAX_DISTANCE 200 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 
int Range=200;
boolean Alarm=false; 
int Counter = 1;
void setup() {
  Serial.begin(9600); 
}

void loop() {
  
  Range = sonar.ping_cm();
 
delay(20);
  
  if (Range < 10) { 

    Alarm = true;   
    Counter = 1;    
  }
  else if (Counter == 11) { 
    Alarm = false;          
    Counter = 1;
  }
  
  if (Alarm)
    AlarmOn();
  
}

void AlarmOn(){
Serial.print("Intruso en:");  
Serial.println(Range); 
Serial.print("Valor del contador:");  
Serial.println(Counter);  
  Counter++;
  
}

looking at the serial output I see that from time to time the sensor gets a 0 value and I don't know if it's a problem with my sketch or with the sensor itself...

Valor del contador:8
Intruso en:40
Valor del contador:9
Intruso en:40
Valor del contador:10
Intruso en loop:0
Intruso en:0
Valor del contador:1
Intruso en:39
Valor del contador:2
Intruso en:39
Valor del contador:3

I hope you can help thanks :wink: