laser tripwire piezo siren

I’m a 2 week old Arduino owner and i am just loving this thing. I am still new to electronics, however I have been able to complete a number of projects with research from the net. My latest project, laser and photocell tripwire with a piezo siren has pretty much been successful, but I am still a bit confused on how to properly operate the piezo siren. I have the siren making sound, but I want to change the sound it makes to sound more like a siren. How do I change the frequency. Can anyone give me some pointers pls. I sorta copy pasted the part of the code for the buzzer so im not really sure what it means. Could someone explain the code a bit for me please?
Im using:
102dB Piezo Siren Model #: 273-079 from radio shack
operates at 6-12V, 2000-4500Hz
It’s connected to arduino via 330 ohm resistor.

here is the code:

int sensePin = 0;
int buzzPin = 9;
int ledPin = 10;


void setup() {
    analogReference(DEFAULT);
    
    Serial.begin(9600);
    delay(100);
  


    pinMode(buzzPin, OUTPUT);
    pinMode(ledPin, OUTPUT);

}

void loop() {

    Serial.println(analogRead(sensePin));

    int val = analogRead(sensePin);

    if(val < 350) for (long i = 0; i < 4420 * 2; i++ )
 
    // 1 / 2048Hz = 488uS, or 244uS high and 244uS low to create 50% duty cycle
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(244);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(244);
    
    if(val < 350) digitalWrite(ledPin, HIGH);
  }

Any help would be truly appreciated! :smiley:

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

Thanks for those links they were very informative. The code I had was right I just needed to tweak it, which I still don't fully understand how to do. I tried the tone example and got a bunch of muffled scratchy sounds. From what I have read, I have to oscillate the piezo just right. I also learned about Npn transistors. Since the arduino can only output 5v, using transistor to switch on a 9v battery gave much better results from the piezo siren and the original code I had. Now I just need to figure out how frequencies work so I can tweak how the siren sounds. For now its loud and works so that's good. One other problem I can't figure out is how to make the siren sound for an elapsed time once the beam is broken.

    if(val < 350) for (long i = 0; i < 4420 * 2; i++ )
 
    // 1 / 2048Hz = 488uS, or 244uS high and 244uS low to create 50% duty cycle
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(244);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(244);

The body of the for loop is this statement:
digitalWrite(buzzPin, HIGH);
Why you need to set that pin HIGH 8,840 times, then delay, then set it LOW once, escapes me.

Missing curlies in your code:

    if (val < 350) { 
      for (long i = 0; i < 4420 * 2; i++ ) {
        // 1 / 2048Hz = 488uS, or 244uS high and 244uS low to create 50% duty cycle
        digitalWrite(buzzPin, HIGH);
        delayMicroseconds(244);
        digitalWrite(buzzPin, LOW);
        delayMicroseconds(244);
      }
    }

I'm actually very new to electronics as far as circuits, and programming. My expertise lies in building desktop Pc's and keeping them running. So as far as what the code does I am not sure but I think, and correct me if I'm wrong, that this part,

digitalWrite(buzzPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(500);

,is what causes the disc in the piezo to oscillate. The " for (long i = 0; i , ...etc " code I think is for the frequency. I just pasted that in from some other sketch, so I'm not entirely sure on its function. All I know is that it works. If there is a better way to code this please let me know. But what I need is that delay. As of now, if someone were to run past the beam it only sounds the siren for a split second. which isn't really an alarm lol. I'd like to set to at least 10-15 seconds. eventually I want to hook up a camera and maybe a keypad to the system. If anyone has any good links or advice or any more info about the code would help.

So as far as what the code does I am not sure but I think, and correct me if I’m wrong, that this part…is what causes the disc in the piezo to oscillate.

Yes, and no. That code makes the piezo oscillate once. However the for loop you have, with { } braces added for emphasis looks like:

for (long i = 0; i < 4420 * 2; i++ )
{
    // 1 / 2048Hz = 488uS, or 244uS high and 244uS low to create 50% duty cycle
    digitalWrite(buzzPin, HIGH);
}
    delayMicroseconds(244);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(244);

Since you did not use curly braces, the compiler assumed that they are where I put them in this snippet.

Compare that with the location that Chagrin showed them, and you’ll see the problem (I hope).

From reading the comments on the vendor's site, the device has a built-in oscillator. Give it power, it makes noise. Cut power, it stops. I don't think you will be able to easily change the frequency but you may be able to change the volume by varying the voltage.

OK so I figured it out. With the red wire on the siren connected to the collector on the npn and the ground wire connected to the 9v positive, and new code with the curlys fixed, if beem is broken I get a loud solid tone for the number seconds specified in the code "for(long i = 0; i < 2048 * 3; i++ ) { " which is set to 3 seconds for now. I can also hear tones with the melody sketch. This was what I wanted, however, its seems that now the polarities are reversed. If i connect it the old way I had it, with red wire from siren connected to 9v + and black connected to the collector, and use my original code, the siren sounds like a… well a siren. example. http://www.youtube.com/watch?v=2mTidjpLSeA . except mine sounds like it needs to be tuned. its not as smooth as the example. Also with this set up the duration code doesn’t work. if I change 3 it actually changes how fast the siren pulses, opposed to how long its triggered. what is going on here? Perhaps one way uses the internal oscillator and the other disables it, or is it just malfunctioning? I think there may be 2 ways to set this up.

Here is the schematic for those that may be interested in starting this project for themselves. Any advice on circuit improvement will be most welcome.

What current is needed by the laser module?