PIR and LDR dependent light circuit not working as intended.

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
int buzzer = 2;
int led1 = 13; // LED for notificatoin and buzzer (red)
int led2 = 6; // LDR activated LED (blue)
int led3 = 9; // LDR && PIR activated LED (white)
int PIR = 4;
const int light = A1; // LDR pin
int lightValue; // for LDR state
int pirValue = 0; // for PIR state
int state = LOW;

void setup()
{
  rf_driver.init();
  pinMode(buzzer, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(PIR, INPUT);
  pinMode(light, INPUT);
}
void loop()
{
  uint8_t buf[9];
  uint8_t buflen = sizeof(buf); //end of rf setup

  lightValue = analogRead(light); // setting up value for LDR
  pirValue = digitalRead(PIR); // setting up state of PIR


  if (rf_driver.recv(buf, &buflen)) // Non-blocking
  {
    for (int i = 0; i < 50; i++) {
      tone( buzzer, 3400, 250);
      digitalWrite(led1, HIGH);
      delay(500);
      tone( buzzer, 2950, 250);
      digitalWrite(led1, LOW);
      delay(500);
    }
  }

  if ((lightValue < 300) && (state == LOW))
  {
    digitalWrite(led2, HIGH);
    state = HIGH;
    delay(5000);
    digitalWrite(led2, LOW);
  }
  if ((lightValue < 300) && (pirValue == HIGH))
  {
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
    delay(100); 
  }
  if ((state == HIGH) && (lightValue >=300))
  {
    state = LOW;
  }
  else
  {
    digitalWrite(buzzer, 0);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    delay(200);
  }
}

That’s the complete code I uploaded. It’s intended for three functions; to buzz and blink led1 when rf signal is received, to temporarily turn on led2 for 5 seconds when light level falls (non-repeating), and to turn on led2 and led3 if PIR is HIGH and LDR is below threshold. But now LED2 and LED3 turn on randomly when LDR is below threshold, but do not respond to PIR in anyway. Please help. :slight_smile:

You should Serial.print() messages to Serial Monitor so you can see the internal state of your system. It could be that your hardware is not producing the values you expect.