RX TX Problem. Bluetooth HC-05

I have set up an UNO with a HC-05 bluetooth module on a test bed and can get everything working as required. (LED on/off, relay board on/off etc)
I have written an Android app using MIT App Inventor and have no problem controlling the UNO.

My problem is sending text back to the app from the UNO.
The following section of code, when used with two buttons on the app, turns an LED on/off but the text sent back to the UNO is always one step behind.
This means that when I first press the ON button on the app the LED lights but the text box stays clear. Then if I press the OFF button the LED turns off but the text box reads "LED ON" and so on.

if (state == '0') {
Serial.println("LED OFF");
digitalWrite(ledPin, LOW);
state = 0;

}

else if (state == '1') {
Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
state = 0;

How is the HC-05 connected to the Uno ?

Please post entire sketch.

Through pins 0 RX and 1 TX


#include <Wire.h>
#define ledPin 13

int state = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
}


void loop() {
  if (Serial.available() > 0) { // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
  }

  if (state == '0') {
    Serial.println("LED OFF");
    digitalWrite(ledPin, LOW); // Turn LED OFF
    state = 0;
  }

  else if (state == '1') {
    Serial.println("LED ON");
    digitalWrite(ledPin, HIGH);
    state = 0;

  }
}

I think that a sensible first move would be to put the HC-05 on a SoftwareSerial port so that you can use the Serial monitor for debugging

I think that a sensible first move would be to put the HC-05 on a SoftwareSerial port so that you can use the Serial monitor for debugging

+1.

Here is my test code for a Bluetooth module connected to an Uno with software serial. Pin 4 (Uno RX) to HC05 TX and pin 7 (Uno TX) to HC05 RX through a voltage divider.

//Sends from serial monitor to the Bluetooth device app
// receives from the Bluetooth device app

#include <SoftwareSerial.h>
SoftwareSerial BTserial(4, 7); // RX | TX

void setup()
{
   Serial.begin(9600);
   BTserial.begin(9600);
}

void loop()
{
   if (Serial.available())
   {
      BTserial.print(char(Serial.read()));
   }
   if (BTserial.available())
   {
      Serial.print(char(BTserial.read()));
   } 
}

I use Bluetooth serial terminal on my Android tablet for testing.

Thankyou. I have tested this code and I can communicate between the two serial monitors with the bonus of not having to disconnect the TX/RX wires before uploading a sketch.
I just need to work out how I can adapt my Android app to work now

Why any need to change the Android app ?

It will send and receive data in just the same way as it always did as it is agnostic as to where the data is coming from or going to

I cant get it to do anything

If I press one of the app buttons all I get is the value on the serial monitor. The LED does not change

Post your new code. Post what the app is sending and what you are receiving in serial monitor. It may be that the app is sending invisible characters (line feed, carriage return)?

We cannot help without information.

The app just sends "1" (on) or "0" off
The serial monitor shows 1 or 0
The code is exactly as I posted earlier but with the addition of your lines to move to pins 4 and 7

Right, but does it also send a terminator, like line feed and/or carriage return. '0' is not equal to "0/n" or "0\r\n".

Put this line Serial.print(BTserial.read(), HEX); in place of the Serial.print(BTserial.read()); and post the result that you see in serial monitor when you press the '0' and '1' buttons in the app.

Both buttons give FFFFFFFF

My conclusion would be that either the app is not sending what you think it is or that there is an error in your code

Please post the code as it is now

//Sends from serial monitor to the Bluetooth device app
// receives from the Bluetooth device app

#include <SoftwareSerial.h>
SoftwareSerial BTserial(4, 7); // RX | TX

#define ledPin 13

int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
  BTserial.begin(9600);
}


void loop() {
  if (Serial.available()==0 ) { // Checks whether data is comming from the serial port
    state = BTserial.read(); // Reads the data from the serial port
  }

  if (state == '0') {
  //  Serial.println("LED OFF");
    Serial.println(BTserial.read(), HEX);
    digitalWrite(ledPin, LOW); // Turn LED OFF
    state = 0;
  }

  else if (state == '1') {
  //  Serial.println("LED ON");
        Serial.println(BTserial.read(), HEX);
    digitalWrite(ledPin, HIGH);
    state = 0;

  }
}
if (Serial.available()==0 ) { // Checks whether data is comming from the serial port
    state = BTserial.read(); // Reads the data from the serial port

You are testing the hardware serial input and if nothing is available you go ahead and read from Bluetooth. That makes no sense

Test whether anything is available from Bluetooth and if so, read it

if (Serial.available()==0 ) If I omit the ==0 then nothing happens until I send a value above 0 from the serial monitor

You need to test whether BTserial has something available not Serial