Uno to Mega Bluetooth link doesn't work

Have been using Uno to Uno without problems. Now I need a Uno linked by Bluetooth to a Mega. Can’t see why it doesn’t work.

Here is the Uno code for an HC05 set up as master:

// Basic Uno Master sketch for bluetooth link

byte a = 0;

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
//Uno D2 to HC-05 TX o/p
//Uno D3 to HC-05 RX i/p through a 3:2 voltage divider

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

void loop()
{
  while (BTserial.available())
  {
    a = BTserial.read();
    Serial.print(char(a));
  }
  if ( a == 65)
  {
    Serial.println("   Transmitting");
    BTserial.write(66);
  }
  while (BTserial.available() == 0) //Waits until data is received
  {
  }
}

Here is the Uno slave code:

//Basic Uno Slave Sketch for bluetooth link
byte a;
#include <SoftwareSerial.h> // For BT comms
SoftwareSerial BTserial(2, 3); // RX | TX
// D2 Uno to HC-05 TX o/p
// D3 Uno to HC-05 Rx i/p through a 3:2 voltage divider

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

void loop()
{
  BTserial.print('A');//Request data by sending a character
  while (BTserial.available() == 0)
  {
  }
  while (BTserial.available())//If there are stored bytes
  {
    a = BTserial.read();
    Serial.print(char(a));
    Serial.println("   Receiving");
  }
}

The slave sends an ‘A’ to trigger data from the master, in this case a ‘B’. It works.
Here is the slave Mega code:

// Basic Slave Mega sketch for bluetooth link
byte a;
unsigned long Loop_Start;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //C18 Mega Rx1 to HC-05 Tx o/p
  //C19 Mega Tx1 to HC-05 Rx i/p via 3:2 divider
}
void loop()
{
  Loop_Start = millis();
  delay(100);
  Serial1.print('A');//Request data by sending a character
  Serial.print('A');
  while (Serial1.available() == 0)
  {
    if (millis() > (Loop_Start + 1000))//Breaks 'while' loop every second
    {
      break;
    }
  }
  while (Serial1.available())//If there are stored bytes
  {
    delay(100);
    a = Serial1.read();
    Serial.print(char(a));
    Serial.println("   Receiving");
  }
}

It doesn’t work and I can’t see why. I put in the 1 second break to get the Mega slave to keep trying. Have tried Serial.print and write, also bytes only, no difference.
Probably me, any ideas?
Peter

//C18 Mega Rx1 to HC-05 Tx o/p

Pin 18 on a Mega is Tx1

...R

You are right, but the note in the sketch is wrong, sorry about that. I connected Tx18 to the Rx input of the HC-05, not 19. Error in the note, not the connection. As there is often confusion between Tx and Rx, I checked it out by using a simple routine to cause the Mega to transmit intermittently. A meter on the pin showed the transmission.

When the Uno master and Mega slave are run together, the master receives 'A' about once in ten tries. Otherwise the bytes it picks up are random but in the range 200-255, no idea why.

Peter

Don't want to confuse things any more than I have already. Here is the revised Mega slave sketch:

// Basic Slave Mega sketch for bluetooth link with corrected HC-o5 connections
byte a;
unsigned long Loop_Start;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //C18 Mega Tx1 to HC-05 Rx i/p via 3:2 divider
  //C19 Mega Rx1 to HC-05 Tx o/p
 
}
void loop()
{
  Loop_Start = millis();
  delay(100);
  Serial1.print('A');//Request data by sending a character
  Serial.print('A');
  while (Serial1.available() == 0)
  {
    if (millis() > (Loop_Start + 1000))//Breaks 'while' loop every second
    {
      break;
    }
  }
  while (Serial1.available())//If there are stored bytes
  {
    delay(100);
    a = Serial1.read();
    Serial.print(char(a));
    Serial.println("   Receiving");
  }
}

Why doesn't it work?

Peter

@GBSC

Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Thanks for your help but can't see your answer, unless moving the question here is it! If so, hope you are right.

Peter

Forgetting about the uno master for a moment in order to help trouble shooting.
If you pair a phone with a bluetooth terminal app with the HC05 on the Mega, and run the terminal app to send to and receive from the Mega does the sketch work correctly?

@GBSC, why don't you try the working Uno program on the Mega with nothing but the essential changes to use Serial1?

...R

Thanks for the idea.

If you put the two sketches side by side you can see that they are the same except for the 'break' code and a delay here and there. These were all added in an attempt to make it work so I started off with almost identical sketches. Without the break, they both just sit there looking at each other!

The Mega just sends an 'A' and waits for a reply. As the Uno doesn't receive the 'A' it just waits. The break causes the Mega to keep trying. This shows up the many attempts before an 'A' gets through.

With two Unos, the link is absolutely solid, no need for retries. Just one initial 'A' and the link works continuously. In practice I would add the break, though, as insurance against a missed 'A'. Also a shut-down sequence at each end in case the link is lost permanently. Can't have manic terminator robots.

Peter

Re. the suggestion to use a phone, am not into Bluetooth phone apps, just have an old iphone. Family sequence, daughter gets an iphone 10 or whatever, wife gets cast off 8, I get cast off 4!

