Turn lights off and on Automatic And Manual

Hello, i was making a small project with arduino uno and HC-05 by controlling lights throw mobile app the code works fine but not as needed at manual mode pressing button it turn on and off lights fin but in automatic mode by PIR Sensor the lamps dont turn on unless i keep pressing same button after changes in PIR value here is the code please help me

int led = 13; // pin for the LED 1
int led2 = 12; // pin for the LED 2
int pir = 8;  // pin for PIR sensor1 
int pir2 = 9;  // pin for PIR sensor2
int val = 0;  // variable for reading the pir value 
int val2= 0;  // variable for reading the pir2 value 
int door=7;  // pin for PIR sensor2
int buzzer=6;  // pin for buzzer
int bluetooth;  // pin for bluetooth values
int doorval;  // variable for reading the door value
int mode=1;
void setup() { 
pinMode(buzzer,OUTPUT); // declare buzzer as output
pinMode(led, OUTPUT); // declare LED as output
pinMode(led2, OUTPUT); // declare LED as output
pinMode(pir, INPUT); // declare PIR sensor1 as input 
pinMode(pir2, INPUT); // declare PIR sensor2 as input 
pinMode(door,INPUT_PULLUP); // declare magetnic door as input
Serial.begin(9600);
} 
void loop()
{
bluetooth = Serial.read(); // Reads the data from the serial port
doorval=digitalRead(door); // read door value
 val = digitalRead(pir); // read pir 1 value 
 val2 = digitalRead(pir2); // read pir 2 value
switch(bluetooth){
case'0': if(val == HIGH){digitalWrite(led, HIGH);}
        if(val == LOW){digitalWrite(led, LOW);}
        if (val2 == HIGH) {digitalWrite(led2, HIGH);}
        if(val2==LOW) { digitalWrite(led2,LOW);}
        break;
case'1': digitalWrite(led, HIGH);
break;
case'2':digitalWrite(led, LOW);
break;
case'3': digitalWrite(led2, HIGH);
break;
case'4': digitalWrite(led2, LOW);
break;
case'5': tone(buzzer, 1000);
break;
case'6': noTone(buzzer); 
break;
      
}
}

in mode 0 the led should turn on and off by motion

I can't promise that this will fix the issue, but there is something there... you perform a Serial.read() before any check with Serial.available() to see if any has in fact arrived.

Not a problem, but...

case'0': if(val == HIGH){digitalWrite(led, HIGH);}
        if(val == LOW){digitalWrite(led, LOW);}
        if (val2 == HIGH) {digitalWrite(led2, HIGH);}
        if(val2==LOW) { digitalWrite(led2,LOW);}
        break;

What was wrong with...

case'0':
  digitalWrite(led, val);
  digitalWrite(led2, val2);
  break;

?

aarg:
I can't promise that this will fix the issue, but there is something there... you perform a Serial.read() before any check with Serial.available() to see if any has in fact arrived.

it didnt fix it :S

pcbbc:
Not a problem, but...

case'0': if(val == HIGH){digitalWrite(led, HIGH);}

if(val == LOW){digitalWrite(led, LOW);}
        if (val2 == HIGH) {digitalWrite(led2, HIGH);}
        if(val2==LOW) { digitalWrite(led2,LOW);}
        break;




What was wrong with...


case'0':
  digitalWrite(led, val);
  digitalWrite(led2, val2);
  break;



?

pcbbc:
Not a problem, but...

case'0': if(val == HIGH){digitalWrite(led, HIGH);}

if(val == LOW){digitalWrite(led, LOW);}
        if (val2 == HIGH) {digitalWrite(led2, HIGH);}
        if(val2==LOW) { digitalWrite(led2,LOW);}
        break;




What was wrong with...


case'0':
  digitalWrite(led, val);
  digitalWrite(led2, val2);
  break;



?

the problem is i have to keep sending 0 to the arduino to apply changes in PIR sensors but i must send 0 once and when it detect motion it lights up by itself

deathlock1998:
in mode 0 the led should turn on and off by motion

You are not using Serial correctly. The line

bluetooth = Serial.read();

will get a new value every time loop() repeats so it will not stay at '0' for any appreciable length of time.

You should use the value received from serial to set a variable that determines the state of the system. That way the state can stay constant even if nothing is sent to serial, and if the wrong value is sent accidentally it can be ignored. Something like

case '0':
    systemState = 'A'; // A for automatic
    break;
case '1';
   systemState = 'N'; // N for on
   break;

and then something like this

