PIR trouble

Can anyone help make the duration of the buzzer go forever. It wants to stop after about three seconds with this code, but I would like it to go until my keypad shuts it off.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {8, 7, 6};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char PIN[4]= {'1','2','3','4'};
char attempt[4]={0,0,0,0};
int z=0;
int inputPin = 10;
int pirState = LOW;
int val = 0;
int pinSpeaker = 9;

void setup(){
pinMode(pinSpeaker,OUTPUT);
pinMode(inputPin, INPUT);
Serial.begin(9600);
}

void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
void correctPIN()
{
digitalWrite(pinSpeaker, LOW);
Serial.println("Correct pin entered");
}

void incorrectPIN()
{
digitalWrite(pinSpeaker, HIGH);
Serial.println("Incorrect pin entered");
}

void checkPIN()
{
int correct=0;
int i;
for ( i = 0; i <4; i++ )
{
if (attempt_==PIN*)_
_
{_
_
correct++;_
_
}_
_
} if (correct==4)_
_
{_
_
correctPIN();_
_
}_
_
else*_
* {*
* incorrectPIN();*
* }*
* for (int zz=0; zz<4; zz++)*
* {*
* attempt[zz]=0;*
* }*
}
void readKeypad()
{
_ char key =keypad.getKey();
* if (key!=NO_KEY)
_
{*

* switch(key)*
* {*
_ case'':_
_
z=0;_
_
break;_
_
case '#':_
_
delay(100);_
_
checkPIN();_
_
break;_
_
default:_
_
attempt[z]=key;_
_
z++;_
_
}_
_
}_
_
}_
void loop()
_
{_
_
val = digitalRead(inputPin); _
_
if (val == HIGH) { _
_
playTone(300, 160);_
_
delay(150);_
_
readKeypad();_
_
}_
_
{_
_
playTone(0,0);_
_
delay(300); _
_
}_
_
}_
_
[/quote]*_

I think you want to change the while loop condition in the playTone() function. Here's what you have:

void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

Try using this:

void playTone(long duration, int freq) {
  duration *= 1000;
  int period = 1000000L / freq;
  unsigned long start = millis();
  while (millis() - start < duration) {
    digitalWrite(pinSpeaker, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pinSpeaker, LOW);
    delayMicroseconds(period / 2);
   }
}

Note: I haven't tried this on my board, but I'm pretty sure it works :wink:

Thank you. And do you know how I would make it stop buzzing using the keypad. When I type in the keypad code the buzzer won't stop.

And do you know how I would make it stop buzzing using the keypad. When I type in the keypad code the buzzer won't stop.

You haven't shown us the code, or explained what you enter in the keypad, so, no we can't help you.