How to make buzzer sound while doing other stuff

I am able to make a buzzer sound by using digitalWrite, and basically setting it high, delaying a few microseconds, and then setting it low and delaying the number of microseconds I calculate that I need for the desired pitch. My problem is, how would I do something else while making the buzzer buzz? For example, if I wanted to make the buzzer buzz at a frequency proportional to the value of an analog input? Any help would be great! Thanks!!

http://arduino.cc/en/Reference/Tone

Don't use delay ().

The buzzers I have, will buz when a voltage is applied (no need for a frequency), so if yours are the same, you can turn on the data pin, and later turn it off when you are done listening to the buzzer.

Now if you have a speaker, it's a different story.

Did you know you can play a song using your buzzer, jackwp ?

@mas,
No I didn't know you could play a song using a buzzer.
I did know you could play a tune, but never heard a buzzer singing before. Did you?
This thread did not indicate that they wanted a "song" or a "tune", so I used the KIS principle to answer the
question : "How to make the buzzer sound while doing other stuff".
Did I answer that question wrongly? If so, I am sorry.

You answered the question but forgot the part where OP mentioned the word "pitch" and "frequency" in his explanation of the question.

It seemed to me that the OP (what's OP?) did not know that their buzzer would buzz without modulating it from arduino. Now they know that it will (if it is like my buzzers). KIS
We need not get into a pissing contest. We are both right. (or maybe we are both wrong), no matter.

OP is a common short for Original Poster or thread starter.
By Keeping It Simple, you might assume people know what you know.
And assumptions are "dangerous".
The way to the answer was given in the first reply and the OP (chris_ )explained that he was able to produce a beep but not the tone he wants.

I'll have another piss now and turn to bed in a moment as it's 1 am here, goodnight.

OP = Original Poster.

The one who started the thread.

Thanks Nick,
Great, I learned what OP is, and hopefully did not piss of Mas.
Have not heard from the OP, maybe sleep time, or they have a life, more outgoing than mine. LOL

I am just not sure if you want to play a tune, or just buzz. Just buzz is a lot easier.

Hi everyone thanks for all the replies haha. I can clarify what I want to do exactly. I have a buzzer and an ultrasonic sensor and I would like to vary the pitch of the buzzer based on the distance an object is from the sensor. I know how to read the data from the sensor through an analog input to determine the distance, but I want to know how to make the pitch of the buzzer be proportional to that distance. So when I move my hand away from the sensor the pitch goes up and vice versa

Coding Badly has mentioned the tone() function.

tone(2,1000); // outputs a 1KHz tone out on pin 2
tone(2,2000); // outputs a 2KHz tone out on pin 2
etc.

so: if a value X is between 2 values say 10cm to 15cm use tone() to generate a 1KHZ o/p
if a value X is between 2 values say 16cm to 20cm use tone() to generate a 2KHz o/p
etc.

Sorry, for my first reply being out of sync (I must have missed part of the original post).
I agree with the others about using tone().
You could derive a number from the distance, and use in the tone()
such as tone(2,distance * 300);

chris_:
Hi everyone thanks for all the replies haha. I can clarify what I want to do exactly. I have a buzzer and an ultrasonic sensor and I would like to vary the pitch of the buzzer based on the distance an object is from the sensor. I know how to read the data from the sensor through an analog input to determine the distance, but I want to know how to make the pitch of the buzzer be proportional to that distance. So when I move my hand away from the sensor the pitch goes up and vice versa

To me a buzzer is a device that buzzes when a voltage is applied to it. As such, its pitch cannot be varied. What is this device that you refer to as a buzzer ?