Buzzer screaches

I was told in the Audio forum that this is a software problem:

I’ve put together a project with a arduino nano, a HC-SR04 and a piezo electric buzzer.
I’ve set the distance in the code/sketch to pick up any movement and sound the buzzer, which works fine.
But the buzzer sounds like a strangled chicken most of the time, and not very loud, so I would like to use something more solid, or better sound.

i have found that the further away from the HC-SR04 the detection is, then the buzzer screaches haphazardly. If I move an object closer to say about 8 or 10 inches, the sound is ok and doesn’t screach.

I added the NewPing library to the original sketch to see if it would help.

#include <NewPing.h>

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;

NewPing sonar (9, 10, 60); 
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // 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;

safetyDistance = distance;
if (safetyDistance <= 60){   <<<<<<<<<<<<< at this distance the buzzer screaches

  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{NewPing sona
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
//Serial.print("Distance: ");
//Serial.println(distance);
}

Thanks

Most probably the buzzer needs some hysteresis, so that it is not turned on and off arbitrarily around the 60 distance. E.g. turn it on only below 58 and off again above 62.

DrDiettrich:
Most probably the buzzer needs some hysteresis, so that it is not turned on and off arbitrarily around the 60 distance. E.g. turn it on only below 58 and off again above 62.

Mmm, how do I acheive that given that the definition of the word is a bit over my head.

Thanks

Have a bool variable buzzerOn, set it if the value drops below the lower bound, and clear it if the value goes above the upper bound. Then turn the buzzer on or off depending on buzzerOn.

Ok, if I have say:
bool buzzerOn = false;

How and where do I determine the high and low bounds.

Thanks

if (distance < lowLimit) buzzerOn = true;
else if (distance > highLimit) buzzerOn = false;
digitalWrite(buzzerPin, buzzerOn);

Thanks, and this is how I have the sketch now, but I expect something is not right , as the buzzer still screaches.
Do I have to change some other parts of the sketch.

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 11;
const int ledPin = 13;
bool buzzerOn = false;

// defines variables
long duration;
int distance;
int safetyDistance;
int lowLimit;
int highLimit;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
if (distance < lowLimit) buzzerOn = true;
else if (distance > highLimit) buzzerOn = false;
digitalWrite(buzzerPin, buzzerOn); 
// 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;

safetyDistance = distance;
if (safetyDistance <= 60){
  digitalWrite(buzzerPin.
  , HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzerPin, LOW);
  digitalWrite(ledPin, LOW);
}
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

duration will be in milliseconds.

GoForSmoke:

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);




duration will be in milliseconds.

Ok, so does something need to be changed.

GoForSmoke:

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);




duration will be in milliseconds.

pulseIn() gives the duration in microseconds, which is what is required for the correct resolution.

Sound will travel more than a third of a metre (or a foot) in a millisecond!

It is microseconds, shows how much I don't use it. Karma to John.

So do I change:

duration = pulseIn(echoPin, HIGH);

to

duration = pulseIn();

in the sketch.

Thanks

Doing what I asked doesn't compile, too few argument.

Sorry about the last mistake. Let's see about now in your code:

else{NewPing sona
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

It looks unfinished.

Also if you use mm for working distance units and ms for working time units

speed of sound in dry air at 20 °C = 343 m / s

becomes

speed of sound in dry air at 20 °C = 343 mm / ms

This allows me to use integers rather than floats. Using long variables and micro-size units would give 3 places roundoff keeping millis-precision untouched.

Other thing is that speed of sound changes with temperature, gets slower in the cold, faster in hot. It's not much difference over 10 degrees and it depends on how steady the temperature will be to matter at all. Otherwise a temperature sensor may be needed.

I posted the sketch / code I was using in post #6, and the Newping line is not there.

Where do I add mm and ms for distand and time in the sketch.

Thanks