I am using this code to make a buzzer go off when a PIR sensor is tripped by movement and I would like the keypad to shut the buzzer off when a code is typed in, however it does not want to work. Can anybody help, it is much appreciated.
Thank you for your help, and that doesn't seem to work either. The keypad still doesn't stop the light from lighting. Also for some reason the serial won't tell me when I have typed in the correct pin or incorrect pin.
Do you know that delays and while loops will make the arduino wait until... And while it's busy waiting, it cannot do anything else.
playTone(300, 160);
This will play a tone for 300 seconds. The playTone function that you use, is a blocking function (it's using a while loop until time elapsed). So during these 300 seconds, it cannot do anything else. That mean, the call to readKeypad() will be executed after the 300 seconds has elapsed.
And what's the point of all those calls to delay() ?
I decided just to cut out the playTone function since I was only wanting the buzzer to buzz constantly. Instead I used this code and it works sometimes, but for some reason, sometimes my Arduino won't read my keypad. Do you know why?
You have to realize that you are trying to do two things at once here, handling the sensor and handling the keypad. That disqualifies from using the delay() function (something that should be avoided anyway..)
The solution to these kinds of problems is to design a separate statemachine for each task. I have attached these state diagrams.
These state machines can then be easily coded (i do not have a keypad hooked so im using serial but it should be easy to modify):
const int speaker = 9;
const int sensor = 10;
unsigned long alarmMark;
enum {idle, buzz, noBuzz} alarmState;
enum {codeOk, key, noKey} codeState;
const char secretCode[] = {1, 2, 3, 4};
byte codeIdx;
void setup(){
 pinMode(speaker, OUTPUT);
 pinMode(sensor, INPUT_PULLUP);
 Serial.begin(115200);
 Serial.println("Armed");
 alarmState = idle;
 codeState = codeOk;
}//setup()
void loop(){
 alarm();//run alarm sm
 code();//run code sm
}//loop()
void alarm(){
//Â Serial.print("alarmState: ");
//Â Serial.println(alarmState);
 switch(alarmState){
  case idle:
   if(!digitalRead(sensor)){
    alarmState = buzz;//change state on next call
    tone(speaker, 160);
    alarmMark = millis();//remember time
    codeState = noKey;//signal code request
    Serial.println("Alarm");
   }//digitalRead(sensor)
   break;
  case buzz:
   if(millis()-alarmMark >= 300){//been here for 300ms
    alarmState = noBuzz;//change state next call
    //Serial.println("mute");
    noTone(speaker);
   }//if(t=300)
   if(codeState == codeOk){//enetered code is valid
    alarmState = idle;//change state on next call
    Serial.println("code ok");
    noTone(speaker);
   }//if(codeOk)
   break;
  case noBuzz:
   if(millis()-alarmMark >= 150){//been here for 150ms
    alarmState = buzz;//change state on next call
    tone(speaker, 160);
    //Serial.println("sound");
   }//if(t=300)
   if(codeState == codeOk){
    alarmState = idle;
    Serial.println("code ok");
   }//if(codeOk)
   break;
 }//switch(alarmState)
}//alarm()
void code(){
 switch(codeState){
  case noKey:
   if(Serial.available()) codeState = key;//wait for key
   break;
  case key:
   if(Serial.read()-'0' == secretCode[codeIdx]) codeIdx++;//check code, advance if valid key
   else codeIdx = 0;//reset if invalid key
   codeState = noKey;//back to waiting state
   if(codeIdx == 4) codeState = codeOk;//signal valid code and stop checking
 }//switch(codeState)
}//code()
#include <SM.h>
const int speaker = 9;
const int sensor = 10;
SM alarm(aIdle);
SM code(cCodeOk);
const char secretCode[] = {1, 2, 3, 4};
byte codeIdx;
void setup(){
 pinMode(speaker, OUTPUT);
 pinMode(sensor, INPUT_PULLUP);
 Serial.begin(115200);
 Serial.println("Armed");
}//setup()
void loop(){
 EXEC(alarm);//run alarm sm
 EXEC(code);//run code sm
}//loop()
State aIdle(){
 if(!digitalRead(sensor)){//sensor is active low
  alarm.Set(aBuzz);//change state on next call
  tone(speaker, 160);
  code.Set(cNoKey);//signal code request
  Serial.println("Alarm");
 }//digitalRead(sensor)
}//aIdle()
State aBuzz(){
 if(alarm.Timeout(300)){//been here for 300ms
  alarm.Set(aNoBuzz);//change state next call
  noTone(speaker);
 }//if(t=300)
}//aBuzz()
State aNoBuzz(){
 if(alarm.Timeout(150)){//been here for 150ms
  alarm.Set(aBuzz);//change state on next call
  tone(speaker, 160);
  //Serial.println("sound");
 }//if(t=300)
}//alarm()
State cCodeOk(){
//do aboluteley nothing
}//cCodeOk()
State cNoKey(){
   if(Serial.available()) code.Set(cKey);//wait for key
}//cNoKey()
State cKey(){
 if(Serial.read()-'0' == secretCode[codeIdx]) codeIdx++;//check code, advance if valid key
 else codeIdx = 0;//reset if invalid key
 code.Set(cNoKey);//back to waiting state
 if(codeIdx == 4){
  code.Set(cCodeOk);//stop checking
  alarm.Set(aIdle);
  Serial.println("code ok");
 }//if(codeok)
}//cKey()