if (systemState == 'A') {
    if(val == HIGH) {
         digitalWrite(led, HIGH);
    }
    if(val == LOW) {
         digitalWrite(led, LOW);
    }
    if (val2 == HIGH) {
         digitalWrite(led2, HIGH);
     }
    if(val2==LOW) { 
         digitalWrite(led2,LOW);
    }
}

...R

Robin2:
You are not using Serial correctly. The line

bluetooth = Serial.read();

will get a new value every time loop() repeats so it will not stay at '0' for any appreciable length of time.

You should use the value received from serial to set a variable that determines the state of the system. That way the state can stay constant even if nothing is sent to serial, and if the wrong value is sent accidentally it can be ignored. Something like

case '0':

systemState = 'A'; // A for automatic
    break;
case '1';
  systemState = 'N'; // N for on
  break;



and then something like this


if (systemState == 'A') {
    if(val == HIGH) {
        digitalWrite(led, HIGH);
    }
    if(val == LOW) {
        digitalWrite(led, LOW);
    }
    if (val2 == HIGH) {
        digitalWrite(led2, HIGH);
    }
    if(val2==LOW) {
        digitalWrite(led2,LOW);
    }
}




...R

Same Problem , i did exactly what you said

deathlock1998:
Same Problem , i did exactly what you said

You need to post the program in which you tried it. There may be some small error either in your implementation or my suggestion. The devil is in the detail.

...R

You need to post the program in which you tried it. There may be some small error either in your implementation or my suggestion. The devil is in the detail.

...R

int led = 13; // pin for the LED 1
int led2 = 12; // pin for the LED 2
int pir = 8;  // pin for PIR sensor1 
int pir2 = 9;  // pin for PIR sensor2
int val = 0;  // variable for reading the pir value 
int val2= 0;  // variable for reading the pir2 value 
int door=7;  // pin for PIR sensor2
int buzzer=6;  // pin for buzzer
int bluetooth=0;  // pin for bluetooth values
int doorval;  // variable for reading the door value
char mode;
void setup() { 
pinMode(buzzer,OUTPUT); // declare buzzer as output
pinMode(led, OUTPUT); // declare LED as output
pinMode(led2, OUTPUT); // declare LED as output
pinMode(pir, INPUT); // declare PIR sensor1 as input 
pinMode(pir2, INPUT); // declare PIR sensor2 as input 
pinMode(door,INPUT_PULLUP); // declare magetnic door as input
Serial.begin(9600);
} 
void loop()
{
if(Serial.available()){;
bluetooth = Serial.read(); // Reads the data from the serial port
doorval=digitalRead(door); // read door value
switch(bluetooth){
case'0': mode='a';
        break;
case'1': mode='b';
break;
case'2':mode='c';
break;
case'3': mode='d';
break;
case'4': mode='e';
break;
case'5': mode='f';
break;
case'6': mode='g'; 
break;
}
if(mode=='a'){
 if(digitalRead(pir) == HIGH){
 digitalWrite(led, HIGH);}
 if(digitalRead(pir) == LOW){
 digitalWrite(led, LOW);}
if(digitalRead(pir2) == HIGH){
 digitalWrite(led2, HIGH);}
 if(digitalRead(pir2) == LOW){
 digitalWrite(led2, LOW);}
}

else if (mode=='b')
digitalWrite(led, HIGH);
else if (mode=='c')
digitalWrite(led, LOW);
else if (mode=='d')
digitalWrite(led2, HIGH);
else if (mode=='e')
digitalWrite(led2, LOW);
else if (mode=='f')
tone(buzzer, 1000);
else if (mode=='g')
noTone(buzzer);





}
}

Before you post code the next time please use the AutoFormat tool to lay it out for easier reading.

I suspect the main problem is this line

if (Serial.available() ) { ;

which should not have a semi-colon

and its corresponding closing } which should be after the end of the SWITCH block.

