Motion sensor and LDR to control LED. Please help to spot the mistake made in an Arduino coding

Hellooo, just another beginner trying out coding. I am building a project which uses a motion sensor and LDR to turn off/on my LED upon receiving a signal "a". Everything works fine except the PIR which cannot detect any motion on the serial monitor and is unable to lit the LED as well when there's motion. Attached are my Arduino code and the result on the serial monitor. Can anyone help please

I have tested the PIR alone and it is working fine.

char incoming_value  = 0;

int ledPin = 13;
int ledPin2 = 12;
int pirsensor = 2;

int calibrationTime = 30;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(pirsensor, INPUT);
  pinMode(A0, INPUT);

  Serial.println("Waiting for the sensor to warm up.");
  delay(calibrationTime * 1000); // Convert the time from seconds to milliseconds.
  Serial.println("SENSOR ACTIVE");

}

//LDR CODE

void loop()
{

  if (Serial.available())
  {
    Serial.println(analogRead(A0));
    pirsensor = digitalRead(2);
    Serial.println(digitalRead(2));
    delay(300);

    char incoming_value = Serial.read();
    Serial.println(incoming_value);
    
    if (incoming_value == 'a')
    {
      while ( analogRead(A0) > 0  ) 
      {
        if (analogRead(A0) > 400 && pirsensor == HIGH)
      {
        digitalWrite(ledPin, HIGH);
        digitalWrite(ledPin2, HIGH);
        Serial.println("Sky is dark and motion is detected");

      }
      else if (analogRead(A0) > 400 && pirsensor == LOW)
      {
        digitalWrite(ledPin, LOW);
        digitalWrite(ledPin2, LOW);
        Serial.println("Sky is Dark and no motion is detected");
      }

      else if (analogRead(A0) < 400 && pirsensor == LOW)
      {
        digitalWrite(ledPin, LOW);
        digitalWrite(ledPin2, LOW);
        Serial.println("Sky is bright and no motion is detected");
      }

      else if (analogRead(A0) < 400 && pirsensor == HIGH)
      {
        digitalWrite(ledPin, LOW);
        digitalWrite(ledPin2, LOW);
        Serial.println("Sky is bright and motion is detected");
      }
     }
    }
  }
}

type or paste code here

All experienced programmers makes code for more less every part of the project. When that works it verifies both proper coding and that the devices are functional.

Make a sketch that only exercises the PIR and makes You know that device well.
Do the same for just LED and serial monitor.

Hi @lemonss
test your modified code

char incoming_value  = 0;
int ledPin = 13;
int ledPin2 = 12;
int pirsensor = 2;
int calibrationTime = 30;
//----------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(pirsensor, INPUT);
  pinMode(A0, INPUT);
  Serial.println("Waiting for the sensor to warm up.");
  delay(calibrationTime * 1000); // Convert the time from seconds to milliseconds.
  Serial.println("SENSOR ACTIVE");
}
//----------------------------------------------
void loop()     //LDR CODE
{
  if (Serial.available())
  {
    char incoming_value = Serial.read();
    Serial.println(incoming_value);
  }
  Serial.println(analogRead(A0));
  pirsensor = digitalRead(2);
  Serial.println(digitalRead(2));
  delay(300);

  if (incoming_value == 'a')
  {
    if (analogRead(A0) > 400 && pirsensor == HIGH)
    {
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin2, HIGH);
      Serial.println("Sky is dark and motion is detected");
    }
    else if (analogRead(A0) > 400 && pirsensor == LOW)
    {
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin2, LOW);
      Serial.println("Sky is Dark and no motion is detected");
    }
    else if (analogRead(A0) < 400 && pirsensor == LOW)
    {
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin2, LOW);
      Serial.println("Sky is bright and no motion is detected");
    }
    else if (analogRead(A0) < 400 && pirsensor == HIGH)
    {
      digitalWrite(ledPin, LOW);
      digitalWrite(ledPin2, LOW);
      Serial.println("Sky is bright and motion is detected");
    }
    incoming_value == ' ';
  }
}

As long as this while loop is executed pirsensor will never change because you are not re-reading it.

while ( analogRead(A0) > 0 )

If analogRead(A0) ever equals to 0 then the loop will be exited and you will have to enter 'a' again to restart the process. Is that what you actually want?

Hi thank you for responding.

The reason I used the && is that I want the 2 conditions to be met before giving an output to the LED.

Everything works initially such that the serial monitor is able to project the 4 different outputs until I started to combine and control it with an app which in this case I am using the MIT app inventor. I use the character "a" as a signal from the MIT app and send it to my Bluetooth Arduino to activate the "AUTO" mode button such that it is able to automatically on and off the LED by itself with the use of motion and LDR sensor.

Hello
Did you run a PIR sensor tutorial to see how this sensor works?

Hi, Thank you. I have tested the code. It is able to print the value of the PIR and LDR. However, it does not go into the 2nd "if " condition such that it did not print the current condition like "Sky is dark/bright and motion is detected" etc etc. LED could not lit up too :frowning:

Yes, I have tried it, it works when there is a change in temperature which would give an output "1" or when there is no motion it will output "0". So in this case when the threshold value which I set for my LDR is > 400 ( meaning night time ) & detected motion = 1. My LED would turn on automatically and vice versa. All of this work without the app. But I wanted to design smart lighting automation where the "automation" starts throughout the day after clicking on the "AUTO" button which I created on the MIT App inventor.

Thank you all for the input! The problem is solved by using the value read by the PIR instead of 'pirsensor'. :slight_smile: