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 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 . ?
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.
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 .
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 .