please help

Hi for all,
I am a bignner user for arduino my project is to use ultrasonic sensor and buzzer to increase sound of buzzer when my hand become closer to it and decrease sound of buzzer when my hand is apart from it ,I used alot of code but all of them made sound when I put my hand and stop when I lef with out any degradation for sound according to distance
Can any one help me to write the appropriate code please
Thanks for all

Installation & Troubleshooting

For problems with Arduino itself, NOT your project

We can't see your code.

I don't think you can change the volume of a piezo buzzer. Can you? Changing the tone. Yes. But volume?

I can't think of any graceful way to change the volume of a piezo buzzer.

TheMemberFormerlyKnownAsAWOL:
We can't see your code.

This is my code
#include "Ultrasonic.h"
Ultrasonic ultrasonic(8,9);
int CMM , buzzer=12;

#define trigPin 8
#define echoPin 9
#define buzzer 10

long duration;
float distanceInch;
int timer;

void setup() {

pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(buzzer,OUTPUT);
}

void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distanceInch=duration0.0133/2;
digitalWrite(buzzer,HIGH);
delay(50);
digitalWrite(buzzer,LOW);
timer=distanceInch
10;
delay(timer);

}

DangerToMyself:
I don't think you can change the volume of a piezo buzzer. Can you? Changing the tone. Yes. But volume?

Thank you for your response I need to make as this video tutorial

I do it but the problem is sound change scoring time and repeated it'sloop regardless the distance whatever close or apart and herrecids the code
#include "Ultrasonic.h"
Ultrasonic ultrasonic(8,9);
int CMM , buzzer=12;

#define trigPin 8
#define echoPin 9
#define buzzer 10

long duration;
float distanceInch;
int timer;

void setup() {

pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(buzzer,OUTPUT);
}

void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distanceInch=duration0.0133/2;
digitalWrite(buzzer,HIGH);
delay(50);
digitalWrite(buzzer,LOW);
timer=distanceInch
10;
delay(timer);

}

I do it but the problem is sound change scoring time and repeated it'sloop regardless the distance whatever close or apart and herrecids the code

I'm guessing Google Translate barfed on that sentence, and I'm afraid I can't see from it what your problem is.

That YouTube video is trash, masquerading as informative.

AS_1as:
my project is to use ultrasonic sensor and buzzer to increase sound of buzzer when my hand become closer to it and decrease sound of buzzer when my hand is apart from it

What do you mean by "increase sound" and "decrease sound"? So far, the people replying are interpreting that to mean you want to control volume, but it's possible you mean controlling the pitch.

From your code, it appears you have an active piezo buzzer. These are the buzzers with the oscillator circuitry built in to produce a fixed frequency on the piezo. You can just set the pin HIGH to turn the buzzer on and LOW to turn it off (or vice versa depending on how you have the buzzer wired). With an active buzzers, it is not possible to control the pitch. I have managed to control the volume on these using PWM (e.g., analogWrite()). That was not an ideal solution, but it did work to some extent.

If you had a passive piezo buzzer, which are the ones you have to use tone() to get sound out of, then you can change the pitch using tone(). You can change the volume of a passive piezo using one of the Volume libraries:

I mean when the object become close to ultrasonic the sound increase or at least it's tone change to become more rapidly and the opposite when the object become apart from ultrasonic? I hope you understand what I mean and I am sorry if I can not demonstrate well.thanks

Split your problem into two tasks - taking range measurements, at no more than about 20Hz, and another task beeping at a rate that depends on the range.
Don't use the range to vary the delay between ranging measurements - that's dumb, particularly if the range is varying rapidly.

A cheap little cell phone add on speaker might be better suited for tone/volume control than a piezo buzzer.

AS_1as:
I mean when the object become close to ultrasonic the sound increase or at least it's tone change to become more rapidly and the opposite when the object become apart from ultrasonic? I hope you understand what I mean and I am sorry if I can not demonstrate well.thanks

Why do you think "sound increase" would be any more meaningful than your previous "increase sound". And "tone change to become more rapidly" also is just gibberish. I already gave you the terms you need to use. You have two options: volume or pitch. I realize English is not your native language, but that just means you need to make an extra effort to be more descriptive. if you explain what you are trying to do several times in multiple ways, even if some of the description doesn't translate well, we'll likely be able to still get it from the rest of the description. But if you only give us a few words and some of them don't make sense, then we are lost and we are forced to do a bunch of back and forth over the course of days when we could have given you an answer in a matter of minutes if you had taken a little extra time to make yourself clear on the first post. So do you really think spending a few less seconds typing is saving you time?

As for TheMemberFormerlyKnownAsAWOL's excellent suggestion, I recommend you study this tutorial for a simple demonstration of the technique you can use to avoid blocking delays in your code:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
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: