Arduino Program for Bluetooth

Good day! I am using HC-05 together with arduino .
My goal is to print “AT” on the serial monitor without typing it and if it replied OK, it will produce an OUTPUT in the pin 13(where i put my LED). What’s the problem in the code? Thanks in advance!

#include <SoftwareSerial.h>
SoftwareSerial BTSerial = SoftwareSerial(10, 11); // RX | TX
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
digitalWrite(9, HIGH);
pinMode(13,OUTPUT);
Serial.begin(9600);
Serial.println(“Enter AT commands:”);
BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.println(“AT\r\n”);
delay(300);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
Serial.write(BTSerial.read());

// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
BTSerial.write(Serial.read());
BTSerial.println(“AT\r\n”);
delay(300);
if (BTSerial.read() == ‘OK’)
digitalWrite(13, HIGH); delay(300);

}

if (BTSerial.read() == 'OK')

Single quotes are for SINGLE characters. Please post a picture of your keyboard, with the ONE key that you pressed to get the ONE character in the single quotes circled.

The read() method returns ONE character. The ONE character will NEVER equal 'OK'.

PaulS Thanks for your response sir. If it is not possible to read a string of characters using the read() method, is there any function that will allow to do so? Thanks.

Im thinking if this will work, the logic will look like this Code:if (BTSerial.read() == 'O','K')

If it is not possible to read a string of characters using the read() method, is there any function that will allow to do so?

The Serial object is an instance of the HardwareSerial class, which derives from the Stream class, which is documented in the reference section. There are several methods that might be useful.

The usual method, before all that hand-holding stuff was added, was to read and store data until the correct number of characters, or some expected delimiter, was received, adding a NULL to the end of the array after each character. Then, when the array was known to be the proper length (because the delimiter said "hey, there's no more coming"), then strcmp() could be used.

Im thinking if this will work, the logic will look like this Code:if (BTSerial.read() == 'O','K')

I'm thinking "No!".

Very basic serial code for controlling the arduino LED.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

Thank you for your responses. Im just a rookie at arduino and im having a hard time with this auto response by the serial monitor.
@PaulS thanks, im getting your point, i will try to compose my code using that function and will post if i have figured out how it works.
@zoomkat i think that would help me a lot!
Will update if i had modify it correctly!
Best regards everyone!

I have figured out that when a bluetooth is in AT mode , it will not return a string, or the function of getting characters as response from the serial monitor is not possible. Am i right. Please correct me if im wrong. Somebody told me that the 'OK' response in the serial monitor which i should read, is in other form of data like hexa or binary. If true, how can i create that code abling me to read that data from the serial. Thanks!!!.

Am i right.

No.

If true, how can i create that code abling me to read that data from the serial.

The data that arrives on the serial port is bytes. Whether those bytes represent characters or binary data, they are still bytes.

Print each byte received using Serial.print(theByte, HEX); followed by Serial.print(" "); Show us the actual output.