using if statement

I tried to do project using PIR sensor and light sensor for these conditions

  1. If the room is empty, the lights should be off regardless of ambient light.
  2. If the room becomes occupied and the lights are off and the ambient light is high the lights stay off.
  3. If the room becomes occupied and the ambient light is low the lights go on.
  4. If the lights are on, the ambient lighting is ignored and rule 1 is applied.

I wrote this code but it doesn't work

can anyone help

int ledpin = 4; // the LED int SENSORL = 12; // light sensor int SENSORO = 13; // ocuapency sensor

void setup() { pinMode(ledpin, OUTPUT); // declare LED as output pinMode(SENSORL, INPUT); // declare light sensor as input pinMode(SENSORO, INPUT); // declare ocuapnecy sensor as input

Serial.begin(9600); }

void loop(){

if ( (SENSORO = LOW) && (SENSORL = LOW)){ digitalWrite(ledpin, LOW); Serial.println("No Motion detected"); }

else if ( (SENSORO = HIGH) && (SENSORL = HIGH)){ digitalWrite(ledpin, LOW); Serial.println( "Motion detected but the romm has light"); } else if ( (SENSORO = HIGH) && (SENSORL = LOW)){ digitalWrite(ledpin, LOW); Serial.println( "Motion detected"); } else if (SENSORL = HIGH); { digitalWrite(ledpin, LOW); Serial.println( "NO Motion detected"); } }

= is not the same as ==

if ( (SENSORO = LOW) && (SENSORL = LOW)){ Should be if ( (SENSORO == LOW) && (SENSORL == LOW)){

Assignment = vs equality ==.

And once you are done with that fix there are 0 chances they would be low given they are just PIN numbers and not the result of a digitalRead() call on those pins...

A best practice is to use the word pin in the name of the constants (so best declared as const byte representing PIN numbers so then the test becomes obvious you are not comparing a state of a pin with HIGH or LOW

const byte ledPin = 4;               // the LED is connected to pin 4
const byte lightSensorPin = 12;      // light sensor is connected to pin 12
const byte occupancySensorPin = 13;  // occupancy sensor is connected to pin 13

how about now?

const byte ledPin = 4; // the LED is connected to pin 4 const byte lightSensorPin = 12; // light sensor is connected to pin 12 const byte occupancySensorPin = 13; // occupancy sensor is connected to pin 13 int state = LOW; int val = 0; int val1 = 0;

void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(lightSensorPin, INPUT); // declare light sensor as input pinMode(occupancySensorPin, INPUT); // declare ocuapnecy sensor as input

Serial.begin(9600); }

void loop(){ val = digitalRead(lightSensorPin); val1 = digitalRead(occupancySensorPin);

if ( (val == LOW) && (val1 == LOW)){ digitalWrite(ledPin, LOW); if (state == LOW) { Serial.println("No Motion detected"); state = LOW; } delay(100); }

else if ( (val == HIGH) && (val1 == HIGH)){ digitalWrite(ledPin, LOW); Serial.println( "Motion detected but the romm has light"); delay(200); } else if ( (val == HIGH) && (val1 == LOW)){ digitalWrite(ledPin, HIGH); if (state == LOW) { Serial.println( "Motion detected"); state = HIGH; delay(300); } } else if (lightSensorPin == HIGH); { digitalWrite(ledPin, LOW); Serial.println( "NO Motion detected"); delay(400); } }

Check out the red parts here

else if ([color=red]lightSensorPin[/color] == HIGH)[color=red];[/color] {

Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Use better variable names than "val" and "val1". Hard to remember which is light level and which is motion.

In this section

    else if ( (val == HIGH) && (val1 == LOW)){
    digitalWrite(ledPin, HIGH);
    if (state == LOW) {
    Serial.println( "Motion detected");
    state = HIGH;

it looks like if the light level is HIGH and NO motion detected, then turn on the LED. Shouldn't you turn on the LED when the light level is LOW and motion IS detected?

What happens here

 if ( (val == LOW) && (val1 == LOW)){
  digitalWrite(ledPin, LOW);
  if (state == LOW) {
   Serial.println("No Motion detected");
   state = LOW;

when "state" is HIGH after the light is turned on and the room is empty? I see.

I did some change but still doesn't work

const byte ledPin = 4;               // the LED is connected to pin 4
const byte lightSensorPin = 12;      // light sensor is connected to pin 12
const byte occupancySensorPin = 13;  // occupancy sensor is connected to pin 13
int state = LOW;
int val = 0;
int val1 = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(lightSensorPin, INPUT);     // declare light sensor as input
  pinMode(occupancySensorPin, INPUT);     // declare ocuapnecy sensor as input


  Serial.begin(9600);
}

void loop() {

  if ( (lightSensorPin == LOW) && (occupancySensorPin == LOW)) {
    digitalWrite(ledPin, LOW);
    if (state == LOW) {
      Serial.println("No Motion detected");
      state = LOW;
    }
    delay(100);
  }

  else if ( (lightSensorPin == HIGH) && (occupancySensorPin == HIGH)) {
    digitalWrite(ledPin, LOW);
    Serial.println( "Motion detected but the romm has light");
    delay(200);
  }
  else if ( (lightSensorPin == HIGH) && (occupancySensorPin == LOW)) {
    digitalWrite(ledPin, HIGH);
    if (state == LOW) {
      Serial.println( "Motion detected");
      state = HIGH;
      delay(300);
    }
  }
  else if (lightSensorPin == HIGH); {
    digitalWrite(ledPin, LOW);
    Serial.println( "NO Motion detected");
    delay(400);
  }
}

You might want to try a 'digitalRead()' on these pins somewhere in loop(). ;)

lightSensorPin

occupancySensorPin

How are they wired?

Gee.... Seems my post 2 and 4 were really useful for you... please read them again

if ( (lightSensorPin == LOW) && (occupancySensorPin == LOW)) {

I thought that renaming your variables and explaining why hoping that a const PIN number will NEVER be high or low but will stay just the PIN number would be enough to get you to think about it... apparently not

So again read and understand the previous post... try again