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);
}
}