HC-SR04 Program glitches / bug fix needed [SOLVED]

Hey All,

I had a project with arduino and HC-SR04 sensor .
The aim is to measure the distance and set an alert system off .
But each time my object goes far than 60 cm the alert system fails .
The serial monitor displays a distance less by almost 10 cm ,which in turn give's out wrong alert .
My code is below (Most of the code was done by me ,I know C / C++)

#define trigPin 7 //TRIG INPUT
#define echoPin 6 //ECHO OUTPUT
#define led 13 //LED BLUE 
#define led2 12 //LED BLUE 
#define led3 11 //LED YELLOW 
#define led4 10 //LED YELLOW 
#define led5 9 //LED RED 
#define led6 8 //LED RED 
#define buzzer 3 //BUZZER  

// Project Work on Distance Measurment Using HC-SR04 Sensor
// Compiled By Febin K Philip
// Febkosq8

int sound = 150;


void setup() {
  Serial.begin (19200); //Opens Serial Port at 19200
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(buzzer, OUTPUT);
}
  int count=5;
  
void loop() {
  long duration, distance; 
  digitalWrite(trigPin, LOW); //Set the trig pin to LOW state
  delayMicroseconds(2); //Halt the program for specified microseconds
  digitalWrite(trigPin, HIGH); //Set the trig pin to HIGH state
  delayMicroseconds(10); //Halt the program for specified microseconds
  digitalWrite(trigPin, LOW) ; //Set the trig pin to LOW state

  duration = pulseIn(echoPin, HIGH); //Finding the duration taken by the wave in traveling
  distance = (duration/2) / 29.1;  //Finding the distance using the extracted data and eqn
  if(count%5 == 0)
  {
    Serial.println("\n:) Program Compiled By Febkosq8 (:\n");
  }
  count=count+1;
   
   if (distance <= 60) {   //Conditions for the program to trigger the alert system
    digitalWrite(led, HIGH);
    sound = 50;
}
  else {
    digitalWrite(led,LOW);
  }
  if (distance < 50) {   //Conditions for the program to trigger the alert system

      digitalWrite(led2, HIGH);
      sound = 200;
}
  else {
      digitalWrite(led2, LOW);
  }
  if (distance < 40) {   //Conditions for the program to trigger the alert system

      digitalWrite(led3, HIGH);
      sound = 350;
} 
  else {
    digitalWrite(led3, LOW);
  }
  if (distance < 30) {      //Conditions for the program to trigger the alert system

    digitalWrite(led4, HIGH);
    sound = 500;
}
  else {
    digitalWrite(led4,LOW);
  }
  if (distance < 20) {  //Conditions for the program to trigger the alert system
    digitalWrite(led5, HIGH);
    sound = 650;
}
  else {
    digitalWrite(led5,LOW);
  }
  if (distance < 10) {   //Conditions for the program to trigger the alert system

    digitalWrite(led6, HIGH);
    sound = 800;
}
  else {
    digitalWrite(led6,LOW);
  }
 
  if (distance > 60 || distance <= 1){     //Conditions for the program to trigger the  alert system

    digitalWrite(led,HIGH);
    digitalWrite(led3,HIGH);
    digitalWrite(led5,HIGH);
    delay(250);
    digitalWrite (led,LOW);
    digitalWrite(led3,LOW); //LED animation for showing no target
    digitalWrite(led5,LOW);
    delay(300);
    digitalWrite(led2,HIGH);
    digitalWrite(led4,HIGH);
    digitalWrite(led6,HIGH);
    Serial.println("\nSNAP :( Target is Out Of Range \n");
    tone(buzzer,10500); //Sound alert for no target 
  }
  else {    
    Serial.print("Target is now ");
    Serial.print(distance);;
    Serial.println(" cm away :)\n");
    tone(buzzer, sound);
   
  }
  delay(333); //Halt the program for specified microseconds
}

The confusing thing is that when i run a normal code without any alert system it measures distance perfectly .See below code .It has no problem's and run's correctly without displaying any inaccuracy .

// defines pins numbers
const int trigPin = 7;
const int echoPin = 6;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(19200); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("Duration :");
Serial.println(duration);
}

Please help me solving my mystery .Seem's like a programming problem ..I am not able to figure it out ...
(My project picture,Serial Monitor output is also attached )
Thanks in advance ...

EDIT 1 :Here is the Solution .
Replace Old extract of code.

.
.
.
.
.
 Serial.println("\nSNAP :( Target is Out Of Range \n");
    tone(buzzer,10500); //Sound alert for no target

.
.
.
.
.

New piece of code

.
.
.
.
.
    Serial.println("\nSNAP :( Target is Out Of Range \n");
    tone(buzzer,900);//Sound alert for no target 
    delay(100);
    noTone(buzzer);


.
.
.
.

Seems like arduino wasn't able to power up the buzzer correctly which made the sensor go mad !

Hope this help's .

From the program that works

int distance;
.
.
.
distance= duration*0.034/2;

From the program with the problem

  long duration, distance;
.
.
.
  distance = (duration / 2) / 29.1;

What happens if you replace the problem code with the working code ?

UKHeliBob:
From the program that works

int distance;

.
.
.
distance= duration*0.034/2;




From the program with the problem


long duration, distance;
.
.
.
  distance = (duration / 2) / 29.1;




What happens if you replace the problem code with the working code ?

Thanks for replying
I already tried that but to avail no use .
Same issue persist's
Those code's are just 2 different versions of the equation .

Figured the workaround out myself ...Still don't know clearly what the problem was :
Here is the old extract of code which i replaced

.
.
.
.
.
 Serial.println("\nSNAP :( Target is Out Of Range \n");
    tone(buzzer,10500); //Sound alert for no target

.
.
.
.
.

New piece of code

.
.
.
.
.
    Serial.println("\nSNAP :( Target is Out Of Range \n");
    tone(buzzer,900);//Sound alert for no target 
    delay(100);
    noTone(buzzer);


.
.
.
.

Seems like arduino wasn't able to power up the buzzer correctly which made the sensor go mad !

Seems like arduino wasn't able to power up the buzzer

There is a limit to how much current an Arduino pin is rated to deliver (20mA if I remember correctly) What sort of buzzer are you using ?

UKHeliBob:
There is a limit to how much current an Arduino pin is rated to deliver (20mA if I remember correctly) What sort of buzzer are you using ?

I suppose i was using "Piezo Electric Buzzer Alarm 5V 12V Continuous for Arduino" as found on Amazon.in ...