Glitch in Object detecting goggles
Posted: Tue Jan 28, 2025 3:07 pm
Hello, I have a question regarding the object-detecting goggles you discussed on your website. So, I made the circuit and everything works alright but the code is not working well. Because sometimes even when no object is present, the buzzer makes small beeping noises. I have put in the direct code from your website; i.e. copying and pasting it onto the Arduino IDE. I've also checked with the serial monitor but to no avail.
if you could send me a detailed explanation of what went wrong or how to fix it it would be deeply appreciated)
The code I used from your website:
/*
Code to make a buzzer beep faster as an object gets closer to an ultrasonic distance sensor
For instructions and circuit diagram see:
https://www.sciencebuddies.org/science- ... king-stick
*/
// constant variables for pins
const int trigPin = 6;
const int echoPin = 7;
const int buzzerPin = 11;
int threshold = 30; // threshold distance to activate buzzer
// buzzer delay variables
int buzzerDelay;
int buzzerDelayMin = 10;
int buzzerDelayMax = 250;
long duration; // ultrasonic return pulse duration
long cm; // distance in centimeters
void setup() { // setup code that only runs once
// set pin modes
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(buzzerPin,OUTPUT);
// uncomment the next line to user serial print for debugging
// Serial.begin(9600);
}
void loop() {
// send trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// read echo pulse
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// control the buzzer
if(cm<threshold){ // if an object is closer than the threshold
// map the distance to the buzzer delay time
buzzerDelay = map(cm,0,threshold,buzzerDelayMin,buzzerDelayMax);
// beep the buzzer
digitalWrite(buzzerPin,HIGH);
delay(buzzerDelay);
digitalWrite(buzzerPin,LOW);
delay(buzzerDelay);
}
else{ // if an ob ject is farther away than the threshold
digitalWrite(buzzerPin,LOW); // turn buzzer off
}
// uncomment the next line for debugging
// Serial.println(cm);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}
( In the image with my circuit shown, I haven't turned it on now and though I understand its on a more mounted surface even on a flat or better one the reaction is the same. Which is the slight buzzing on the sensor even with nothing there.)
--------------------------------
[Administrator note: Project url: https://www.sciencebuddies.org/science- ... king-stick
Also, you noted you were including the code here and didn't. I have pasted it in from your email.]
if you could send me a detailed explanation of what went wrong or how to fix it it would be deeply appreciated)
The code I used from your website:
/*
Code to make a buzzer beep faster as an object gets closer to an ultrasonic distance sensor
For instructions and circuit diagram see:
https://www.sciencebuddies.org/science- ... king-stick
*/
// constant variables for pins
const int trigPin = 6;
const int echoPin = 7;
const int buzzerPin = 11;
int threshold = 30; // threshold distance to activate buzzer
// buzzer delay variables
int buzzerDelay;
int buzzerDelayMin = 10;
int buzzerDelayMax = 250;
long duration; // ultrasonic return pulse duration
long cm; // distance in centimeters
void setup() { // setup code that only runs once
// set pin modes
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(buzzerPin,OUTPUT);
// uncomment the next line to user serial print for debugging
// Serial.begin(9600);
}
void loop() {
// send trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// read echo pulse
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// control the buzzer
if(cm<threshold){ // if an object is closer than the threshold
// map the distance to the buzzer delay time
buzzerDelay = map(cm,0,threshold,buzzerDelayMin,buzzerDelayMax);
// beep the buzzer
digitalWrite(buzzerPin,HIGH);
delay(buzzerDelay);
digitalWrite(buzzerPin,LOW);
delay(buzzerDelay);
}
else{ // if an ob ject is farther away than the threshold
digitalWrite(buzzerPin,LOW); // turn buzzer off
}
// uncomment the next line for debugging
// Serial.println(cm);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}
( In the image with my circuit shown, I haven't turned it on now and though I understand its on a more mounted surface even on a flat or better one the reaction is the same. Which is the slight buzzing on the sensor even with nothing there.)
--------------------------------
[Administrator note: Project url: https://www.sciencebuddies.org/science- ... king-stick
Also, you noted you were including the code here and didn't. I have pasted it in from your email.]