Arduino Uno stops recieving data

I have wireless simplex bluetooth communication setup using Arduino Mega(sending side) and Arduino Uno (recieving side). Arduino Mega that is sending 2 integer numbers via serial2 port through HC-05 bluetooth module. On a recieving side Bluetooth module is connected to Arduino Uno serial port only to pin 0 (rx) so i can print data to computer. On a recieving side i get a stream of numbers that i should get but after 0.5 seconds computer stops recieving data. I get another 0.5 sec every time i restart arduino uno. Why i can’t get a constant stream of data?

This is a arduino uno code

int data[10];
void setup() 
{
  Serial.begin(115200);

}

void loop() 
{
  if (Serial.available() == 2)
  {
    for(int i = 0; i <= 1; i++)
    {
      data[i] = Serial.read();
    }
    Serial.print(data[0]);
    Serial.print("\t");
    Serial.println(data[1]);
  }

Arduino mega code is quite large but this is a piece of code that is important for this topic

for(int i = 0; i <= 1; i++)
             {
               Serial2.write(data[i]);
             }

I also tried using SoftwareSerial but the same problem occured

I wonder are you sending data too quickly and causing the serial output buffer to fill up. That then causes Serial.print() to block the Arduino.

Post the version of the code that you tried with SoftwareSerial.

Write a short program for the Mega that illustrates the problem and post that.

...R

Mega is sending data 100 times/second

Arduino uno with SoftwareSerial

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

int data[10];
void setup() 
{
  Serial.begin(115200);
  mySerial.begin(115200);
}

void loop() 
{
  if (mySerial.available() == 2)
  {
    for(int i = 0; i <= 1; i++)
    {
      data[i] = mySerial.read();
    }
    Serial.print(data[0]);
    Serial.print("\t");
    Serial.println(data[1]);
  }
  

}

There is no problem with Mega, it is sending data constantly. Uno is the one that is blocking so the input serial buffer of Uno is probably filling up.

I have made a new version of the Uno program and i can now recieve data for around 5 sec before it stops working.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

int data[10];
int num = 0;
void setup() 
{
  Serial.begin(115200);
  mySerial.begin(115200);
}

void loop() 
{
num =  mySerial.available();
  num %= 2;
  if (num == 0 && mySerial.available() > 0)
  {
    for(int i = 0; i <= 1; i++)
    {
      data[i] = mySerial.read();
    }
    Serial.print(data[0]);
    Serial.print("\t");
    Serial.println(data[1]);
  }
  

}

I don't think SoftwareSerial can work at 115200 baud. Start with 9600 and if that works try the next higher speed.

What happens if you slow down the Mega so it only sends (say) 5 times per second?

An Uno won't block because the input buffer is full - the data just gets lost. But it will block if the output buffer gets full.

Try the second example in Serial Input Basics. Even better would be to design the Mega program to comply with example 3.

Perhaps you need a more complete system in which the Uno sends a message to the Mega when it is ready to receive more data and the Mega only sends a chunk of data when it gets the message.

I would very much like to see the Mega program - preferably a short version that just concentrates on this problem.

...R