Hi,
I'm new with arduino programming and all that stuff but managed to make a project in which i want to unlock a door with my android phone.So far everything worked perfectly until i wanted to add the auto lock/unlock feature.
I made the android app send the commands for lock and unlock(manually pressing buttons),they work perfectly.It reconnects automatically when the app is started to the bluetooth module(HC-05).
My main concern is that the STATE pin returns only '0' value altho the bluetooth module is connected with my phone. I have no clue why or what my mistake is on the coding part involving arduino.
I want to use the STATE pin as a value that will be used by the Android app when i activate auto lock/unlock feature.
I used the " Serial.println(digitalRead(BTpin));" command to see what the pin STATE returns,that is the only reason it is included in the command without a specified statement attached to it.
I did a ton of searching on the internet about how to code it right and so on but nothing worked since STATE pin only returns the value '0'.Now i am confused and i have no clue what worked or what might have worked.
What i am trying to make arduino on a logical thinking: "If the bluetooth module is connects to the phone unlock the door, when the connection is lost lock the door." On a more detailed level i will make the app send a value/string(whatever) when in auto mode, which arduino will read it and unlock the door,but arduino will be the one checking if the connection is still available and if not it will lock the door.
#include <SoftwareSerial.h>
#define ledPin 7
#define autoPin 8
#define BTpin 12
SoftwareSerial BTserial(2, 3);
int state = 0;
void setup()
{
pinMode(BTpin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0)
{
state = Serial.read();
}
if (state == '0')
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
Serial.println("Locked");
Serial.println(digitalRead(BTpin));
state = 0;
}
else if (state == '1')
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
Serial.println("Unlocked");
Serial.println(digitalRead(BTpin));
state = 0;
}
}