If I did try it and it worked, what is the next move, anyway? The Uno/Uno works, the Uno/Mega doesn't, and the code seems so simple and obvious.

I re-installed the IDE to see if that was the problem. This is via the Microsoft Shop and I see there is a note about bugs they are still trying to resolve. Has one just popped up?

Peter

GBSC:
If you put the two sketches side by side you can see that they are the same

I did try putting them side by side. That's why I made the suggestion in Reply #7

Have you tried what I suggested?

...R

OK, just tried it.

The Mega sends an A, the Uno prints out "A Transmitting". It stops there unless I close the serial monitor display on the Mega and open it again to restart it. The Uno then prints that message again. The Mega is not seeing the transmitted info. to trigger it to send another 'A'.

The consistent, one shot "A Transmitting" is an improvement! Thanks for your help, do you have you any further thoughts?

Peter

Thought I would complete the picture. Here is the stripped down Mega sketch:

// Basic Slave Mega sketch for bluetooth link with corrected HC-05 connections
//As Uno where pos.
byte a;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //C18 Mega Tx1 to HC-05 Rx i/p via 3:2 divider
  //C19 Mega Rx1 to HC-05 Tx o/p
 
}
void loop()
{
  Serial1.print('A');//Request data by sending a character
  Serial.print('A');
  while (Serial1.available() == 0)
  {
  }
  while (Serial1.available())//If there are stored bytes
  {
    a = Serial1.read();
    Serial.print(char(a));
    Serial.println("   Receiving");
  }
}

Peter

I started with the Uno program from your Original Post and I made the minimum changes needed to make it work with a Mega using Serial1 and this is what I get

// Basic Uno Master sketch for bluetooth link
// Revised for Mega

byte a = 0;

//~ #include <SoftwareSerial.h>
//~ SoftwareSerial BTserial(2, 3); // RX | TX
//Uno D2 to HC-05 TX o/p
//Uno D3 to HC-05 RX i/p through a 3:2 voltage divider

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



void loop()
{
  while (Serial1.available())
  {
    a = Serial1.read();
    Serial.print(char(a));
  }
  if ( a == 65)
  {
    Serial.println("   Transmitting");
    Serial1.write(66);
  }
  while (Serial1.available() == 0) //Waits until data is received
  {
  }
}

What happens when you try that?

…R

GBSC:
Re. the suggestion to use a phone, am not into Bluetooth phone apps, just have an old iphone.

Forget that one, good idea as it was. HC-05 is incompatible with iPhone.

Thanks for the Uno sketch revised for a Mega but I am trying to get a Uno master to work with a Mega slave so revising the master sketch to work with a Mega is not going to help directly.

This morning I actually tried two Megas as master/slave. My Master sketch for the Mega is almost identical to yours as you might expect! They didn't work at all.

I am beginning to think that my three recently purchased Megas are all faulty as regards the serial ports. Have now tried all three, and Serial1, 2 and 3 one one of them.

However, more than happy to try any ideas you may suggest.

Peter

GBSC:
Thanks for the Uno sketch revised for a Mega but I am trying to get a Uno master to work with a Mega slave so revising the master sketch to work with a Mega is not going to help directly.

Obviously I modified the wrong Uno program without realizing it. Why don't you have a go at modifying the correct Uno program in the same style that I used?

...R

Did that, no go. Will have another look at your Mega master sketch tomorrow to see if there is something I missed that could be applied to the Mega slave sketch. Just can’t get the Megas to squeak when receiving!

Peter

GBSC:
Did that, no go.

Please post the revised program. Without seeing that I can't help.

This should be a trivial problem - so it is likely there is an error in some small detail.

...R

Have tried many variations without success. To make sure no detail escapes, I have just tried these two sketches again to verify that we are on the same wavelength!

Here is my version of the simplified Mega master sketch, which I think is identical to yours, R:

// Basic Mega Master sketch for bluetooth link
// PH version
byte a = 0;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //C18 Mega Tx1 to HC-05 Rx i/p via 3:2 divider
  //C19 Mega Rx1 to HC-05 Tx o/p
}

void loop()
{
  while (Serial1.available())
  {
    a = Serial1.read();
    Serial.print(char(a));
     }
  if ( a == 65)
  {
    Serial.println("   Transmitting");
    Serial1.write(66);
  }
  while (Serial1.available() == 0) //Waits until data is received
  {
  }
}

Here is the simplified Mega slave sketch:

// Basic Slave Mega sketch for bluetooth link with corrected HC-05 connections
//As Uno where pos.
byte a;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //C18 Mega Tx1 to HC-05 Rx i/p via 3:2 divider
  //C19 Mega Rx1 to HC-05 Tx o/p
 
}
void loop()
{
  Serial1.print('A');//Request data by sending a character
  Serial.print('A');
  
  while (Serial1.available() == 0)
  {
  }
  while (Serial1.available())//If there are stored bytes
  {
    a = Serial1.read();
    Serial.print(char(a));
    Serial.println("   Receiving");
  }
}

If they work with other Megas then mine are faulty, QED.
Here is another idea. Under 'Tools' there is a huge range of Programmers, no idea which one to use so I am staying with ArduinoIsp. Is this right?

Peter