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.
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.
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;
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;
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.
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);
}
}
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);
}
}
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?
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?
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