Bluetooth communication using Arduino Leonardo

Hi!

I’m working on a simple program to send and receive text between my phone and the Arduino Leonardo with a HC-05 Bluetooth module. The problem I have is that I can send text from my phone (using the app Arduino BlueControl) to the serial monitor, but not from the serial monitor to the phone. I have the Rx and Tx connected on the dedicated pins (PD0 and PD1) on the Leonardo. This is my code:

#include <SoftwareSerial.h>
 
char c = ' ';
 
// BTconnected is false when not connected and true when connected
boolean BTconnected = false;
 
// connect the STATE pin to Arduino pin 4
const byte BTpin = 3;
 
void setup() 
{
    // set the BTpin for input
    pinMode(BTpin, INPUT);   
 
    // start serial communication with the serial monitor on the host computer
    Serial.begin(9600);
    while (!Serial){}
    Serial.println("Arduino is ready");
    Serial.println("Connect the HC-05 to a device to continue");
 
    // wait until the HC-05 has made connection
    
    
    while (!BTconnected)
    {
      if ( digitalRead(BTpin)==HIGH)  
      { 
        BTconnected = true;
      };
    }
 
    Serial.println("HC-05 is now connected");
    Serial.println("");
 
    Serial1.begin(9600);
    while (!Serial1){}
}
 
void loop()
{
    // Keep reading from the HC-05 and send to Arduino Serial Monitor
    if (Serial1.available())
    {  
        c = Serial1.read();
        Serial.write(c);
    }
 
    // Keep reading from Arduino Serial Monitor input field and send to HC-05
    if (Serial.available())
    {
        c =  Serial.read();
        Serial1.write(c);
    }
}

Is there something wrong with the code?

The code does work when I use SoftwareSerial and set the Rx and Tx to some other pins.

Thanks in advance!

Edit: I’ve debugged the code using Serial.println(“test”) in the last if statement, which was printed in the serial monitor. so the serial port is available and it should execute the Serial1.write(c) line.

The code is mixed up rubbish and while you have called the Software serial library, you are not using it. This doesn't matter, you are better off without it. There is also a swag of other redundant junk, so much so that what you want to do is not entirely clear.

I am not familiar with Leonardo but the following might work

String readString;
char c;

void setup()
{
    pinMode(0, INPUT_PULLUP);
    Serial1.begin(9600);
    Serial1.println("OK then, you first, say something.....");
    Serial1.println("Go on, type something and hit Send,");
    Serial1.println("or just hit the Enter key,"); 
    Serial1.println("then I will repeat it!");
    Serial1.println("");
}

void loop() {
    while (Serial1.available()) 
  {
    delay(3);  
    c = Serial1.read();
    readString += c; 
  }// end while
  if (readString.length() >0) 
  {  
    Serial1.write(c);  
   readString="";  
  } // end if
}

Serial1 is pins 0,1.

While not that different, it is just bare bones to prove the point between phone and Arduino both ways. The serial monitor is not involved. Use a standard plain-vanilla Bluetooth terminal, and press reset on Arduino once phone connection is made.