Piezo Speaker and Coding Problems

Hello, I am trying to make a device that makes a sound when an object comes near it. I am using 5 ultrasonic sensors, one active buzzer, and an arduino due to make this. I know you can't use tone with the due but since i'm using an active buzzer i can jest send any electric impulse to the buzzer which is what i did. The problem is that all the buzzer does, when i upload, is make a continuous noise and the ultrasonic sensors do nothing. Can someone look over the code please, i think that's what's causing it.

Here's the code:

// distance sensor components
const int trigPin = 13;
const int echoPin = 12;
const int trigPin1 = 11;
const int echoPin1 = 10;
const int trigPin2 = 9;
const int echoPin2 = 8;
const int trigPin3 = 7;
const int echoPin3 = 6;
const int trigPin4 = 5;
const int echoPin4 = 4;

// distace sensor data
long duration;
int distance;
long duration1;
int distance1;
long duration2;
int distance2;
long duration3;
int distance3;
long duration4;
int distance4;

// piezo buzzer variable
const int pezbuz = 2;

void setup()
{
Serial.begin(9600); // Starts the serial communication
pinMode(trigPin, OUTPUT);// Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode (pezbuz, OUTPUT); // sets the piezo buzzer as an output
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // converting the distance to centimeters.

digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1;

digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;

long duration3, distance3;
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
duration3 = pulseIn(echoPin3, HIGH);
distance3 = (duration3 / 2) / 29.1;

digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
duration4 = pulseIn(echoPin4, HIGH);
distance4 = (duration4 / 2) / 29.1;

if (distance <= 10)
{
digitalWrite (pezbuz, HIGH);
}

if (distance1 <= 10)
{
digitalWrite (pezbuz, HIGH);
}

if (distance2 <= 10)
{
digitalWrite (pezbuz, HIGH);
}

if (distance3 <= 10)
{
digitalWrite (pezbuz, HIGH);
}

if (distance4 <= 10)
{
digitalWrite (pezbuz, HIGH);
}
}

When do you ever turn the buzzer off?

You never write the buzzerpin LOW.

You could solve the problem with one "if-else" statement for all the sensors.
Note the "or" sign between the distance tests.

if (distance <= 10 || distance1 <= 10 || distance2 <= 10 || distance3 <= 10 || distance4 <= 10) digitalWrite(pezbuz, HIGH);
else digitalWrite(pezbuz, LOW);

Leo..

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Thank you leo bi tried your code but the same problem still prevails if there are any other suggestions that would be much appreciated thank you very much.

And Tom I will make sure to do that thank you.

khaker:
Thank you leo bi tried your code but the same problem still prevails if there are any other suggestions that would be much appreciated thank you very much.

And Tom I will make sure to do that thank you.

Further suggestions would be pointless unless you post your revised code, in code tags as explained above.

Revised code:

// distance sensor components
const int trigPin = 13;
const int echoPin = 12;
const int trigPin1 = 11;
const int echoPin1 = 10;
const int trigPin2 = 9;
const int echoPin2 = 8;
const int trigPin3 = 7;
const int echoPin3 = 6;
const int trigPin4 = 5;
const int echoPin4 = 4;

// distace sensor data
long duration;
int distance;
long duration1;
int distance1;
long duration2;
int distance2;
long duration3;
int distance3;
long duration4;
int distance4;

// piezo buzzer variable
const int pezbuz = 2;

void setup()
{
  Serial.begin(9600); // Starts the serial communication
  pinMode(trigPin, OUTPUT);// Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode (pezbuz, OUTPUT); // sets the piezo buzzer as an output
}

 
void  loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds (5);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1; // converting the distance to centimeters.

  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds (5);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1 / 2) / 29.1;

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds (5);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 / 2) / 29.1;

  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds (5);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3 / 2) / 29.1;

  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds (5);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4 / 2) / 29.1;

if (distance <= 10 || distance1 <= 10 || distance2 <= 10 || distance3 <= 10 || distance4 <= 10)
  digitalWrite(pezbuz, HIGH);
else 
 digitalWrite (pezbuz, LOW);
}

aarg:
Further suggestions would be pointless unless you post your revised code, in code tags as explained above.

aarg:
Further suggestions would be pointless unless you post your revised code, in code tags as explained above.

Add a Serial.println to the end of each sensor block, so you can see which sensor(s) trigger the buzzer.
Leo..

Wawa:
Add a Serial.println to the end of each sensor block, so you can see which sensor(s) trigger the buzzer.
Leo..

Okay thanks but where will it print to?

khaker:
Okay thanks but where will it print to?

Print all the distances to the serial monitor, to see what the problem is.

To avoid problems like this, start with one sensor.
When that works, add another one.
If that works too, then add the rest.
Leo..

Thank you will do and just to make sure, this is correct right:

// distance sensor components
const int trigPin = 13;
const int echoPin = 12;
const int trigPin1 = 11;
const int echoPin1 = 10;
const int trigPin2 = 9;
const int echoPin2 = 8;
const int trigPin3 = 7;
const int echoPin3 = 6;
const int trigPin4 = 5;
const int echoPin4 = 4;

// distace sensor data
long duration;
int distance;
long duration1;
int distance1;
long duration2;
int distance2;
long duration3;
int distance3;
long duration4;
int distance4;

// piezo buzzer variable
const int pezbuz = 2;

void setup()
{
  Serial.begin(9600); // Starts the serial communication
  pinMode(trigPin, OUTPUT);// Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode (pezbuz, OUTPUT); // sets the piezo buzzer as an output
}

 
void  loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds (5);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1; // converting the distance to centimeters.
  Serial.println

  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds (5);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1 / 2) / 29.1;
  Serial.println

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds (5);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 / 2) / 29.1;
  Serial.println

  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds (5);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3 / 2) / 29.1;
  Serial.println

  digitalWrite(trigPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin4, HIGH);
  delayMicroseconds (5);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4 / 2) / 29.1;
  Serial.println  

if (distance <= 10 || distance1 <= 10 || distance2 <= 10 || distance3 <= 10 || distance4 <= 10)
  digitalWrite(pezbuz, HIGH);
else 
 digitalWrite (pezbuz, LOW);
}

Serial.println(distance);

and

Serial.println(distance1);

at the end of the second block.

etc.
Leo..

Wawa:
Serial.println(distance);

and

Serial.println(distance1);

at the end of the second block.

etc.
Leo..

Update: it is currently working nearly perfectly thank you so much for the help. Just one more thing though, since i say digitalwrite low if there's nothing in front of the sensor it is constantly emitting this high pitch sound, is there any way to make it do nothing if there's nothing in front of the sensor?

What are the values you see.

You have told the program to sound the buzzer if any of them is equal to or less than 10.
Leo..

Wawa:
What are the values you see.

You have told the program to sound the buzzer if any of them is equal to or less than 10.
Leo..

It's all good now thank you.