Need help in using PIR sensor

hi

need help in my project,

when the password is entered the door will open using a servo and the servo will continuously triggered based on PIR sensor, the problem is that i dont know how to stop PIR sensor using WHILE loop, is there anyone that can help me

here is the code

void with_keypad_and_servo(){
 
  String mastercode = "696969"; // the password
  String enteredcode = ""; // the code you enter
  String reason = "   INCORRECT CODE   "; // the default reason why entry failed 
  // read a 6 digit code (you can have any length)
  for (int i = 0; i < 6; i++){
    char key = keypad.waitForKey();
    if(key == '#'){ // # is used to cancel
      i = 6; //skip to the end
      reason = "      Cancelled     "; // set the other reason why entry failed. You pressed # to cancel if you pressed a wrong number
    }
    if(key != NO_KEY){ // if you aren't reading no keypress
      lcd.print("*"); // print a # to the LCD so people don't see the number but you know you pressed a button
      enteredcode += key; // append the keypress to the end of the entered code string
    }
  }
 
  if (enteredcode == mastercode) // if the code is correct
  {
    lcd.setCursor (0, 2);
    lcd.print("                    ");
    lcd.setCursor (0, 2);
    lcd.print("    CODE CORRECT    "); // print a success message
    digitalWrite(A0, HIGH); // turn on the green LED
    myservo.write(0); // move the servo 180 degrees and open the door lock
    // make 3 beeps
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
    delay (3000); // wait 3 seconds while the door is opened
    digitalWrite(A0, LOW); // turn off the LED
    myservo.write(179); // close the door lock
    //reset the LCD for the next user
    lcd.clear();
    lcd.home();
      DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);


    lcd.setCursor(0, 1);
       lcd.print("Enter(9 digit) Pass:");
    lcd.setCursor(0, 3);
     lcd.print("Press(#)Cancel Pass");
    lcd.setCursor(0, 2);
  }
  
  else {  // if the code is wrong or cancelled
    // tell the user what happened
    lcd.setCursor (0, 2);
    lcd.print("                    ");
    lcd.setCursor (0, 2);
    lcd.print(reason); // display the reason for failure. Incorrect code by default unless cancel is pressed
    digitalWrite(A1, HIGH); // turn on the red LED
    buzz(10, 2500, 500); // make a long fail beep
    delay (3000); // wait 3 seconds before allowing a retry
    digitalWrite(A1, LOW); // turn off LED
    // reset the LCD for the next try
    lcd.clear();
    lcd.home();
   DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);


    lcd.setCursor(0, 1);
    lcd.print("Enter(9 digit) Pass:");
    lcd.setCursor(0, 3);
     lcd.print("Press(#)Cancel Pass");
    lcd.setCursor(0, 2);

  }
  
  
 

    

  while(enteredcode == mastercode) {
  PIR_sensor();
    
        
  }
  
 
 
}



void PIR_sensor(){

val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(A0, HIGH);   // turn LED ON
    delay(1000);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
      
      
      myservo.write(0); // move the servo 180 degrees and open the door lock
    // make 3 beeps
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
      
      
    }
  } 
  else {
      digitalWrite(A0, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      myservo.write(179);
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    
  }
  
  
  
  
  
  
}

hi,

I need help in my project using PIR sensor controlled by a keypad, the problem is i dont know how stop PIR sensor using while loop can anyone help me?

here is the code

while(enteredcode == mastercode) {
  PIR_sensor();
    
        
  }



void PIR_sensor(){

val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(A0, HIGH);   // turn LED ON
    delay(1000);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
      
      
      myservo.write(0); // move the servo 180 degrees and open the door lock
    // make 3 beeps
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
    delay(200);
    buzz(10, 2500, 100);
      
      
    }
  } 
  else {
      digitalWrite(A0, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      myservo.write(179);
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
    
  }
  
  
  
  
  
  
}

Let PIR_sensor() return something and check it in the while loop.

int somevalue = 3456;
while (enteredcode==mastercode && somevalue==3456)
{
  somevalue = PIR_sensor();
}

I'm not sure why you're calling PIR_sensor() in a while loop. That might be clear if you post your complete code and provide a description why you used the approach and what you're trying to achieve. Whatever you want to do, it can more than likely be done in the loop() without the need for while.

You have an infinite loop. It will stop when something inside it changes enteredcode to not equal mastercode anymore. Since nothing in the while loop changes enteredcode, that would be never.

Please see the answers on your other thread here:

To be of help, we need to see code that has compiled. Your code doesn't have the basic elements defined (e.g., setup() and loop()) nor are any of the variables defined. These are really minimums that must be present before we can be of much help.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.
Threads merged.

  • Moderator