How To Read Sensor And Control Lights With Serial Port ?

hi everyone ,
i am trying to read the sensor and also controlling the led bulbs with serial port but i am having a issue when i am embedding the sensor code and serial port code sensor stop working . Please help if anybody can and here is my code .
Thanks In advance

int room1 = 12;
int room2 = 11;
int room3 = 10;
int room4 = 9;
int val = 0;
int SensorVal = 0 ;

void setup() {
  pinMode(room1, OUTPUT);
    pinMode(room2, OUTPUT);
      pinMode(room3, OUTPUT);
       pinMode(room4, OUTPUT);
       pinMode (3, INPUT);//pin 9 reads the high signal from the PIR
  Serial.begin(9600);
}

void loop() {

  while (Serial.available() == 0);
  int val = Serial.read() - '0';

  if(val == 1)
  {
  digitalWrite(room1 , LOW);
  digitalWrite(room2 , LOW);
  digitalWrite(room3 , LOW);
  digitalWrite(room4 , LOW);
    
  }
  else if (val == 1)
  {
    Serial.println("Room one Led IS OFF");
    digitalWrite(room1 , HIGH);
  } else if (val == 2)
  {
    Serial.println("Room one Led Is ON");
    digitalWrite(room1 , LOW);
  }
  
  
  if (val == 3)
  {
    Serial.println("Control room LED is OFF");
    digitalWrite(room2 , HIGH);
  } else if (val == 4)
  {
    Serial.println("Control room LED Is ON");
    digitalWrite(room2 , LOW);
  }
  
  
  if (val == 5)
  {
    Serial.println("Room 2 LED OFF");
    digitalWrite(room3 , HIGH);
  } else if (val == 6)
  {
    Serial.println("Room 2 LED Is ON");
    digitalWrite(room3 , LOW);
  }
  
  if (val == 7)
  {
    Serial.println("Room 3 LED OFF");
    digitalWrite(room4 , HIGH);
  } else if (val == 8)
  {
    Serial.println("Room 3 LED Is ON");
    digitalWrite(room4 , LOW);
  }
  
  SensorVal = digitalRead(3);//Pin 3 is initiallly 0
 
    if (SensorVal == 1){
    digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
    
    }else if (SensorVal == 0){
    digitalWrite(2, LOW);   // turn the LED on (HIGH is the voltage level)
    
    }

  
  
}

Thank you for attaching your code.

Do you know about using CTRL T to format your code?
IMO place { an } on a separte line.

This is blocking, is this what you want:
while (Serial.available() == 0);

You have:
if(val == 1)
and
else if (val == 1)
Is this what you want?

When SensorVal is not 1 then it has to be 0:
if (SensorVal == 1) {
digitalWrite(2, HIGH) ; // turn the LED on (HIGH is the voltage level)

}else if (SensorVal == 0) {
digitalWrite(2, LOW) ; // turn the LED on (HIGH is the voltage level)
Watch comments.

These can be, const byte
int room1 = 12;
int room2 = 11;
int room3 = 10;
int room4 = 9;

Thank you for replying .
And yes exactly what i want but when i am trying to do this sensor stops working and individually both of thing light and sensor works properly what would i do . ?

if(val == 1)
  {
  digitalWrite(room1 , LOW);
  digitalWrite(room2 , LOW);
  digitalWrite(room3 , LOW);
  digitalWrite(room4 , LOW);
    
  }
  else if (val == 1)
  {
    Serial.println("Room one Led IS OFF");
    digitalWrite(room1 , HIGH);
  }

Your 1st “else if” will never happen as the “if” statement is validating to the same thing
i.e. val==1

Sound Confusing Can you please explain it .
Thanks in advance .

while (Serial.available() == 0) ;
This line will cause your code to be in a tight loop waiting for a character from the serial port.
Nothing else happens (except interrupts) while you are in this loop.

Because of the above, you do realize that:
SensorVal = digitalRead(3) ;
Only happens after a character is received.
Then nothing until the next character comes in etc.

if(val==1)
{

}

else if(val==1)
{

}

When val is = 1 then the top {} code is executed.

The else if {} does not happen since the top if statement was already true.

@Ali_Mastoi

Let us step back a bit.

Assume no serial input is received.
Tell us what happens below:

while (Serial.available() == 0);
int val = Serial.read() - '0';

.

Now tell us what this does:

if (Serial.available() > 0 )
{
int val = Serial.read() - '0';
}

Now Tell us what happens here:

  if (Serial.available() > 0)
  {
    int val = Serial.read() - '0';

    if(val == 1)
    {
      digitalWrite(room1 , LOW);
      digitalWrite(room2 , LOW);
      digitalWrite(room3 , LOW);
      digitalWrite(room4 , LOW);

    }

//    else if (val == 1)
//    {
//      Serial.println("Room one Led IS OFF");
//      digitalWrite(room1 , HIGH);
//    } 

    else if (val == 2)
    {
      Serial.println("Room one Led Is ON");
      digitalWrite(room1 , LOW);
    }


    else if (val == 3)
    {
      Serial.println("Control room LED is OFF");
      digitalWrite(room2 , HIGH);
    } 

    else if (val == 4)
    {
      Serial.println("Control room LED Is ON");
      digitalWrite(room2 , LOW);
    }

    else if (val == 5)
    {
      Serial.println("Room 2 LED OFF");
      digitalWrite(room3 , HIGH);
    } 

    else if (val == 6)
    {
      Serial.println("Room 2 LED Is ON");
      digitalWrite(room3 , LOW);
    }

    else if (val == 7)
    {
      Serial.println("Room 3 LED OFF");
      digitalWrite(room4 , HIGH);
    } 

    else if (val == 8)
    {
      Serial.println("Room 3 LED Is ON");
      digitalWrite(room4 , LOW);
    }
  } //END of     if(Serial.available() > 0)

thanks for the reply sir .
i have tried your code that you have posted above is working properly but i have another thing also a sensors when i an merging the lights and sensor code my sensor stops working :frowning: .

here is the video link watch this then u will better understand what kind of system i am trying to make .
link : Arduino Home Security System - YouTube
but i dont want the lcd , camera . and keypad just sensor and lights .

thanks for your support .