Hi,
I'm new to programming with arduino uno and hc-05. I can switch on/off with androind phone via hc-05.
But I have a problem with PIR. Firstly I'll switch on LED and if the sensor detects motion it will turn off the lamp but PIR don't switch off the led. My code is as follows and what is the problem, please help me.
I'm sending "b" to switch on LED and "d" to switchoff from app.
Thx.
char Incoming_value = 0;
int pin = 7;
int value;
void setup()
{
Serial.begin (9600);
pinMode(13, OUTPUT);
pinMode(pin, INPUT);
}
void loop(){
value = digitalRead(pin);
Serial.println(value);
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if (Incoming_value == 'b'){
Serial.print(value);
while (digitalRead(pin) == 0){
digitalWrite(13, HIGH);
}
}else{ if (Incoming_value == 'd')
digitalWrite(13, LOW);}
}
}
What are you trying to make it do? Right now, if a 'b' is received, it will keep the LED on as long as the PIR input is LOW.
Can you please describe exactly how you want the program to behave? In some sense I see that either the serial or the PIR should change the LED, but in what way? For example, what if serial says to turn it on, and PIR says turn it off? Who should win? Do you want it to remember its state, for example you set the LED on, it stays on until something else changes? If so, you need to add another variable and some tests for that.
Here I rewrote it not knowing the answers to those questions, but perhaps a skeleton that you could work with more easily:
char Incoming_value = 0;
int pin = 7;
int value;
void setup() {
Serial.begin (9600);
pinMode(13, OUTPUT);
pinMode(pin, INPUT);
}
void loop() {
// handle serial input
Serial.println(value);
if (Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
if (Incoming_value == 'b') {
value = 1;
digitalWrite(13, HIGH);
} else {
if (Incoming_value == 'd')
{
value = 0;
digitalWrite(13, LOW);
}
}
}
// handle PIR
value = digitalRead(pin);
Serial.print(value);
if (value == 0) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
}