You have everything inside inside the if (Serial

...R

What he/she said.

Plus you totally missed the point about my previous comment. But wrt the way you have it coded now...

if(digitalRead(pir) == HIGH){
 digitalWrite(led, HIGH);}
 if(digitalRead(pir) == LOW){
 digitalWrite(led, LOW);}
if(digitalRead(pir2) == HIGH){
 digitalWrite(led2, HIGH);}
 if(digitalRead(pir2) == LOW){
 digitalWrite(led2, LOW);}

All that can be a much more simply written as...

digitalWrite(led, digitalRead(pir));
digitalWrite(led2, digitalRead(pir2));

You are just copying the state of the pir inputs to the led outputs. No need for all the if shenanigans....

Robin2:
Before you post code the next time please use the AutoFormat tool to lay it out for easier reading.

I suspect the main problem is this line

if (Serial.available() ) { ;

which should not have a semi-colon

and its corresponding closing } which should be after the end of the SWITCH block.

You have everything inside inside the if (Serial

...R

Sorry my knowledge is weak i dont know what is AutoFormat,and i removed the semi-colon same error :frowning:

deathlock1998:
Sorry my knowledge is weak i dont know what is AutoFormat,and i removed the semi-colon same error :frowning:

Please post the entire updated sketch.

deathlock1998:
Sorry my knowledge is weak i dont know what is AutoFormat,

In the Arduino IDE select Tools/Auto Format

...R

pcbbc:
What he/she said.

Plus you totally missed the point about my previous comment. But wrt the way you have it coded now...

if(digitalRead(pir) == HIGH){

digitalWrite(led, HIGH);}
if(digitalRead(pir) == LOW){
digitalWrite(led, LOW);}
if(digitalRead(pir2) == HIGH){
digitalWrite(led2, HIGH);}
if(digitalRead(pir2) == LOW){
digitalWrite(led2, LOW);}




All that can be a much more simply written as...


digitalWrite(led, digitalRead(pir));
digitalWrite(led2, digitalRead(pir2));



You are just copying the state of the pir inputs to the led outputs. No need for all the if shenanigans....

i wrote what you told me in the previous comment but my laptop restarted and didnt save the code what i pasted here was the last saved code , i wont ignore any advise and it was a good one made most of the code simple

int led = 13; // pin for the LED 1
int led2 = 12; // pin for the LED 2
int pir = 8;  // pin for PIR sensor1
int pir2 = 9;  // pin for PIR sensor2
int val = 0;  // variable for reading the pir value
int val2 = 0; // variable for reading the pir2 value
int door = 7; // pin for PIR sensor2
int buzzer = 6; // pin for buzzer
int bluetooth = 0; // pin for bluetooth values
int doorval;  // variable for reading the door value
char mode;
void setup() {
 pinMode(buzzer, OUTPUT); // declare buzzer as output
 pinMode(led, OUTPUT); // declare LED as output
 pinMode(led2, OUTPUT); // declare LED as output
 pinMode(pir, INPUT); // declare PIR sensor1 as input
 pinMode(pir2, INPUT); // declare PIR sensor2 as input
 pinMode(door, INPUT_PULLUP); // declare magetnic door as input
 Serial.begin(9600);
}
void loop()
{
 if (Serial.available()) {
   bluetooth = Serial.read(); // Reads the data from the serial port
   doorval = digitalRead(door); // read door value
   switch (bluetooth) {
     case'0': mode = 'a';
       break;
     case'1': mode = 'b';
       break;
     case'2': mode = 'c';
       break;
     case'3': mode = 'd';
       break;
     case'4': mode = 'e';
       break;
     case'5': mode = 'f';
       break;
     case'6': mode = 'g';
       break;
   }
   if (mode == 'a') {
     digitalWrite(led, digitalRead(pir));
     digitalWrite(led2, digitalRead(pir2));
   }
   else if (mode == 'b')
     digitalWrite(led, HIGH);
   else if (mode == 'c')
     digitalWrite(led, LOW);
   else if (mode == 'd')
     digitalWrite(led2, HIGH);
   else if (mode == 'e')
     digitalWrite(led2, LOW);
   else if (mode == 'f')
     tone(buzzer, 1000);
   else if (mode == 'g')
     noTone(buzzer);
 }
}

Here is the last update

You have not moved the closing } for the if (Serial.available()) { so it comes after the SWITCH block

...R

Robin2:
You have not moved the closing } for the if (Serial.available()) { so it comes after the SWITCH block

...R

I checked it the closing of if serial is at the end of the program

What are all those "mode" flags for? You don't do anything with them except select some action on them once thereafter. Why not just ditch them and perform the desired action directly?

    switch (bluetooth) {
      case'0':
       digitalWrite(led, digitalRead(pir));
       digitalWrite(led2, digitalRead(pir2));
        break;

      case'1':
       digitalWrite(led, HIGH);
        break;
...

aarg:
What are all those "mode" flags for? You don't do anything with them except select some action on them once thereafter. Why not just ditch them and perform the desired action directly?

    switch (bluetooth) {

case'0':
      digitalWrite(led, digitalRead(pir));
      digitalWrite(led2, digitalRead(pir2));
        break;

case'1':
      digitalWrite(led, HIGH);
        break;
...

I did that , but in comments someone told me that value of bluetooth is reset every time loop starts so he suggested that advice and i tried it but same results i dont know whats wrong . When l removed the manual script it works automatically fine with no problem but when i combine manual and automatic the problem appears