Bluetooth overflow problem (Arduino micro as slave and Arduino mega as master)

Hello. My system uses an Arduino micro to read and measure the data from an MPU6050 sensor and FSR sensors and send it through Bluetooth(FB155BC) to the mainboard(Arduino Mega). My problem is that when I receive the data from the slave it looks live an overflow is happening(the number 255 appears instead of the actual data sometimes, but not constantly). I don't know how to fix this problem. Here are the codes:
Master code:

#include <stdio.h>
#include<movingAvg.h>


int buttonState = 0;
uint8_t buf_r[50], buf_l[50];
uint8_t old_buf_r[50], old_buf_l[50];
uint8_t pack_r[50], pack_l[50];
uint8_t intact_pack_r[50], intact_pack_l[50];


movingAvg encvel(10);
movingAvg enc(10);


int Encoder_pin[10] = {25, 27, 29, 31, 33, 35, 37, 39, 41, 43};
int Encoder_state[10];
int Encoder_sum = 0;
double Encoder_deg_new = 0, Encoder_deg_old = 0;
double E_vel_deg_new, E_vel_deg_old, Encoder_vel, Encoder_vel_rpm;
int Encoder_revcnt = 0;
double A_encoder = 0;
char Value[10];
unsigned int count = 0;
volatile byte state = 0;
double encavg=0;
double torque=0;

double Encoder_offset =0;

float seno;
int frequencia;

unsigned long previousMicro = 0; 
const long interval = 1000;  

int flagr=0;
int flagl=0;

boolean newData = false;

void setup() {


  for (int i = 0; i < 10; i++) {
    pinMode(Encoder_pin[i], INPUT);
  }
  

  
  Serial.begin(115200);
  Serial3.begin(115200);
  Serial2.begin(115200);
  encvel.begin();
  enc.begin();
  
Serial3.write('A');
Serial2.write('B');




}


void loop() {


unsigned long currentMicro = micros();

  if ((unsigned long)(micros() - previousMicro) >= interval) {

     previousMicro = micros(); // keep in sync with the time 




 Encoder_deg_old = Encoder_deg_new; 
    E_vel_deg_old = E_vel_deg_new;

    for (int i = 0; i < 10; i++) {
      Encoder_state[i] = digitalRead(Encoder_pin[i]);
     }

    Encoder_sum = 0;

    int Encoder_cnt = 0;

    for (int Encoder_cnt = 0; Encoder_cnt < 10; Encoder_cnt++) {
      Encoder_sum += Encoder_state[Encoder_cnt] << Encoder_cnt;
      }
int encavg=enc.reading(Encoder_sum);

    Encoder_deg_new = ((long)((((double)Encoder_sum*0.3515625) - Encoder_offset + 360)*(long)(100)))%36000/100.0;
        

 int i, j, val, k1_r, k2_r, k1_l, k2_l = 0;
  int t1_r, t2_r, t1_l, t2_l = 0;
  char checknumberL = '?';
  char checknumberR = '&';


if(Serial3.available() && flagr==0){
    for (j = 0; j < 6; j++)
  {
    buf_r[j] = Serial3.read();
    }

if((buf_r[0]=='

Cont. on the next post because of character limit.)&&(buf_r[5] == checknumberR)){
    flagr=1;
}
   
}
if(Serial2.available()>0 && flagl==0){

for (j = 0; j < 6; j++)
  {
    buf_l[j] = Serial2.read();
  }
 
if((buf_l[0]=='!')&&(buf_l[5] == checknumberL)){
  flagl=1;
}
 
}

//  /////////////////raw data check
    for (int i = 0; i < 6; i++) {
      Serial.print(buf_r[i]);
      Serial.print('\t');
    }
    Serial.print('\n');

for (int i = 0; i <6; i++) {
      Serial.print(buf_l[i]);
      Serial.print('\t');
    }
    Serial.print('\n');

//data processing
  /////////////////Right//////////////////////
 
if(flagr==1){
 
if (buf_r[5] == checknumberR) {
    for (j = 0; j < 6; j++) {
      pack_r[j] = buf_r[j];
    }
    for ( i = 0; i < 6 - 1; i++) {
      if ( pack_r[i] == checknumberR ) {
        for (j = 0; j < 6; j++) {
          pack_r[j] = intact_pack_r[j];
        }
      }
    }
    for (j = 0; j < 6; j++) {
      intact_pack_r[j] = pack_r[j];
    }
  }
  else {
    for (int k = 0; k < 6; k++) {
      if (buf_r[k] == checknumberR) {
        k1_r = k;
        k2_r = 4 - k;
        while (k1_r >= 0) {
          pack_r[5 - t1_r] = buf_r[k1_r];
          k1_r--; t1_r++;
        }
        while (k2_r >= 0) {
          pack_r[k2_r] = old_buf_r[5 - t2_r];
          k2_r--; t2_r++;
        }
        break;
      }
      else {
        ;
      }
    }
    for ( i = 0; i < 6 - 1; i++) {
      if ( pack_r[i] == checknumberR ) {
        for (j = 0; j < 6; j++) {
          pack_r[j] = intact_pack_r[j];
        }
      }
    }
    for (j = 0; j < 6; j++) {
      intact_pack_r[j] = pack_r[j];
    }
  }
  if (pack_r[0] != '


Cont. on the next post because of character limit.) {
    for (j = 0; j < 6; j++) {
      pack_r[j] = intact_pack_r[j];
    }
  }
  for (j = 0; j < 6; j++) {
    old_buf_r[j] = buf_r[j];
  }

  Serial3.write('A');

//  for (j = 0; j < 6; j++)
//   {
//    buf_r[j] = ' ';
//   }
  
  flagr=0;
}

  

  ///////////////////left////////////////////
if(flagl==1){
  
  if (buf_l[5] == checknumberL) {
    for (j = 0; j < 6; j++) {
      pack_l[j] = buf_l[j];
    }
    for ( i = 0; i < 6 - 1; i++) {
      if ( pack_l[i] == checknumberL ) {
        for (j = 0; j < 6; j++) {
          pack_l[j] = intact_pack_l[j];
        }
      }
    }
    for (j = 0; j < 6; j++) {
      intact_pack_l[j] = pack_l[j];
    }
  }
  else {
    for (int k = 0; k < 6; k++) {
      if (buf_l[k] == checknumberL) {
        k1_l = k;
        k2_l = 4 - k;
        while (k1_l >= 0) {
          pack_l[5 - t1_l] = buf_l[k1_l];
          k1_l--; t1_l++;
        }
        while (k2_l >= 0) {
          pack_l[k2_l] = old_buf_l[5 - t2_l];
          k2_l--; t2_l++;
        }
        break;
      }
      else {
        ;
      }
    }
    for ( i = 0; i < 6 - 1; i++) {
      if ( pack_l[i] == checknumberL ) {
        for (j = 0; j < 6; j++) {
          pack_l[j] = intact_pack_l[j];
        }
      }
    }
    for (j = 0; j < 6; j++) {
      intact_pack_l[j] = pack_l[j];
    }
  }

  if (pack_l[0] != '?') {
    for (j = 0; j < 6; j++) {
      pack_l[j] = intact_pack_l[j];
    }
  }
  for (j = 0; j < 6; j++) {
    old_buf_l[j] = buf_l[j];
  }

  Serial2.write('B');  

 

   for (j = 0; j < 6; j++)
  {
    buf_l[j] = ' ';
  }

   flagl=0;
}



float pi=3.14159;
float encorderrad = Encoder_deg_new*(pi/180);
float AR=((pack_r[1] << 8) | pack_r[2]);
float FR=((pack_r[3] << 8) | pack_r[4])*0.01;
float AL=((pack_l[1] << 8) | pack_l[2])*0.01;
float FL=((pack_l[3] << 8) | pack_l[4])*0.01;
float Rangrad=AR*(pi/180);
float Langrad=AL*(pi/180);
  float torque = (0.17*0.022539446/0.089013718)*(FR*cos(encorderrad-Rangrad)+FL*cos((encorderrad+pi)-Langrad));

 
//Serial.print(Encoder_deg_new);
//Serial.print(" ");
//Serial.println(360);

//      if((pack_l[0] == 200) &&
//         (pack_r[0] == 200) 
//        &&(pack_l[1] < 180) &&
//        (pack_r[1] < 180) &&
//        (pack_l[2] != pack_l[1]||pack_l[0]) &&
//        (pack_r[2] != pack_r[1]||pack_r[0])
//        )
//        {
//
//
//
////      Serial.print(Encoder_deg_new);
////      Serial.print(" ");
//      
//      for (int i = 0; i < 6; i++) {    
//      Serial.print(pack_l[i]);
//      Serial.print("\t");
//      }
//      Serial.print(" ");
//      for (int i = 0; i < 6; i++) {
//      Serial.print(pack_r[i]);
//      Serial.print("\t");
//      }
//      Serial.println("");
//      Serial.println(AL);
////      Serial.print(" ");
////      Serial.print(FL);
////      Serial.print(" ");
//      Serial.println(AR*0.01);
//      Serial.print(" ");
//      Serial.println(FR);
////      Serial.print(" ");
////      Serial.println(torque);

//      }
//
//      else if((pack_l[0] != 200) ||
//         (pack_r[0] != 200)){
//         Serial.print("");
//         }

         }

  }

Cont. on the next post because of character limit.

Cont.

On of the slaves (Right) same code with start and end markers being different:

#include <Wire.h>
#include <Kalman.h>
#include<movingAvg.h>

movingAvg mySensor0(10);
movingAvg mySensor1(10);
movingAvg mySensor2(10);
movingAvg mySensor3(10);

movingAvg myF0(10);
movingAvg myF1(10);
movingAvg myF2(10);
movingAvg myF3(10);

#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

char master;

int SensorPin0 = A0; //analog pin 0
int SensorPin1 = A1;
int SensorPin2 = A2;
int SensorPin3 = A3;
int MeanDataforce = 0;
int SensorReading0 = 0;
int SensorReading1 = 0;
int SensorReading2 = 0;
int SensorReading3 = 0;
float F0 = 0;
float F1 = 0;
float F2 = 0;
float F3 = 0;
char F_sign =0;
uint8_t F_h, F_l;

char pAngle_sign = 0;
uint8_t pAngle_h, pAngle_l;

/* IMU Data */
int16_t accX, accY, accZ, tempRaw, gyroX, gyroY, gyroZ;
double accXangle, accYangle; // Angle calculate using the accelerometer
double temp; // Temperature
double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
double CurX, CurY, CurZ;
uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

unsigned long previousMicro = 0; 
const long interval = 1000; 

int state = 0;
int flag=0;

void setup() {

  

  Serial.begin(115200);
  Serial1.begin(115200);

  
  mySensor0.begin();
  mySensor1.begin();
  mySensor2.begin();
  mySensor3.begin();

  myF0.begin();
  myF1.begin();
  myF2.begin();
  myF3.begin();
  
/*MPU setup*/
 Wire.begin();
#if ARDUINO >= 157
  Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
  TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  /* Set kalman and gyro starting angle */
  while (i2cRead(0x3B, i2cData, 6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  kalmanX.setAngle(accXangle); // Set starting angle
  kalmanY.setAngle(accYangle);
  gyroXangle = accXangle;
  gyroYangle = accYangle;
  compAngleX = accXangle;
  compAngleY = accYangle;

  timer = micros();
}

void loop() {

unsigned long currentMicro = micros();

  if ((unsigned long)(micros() - previousMicro) >= interval) {

     previousMicro = micros(); // keep in sync with the time 

//while(!Serial1.available());

 if(Serial1.available()){
 master=Serial1.read();
  
 Serial.println(master);
 }

 if(master=='A'&& flag==0){
  flag=1;
 }

/* Update all the IMU values */

  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = ((i2cData[6] << 8) | i2cData[7]);
  gyroX = ((i2cData[8] << 8) | i2cData[9]);
  gyroY = ((i2cData[10] << 8) | i2cData[11]);
  gyroZ = ((i2cData[12] << 8) | i2cData[13]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  double gyroXrate = (double)gyroX / 131.0;
  double gyroYrate = -((double)gyroY / 131.0);
  double gyroZrate = ((double)gyroZ / 131.0);
  gyroXangle += gyroXrate * ((double)(micros() - timer) / 1000000); // Calculate gyro angle without any filter
  gyroYangle += gyroYrate * ((double)(micros() - timer) / 1000000);
  gyroZangle += gyroZrate * ((double)(micros() - timer) / 1000000);

  double compGain = 0.99;
  compAngleX = (compGain * (compAngleX + (gyroXrate * (double)(micros() - timer) / 1000000))) + ((1 - compGain) * accXangle); // Calculate the angle using a Complimentary filter
  compAngleY = (compGain * (compAngleY + (gyroYrate * (double)(micros() - timer) / 1000000))) + ((1 - compGain) * accYangle);
  compAngleZ = (compAngleZ + (gyroZrate * (double)(micros() - timer) / 1000000));
  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros() - timer) / 1000000); // Calculate the angle using a Kalman filter
  kalAngleY = kalmanY.getAngle(accYangle, gyroYrate, (double)(micros() - timer) / 1000000);
  //kalAngleZ = kalmanZ.getAngle(accZangle, gyroZrate, (double)(micros() - timer) / 1000000);
  timer = micros();

   int CurX = (abs(kalAngleX - 90))*100;
  CurY = 180 - kalAngleY;
  CurZ = compAngleZ;
//  arduino2arduino(CurX);
  

  /*FORCE SENSOR*/
    int SensorReading0 = analogRead(SensorPin0);
    int SensorReading1 = analogRead(SensorPin1);
    int SensorReading2 = analogRead(SensorPin2);
    int SensorReading3 = analogRead(SensorPin3);

    int avg0 = mySensor0.reading(SensorReading0);
    int avg1 = mySensor1.reading(SensorReading1);
    int avg2 = mySensor2.reading(SensorReading2);
    int avg3 = mySensor3.reading(SensorReading3);
    
  double F0 = 0.002594*exp(0.0215*avg0);//ok
  double F1 = 0.01507*exp(0.01592*avg1);//ok
  double F2 = 0.006178*exp(0.0209*avg2);//ok
  double F3 = 0.001196*exp(0.020247*avg3);//ok

int MeanDataforce = (F0 + F1 + F2 + F3)*100;


  F_h =  (uint8_t)(MeanDataforce>>8);
  F_l = (uint8_t)(MeanDataforce);



  pAngle_h = (uint8_t)(CurX>>8);
  pAngle_l = (uint8_t)(CurX);

int x= (pAngle_h << 8) | pAngle_l;

//   Serial.print(CurX);
//  Serial.print(" "); 
//  Serial.print(pAngle_h);
//  Serial.print("  ");
//  Serial.print(pAngle_l);
//    Serial.print(" ");
//    Serial.println(x*0.01);
//  Serial.print(" ");
//  Serial.print(MeanDataforce);
//  Serial.print(" ");
//  Serial.print(F_h);
//  Serial.print(" ");
//  Serial.println(F_l);
//
//
//  Serial.print(" ");
//  Serial.print(F0);
//  Serial.print(" ");
//  Serial.print(F1);
//  Serial.print(" ");
//  Serial.print(F2);
//  Serial.print(" ");
//  Serial.println(F3);




   }

 if(flag==1){
  Serial1.write('

I've tried to make a request command sending 'A' for the slave, but it does not work as I intended. If you have any idea on how I can fix my code they are all welcome. If you need any more info please let me know.

Thank you,
Tuani);
 Serial1.write(pAngle_h);
 Serial1.write(pAngle_l);
 Serial1.write(F_h);
 Serial1.write(F_l);
 Serial1.write('&');
 
 flag=0;
}

}


I've tried to make a request command sending 'A' for the slave, but it does not work as I intended. If you have any idea on how I can fix my code they are all welcome. If you need any more info please let me know.

Thank you,
Tuani

You can more easily and robustly communicate between your two Arduinos using the library SerialTransfer.h. It automatically packetizes and parses your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

Power_Broker:
You can more easily and robustly communicate between your two Arduinos using the library SerialTransfer.h. It automatically packetizes and parses your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes examples.

Thank you, but even using this library I still have the same problem. When I used this library I got the -1 error.

Tuani:
Thank you, but even using this library I still have the same problem. When I used this library I got the -1 error.

Please post your current sketches.

First I would like to thank you so much because it actually did help after I added: "while(!Serial3.available());". I don't have the overflow problem anymore, I think. But the error -1 message still appears. So if you know why, please let me know.

Slave code:

#include <Wire.h>
#include <Kalman.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransfer;

movingAvg mySensor0(10);
movingAvg mySensor1(10);
movingAvg mySensor2(10);
movingAvg mySensor3(10);

movingAvg myF0(10);
movingAvg myF1(10);
movingAvg myF2(10);
movingAvg myF3(10);



#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

char master;

int SensorPin0 = A0; //analog pin 0
int SensorPin1 = A1;
int SensorPin2 = A2;
int SensorPin3 = A3;
int MeanDataforce = 0;
int SensorReading0 = 0;
int SensorReading1 = 0;
int SensorReading2 = 0;
int SensorReading3 = 0;
float F0 = 0;
float F1 = 0;
float F2 = 0;
float F3 = 0;
char F_sign =0;
uint8_t F_h, F_l;

char pAngle_sign = 0;
uint8_t pAngle_h, pAngle_l;

/* IMU Data */
int16_t accX, accY, accZ, tempRaw, gyroX, gyroY, gyroZ;
double accXangle, accYangle; // Angle calculate using the accelerometer
double temp; // Temperature
double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
double CurX;
uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

unsigned long previousMicro = 0; 
const long interval = 1000; 

int state = 0;
int flag=0;


void setup() {

  

  Serial.begin(115200);
  Serial1.begin(115200);
myTransfer.begin(Serial1);
  
  mySensor0.begin();
  mySensor1.begin();
  mySensor2.begin();
  mySensor3.begin();

  myF0.begin();
  myF1.begin();
  myF2.begin();
  myF3.begin();
  
/*MPU setup*/
 Wire.begin();
#if ARDUINO >= 157
  Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
  TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  /* Set kalman and gyro starting angle */
  while (i2cRead(0x3B, i2cData, 6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  kalmanX.setAngle(accXangle); // Set starting angle
  kalmanY.setAngle(accYangle);
  gyroXangle = accXangle;
  gyroYangle = accYangle;
  compAngleX = accXangle;
  compAngleY = accYangle;

  timer = micros();
}

void loop() {

/* Update all the IMU values */

  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = ((i2cData[6] << 8) | i2cData[7]);
  gyroX = ((i2cData[8] << 8) | i2cData[9]);
  gyroY = ((i2cData[10] << 8) | i2cData[11]);
  gyroZ = ((i2cData[12] << 8) | i2cData[13]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  double gyroXrate = (double)gyroX / 131.0;
  double gyroYrate = -((double)gyroY / 131.0);
  double gyroZrate = ((double)gyroZ / 131.0);
  gyroXangle += gyroXrate * ((double)(micros() - timer) / 1000000); // Calculate gyro angle without any filter
  gyroYangle += gyroYrate * ((double)(micros() - timer) / 1000000);
  gyroZangle += gyroZrate * ((double)(micros() - timer) / 1000000);

  double compGain = 0.99;
  compAngleX = (compGain * (compAngleX + (gyroXrate * (double)(micros() - timer) / 1000000))) + ((1 - compGain) * accXangle); // Calculate the angle using a Complimentary filter
  compAngleY = (compGain * (compAngleY + (gyroYrate * (double)(micros() - timer) / 1000000))) + ((1 - compGain) * accYangle);
  compAngleZ = (compAngleZ + (gyroZrate * (double)(micros() - timer) / 1000000));
  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros() - timer) / 1000000); // Calculate the angle using a Kalman filter
  kalAngleY = kalmanY.getAngle(accYangle, gyroYrate, (double)(micros() - timer) / 1000000);
  //kalAngleZ = kalmanZ.getAngle(accZangle, gyroZrate, (double)(micros() - timer) / 1000000);
  timer = micros();

   int CurX = (abs(kalAngleX - 90))*100;
  
  /*FORCE SENSOR*/
    int SensorReading0 = analogRead(SensorPin0);
    int SensorReading1 = analogRead(SensorPin1);
    int SensorReading2 = analogRead(SensorPin2);
    int SensorReading3 = analogRead(SensorPin3);

    int avg0 = mySensor0.reading(SensorReading0);
    int avg1 = mySensor1.reading(SensorReading1);
    int avg2 = mySensor2.reading(SensorReading2);
    int avg3 = mySensor3.reading(SensorReading3);
    
  double F0 = 0.002594*exp(0.0215*avg0);//ok
  double F1 = 0.01507*exp(0.01592*avg1);//ok
  double F2 = 0.006178*exp(0.0209*avg2);//ok
  double F3 = 0.001196*exp(0.020247*avg3);//ok

int MeanDataforce = (F0 + F1 + F2 + F3)*100;


  F_h =  (uint8_t)(MeanDataforce>>8);
  F_l = (uint8_t)(MeanDataforce);

  pAngle_h = (uint8_t)(CurX>>8);
  pAngle_l = (uint8_t)(CurX);

  myTransfer.txBuff[0] = '

Master code:

#include <stdio.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransferR;

int buttonState = 0;
uint8_t buf_r[6], buf_l[6];
uint8_t old_buf_r[6], old_buf_l[6];
uint8_t pack_r[6], pack_l[6];
uint8_t intact_pack_r[6], intact_pack_l[6];


boolean newData = false;

void setup() {

  
  Serial.begin(115200);
  Serial3.begin(115200);
  myTransferR.begin(Serial3);
 
  Serial2.begin(115200);
}


void loop() {


while(!Serial3.available());

if(myTransferR.available()){
     for (byte i = 0; i < myTransferR.bytesRead; i++)
  { 
    
    buf_r[i] = myTransferR.rxBuff[i];
    
  }
}
else if(myTransferR.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransferR.status);
  }



unsigned long currentMicro = micros();

  if ((unsigned long)(micros() - previousMicro) >= interval) {

     previousMicro = micros(); // keep in sync with the time 

 

 int i, j,p, val, k1_r, k2_r, k1_l, k2_l = 0;
  int t1_r, t2_r, t1_l, t2_l = 0;
  char checknumberL = '?';
  char checknumberR = '&';


//  /////////////////raw data check
    for (int i = 0; i < 6; i++) {
      Serial.print(buf_r[i]);
      Serial.print('\t');
    }
    Serial.print('\n');

 }

  }

Again, thank you so much!;
  myTransfer.txBuff[1] = pAngle_h;
  myTransfer.txBuff[2] = pAngle_l;
  myTransfer.txBuff[3] = F_h;
  myTransfer.txBuff[4] = F_l;
  myTransfer.txBuff[5] = '&';

myTransfer.sendData(6);

}


Master code:

§DISCOURSE_HOISTED_CODE_1§


Again, thank you so much!

author=Power_Broker link=msg=4538734 date=1585667992]
Please post your current sketches.

Also, can I use this for two SerialTransfers? Because when I try with 2(receiving from two different Bluetooth) the receiving stops after a while. Here is the master code for two:

#include <stdio.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransferR;
SerialTransfer myTransferL;

int buttonState = 0;
uint8_t buf_r[6], buf_l[6];
uint8_t old_buf_r[6], old_buf_l[6];
uint8_t pack_r[6], pack_l[6];
uint8_t intact_pack_r[6], intact_pack_l[6];


movingAvg encvel(10);
movingAvg enc(10);



int Encoder_pin[10] = {25, 27, 29, 31, 33, 35, 37, 39, 41, 43};
int Encoder_state[10];
int Encoder_sum = 0;
double Encoder_deg_new = 0, Encoder_deg_old = 0;
double E_vel_deg_new, E_vel_deg_old, Encoder_vel, Encoder_vel_rpm;
int Encoder_revcnt = 0;
double A_encoder = 0;
char Value[10];
unsigned int count = 0;
volatile byte state = 0;
double encavg=0;
double torque=0;

double Encoder_offset =0;

float seno;
int frequencia;

unsigned long previousMicro = 0; 
const long interval = 1000;  

int flagr=0;
int flagl=0;

int temp;

boolean newData = false;

void setup() {


 for (int i = 0; i < 10; i++) {
   pinMode(Encoder_pin[i], INPUT);
 }
   
 Serial.begin(115200);
 Serial3.begin(115200);
 myTransferR.begin(Serial3);

 Serial2.begin(115200);
 myTransferL.begin(Serial2);
 encvel.begin();
 enc.begin();
 

}


void loop() {


while(!Serial3.available());

if(myTransferR.available()){
    for (byte i = 0; i < myTransferR.bytesRead; i++)
 { 
   
   buf_r[i] = myTransferR.rxBuff[i];
   
 }
}
else if(myTransferR.status < 0)
 {
   Serial.print("ERROR: ");
   Serial.println(myTransferR.status);
 }

while(!Serial2.available());

if(myTransferL.available()){
    for (byte i = 0; i < myTransferL.bytesRead; i++)
 { 
   
   buf_l[i] = myTransferL.rxBuff[i];
   
 }
}
else if(myTransferL.status < 0)
 {
   Serial.print("ERROR: ");
   Serial.println(myTransferL.status);
 }

unsigned long currentMicro = micros();

 if ((unsigned long)(micros() - previousMicro) >= interval) {

    previousMicro = micros(); // keep in sync with the time 




Encoder_deg_old = Encoder_deg_new; 
   E_vel_deg_old = E_vel_deg_new;

   for (int i = 0; i < 10; i++) {
     Encoder_state[i] = digitalRead(Encoder_pin[i]);
    }

   Encoder_sum = 0;

   int Encoder_cnt = 0;

   for (int Encoder_cnt = 0; Encoder_cnt < 10; Encoder_cnt++) {
     Encoder_sum += Encoder_state[Encoder_cnt] << Encoder_cnt;
     }
int encavg=enc.reading(Encoder_sum);

   Encoder_deg_new = ((long)((((double)Encoder_sum*0.3515625) - Encoder_offset + 360)*(long)(100)))%36000/100.0;
       

int i, j,p, val, k1_r, k2_r, k1_l, k2_l = 0;
 int t1_r, t2_r, t1_l, t2_l = 0;
 char checknumberL = '?';
 char checknumberR = '&';


//  /////////////////raw data check
//    for (int i = 0; i < 6; i++) {
//      Serial.print(buf_r[i]);
//      Serial.print('\t');
//    }
//    Serial.print('\n');

   for (int i = 0; i <6; i++) {
     Serial.print(buf_l[i]);
     Serial.print('\t');
   }
   Serial.print('\n');


 //data processing
/////////////////Right//////////////////////

 
if (buf_r[5] == checknumberR) {
   for (j = 0; j < 6; j++) {
     pack_r[j] = buf_r[j];
   }
   for ( i = 0; i < 6 - 1; i++) {
     if ( pack_r[i] == checknumberR ) {
       for (j = 0; j < 6; j++) {
         pack_r[j] = intact_pack_r[j];
       }
     }
   }
   for (j = 0; j < 6; j++) {
     intact_pack_r[j] = pack_r[j];
   }
 }
 else {
   for (int k = 0; k < 6; k++) {
     if (buf_r[k] == checknumberR) {
       k1_r = k;
       k2_r = 4 - k;
       while (k1_r >= 0) {
         pack_r[5 - t1_r] = buf_r[k1_r];
         k1_r--; t1_r++;
       }
       while (k2_r >= 0) {
         pack_r[k2_r] = old_buf_r[5 - t2_r];
         k2_r--; t2_r++;
       }
       break;
     }
     else {
       ;
     }
   }
   for ( i = 0; i < 6 - 1; i++) {
     if ( pack_r[i] == checknumberR ) {
       for (j = 0; j < 6; j++) {
         pack_r[j] = intact_pack_r[j];
       }
     }
   }
   for (j = 0; j < 6; j++) {
     intact_pack_r[j] = pack_r[j];
   }
 }
 if (pack_r[0] != '

) {
   for (j = 0; j < 6; j++) {
     pack_r[j] = intact_pack_r[j];
   }
 }
 for (j = 0; j < 6; j++) {
   old_buf_r[j] = buf_r[j];
 }

///////////////////left////////////////////

if (buf_l[5] == checknumberL) {
   for (j = 0; j < 6; j++) {
     pack_l[j] = buf_l[j];
   }
   for ( i = 0; i < 6 - 1; i++) {
     if ( pack_l[i] == checknumberL ) {
       for (j = 0; j < 6; j++) {
         pack_l[j] = intact_pack_l[j];
       }
     }
   }
   for (j = 0; j < 6; j++) {
     intact_pack_l[j] = pack_l[j];
   }
 }
 else {
   for (int k = 0; k < 6; k++) {
     if (buf_l[k] == checknumberL) {
       k1_l = k;
       k2_l = 4 - k;
       while (k1_l >= 0) {
         pack_l[5 - t1_l] = buf_l[k1_l];
         k1_l--; t1_l++;
       }
       while (k2_l >= 0) {
         pack_l[k2_l] = old_buf_l[5 - t2_l];
         k2_l--; t2_l++;
       }
       break;
     }
     else {
       ;
     }
   }
   for ( i = 0; i < 6 - 1; i++) {
     if ( pack_l[i] == checknumberL ) {
       for (j = 0; j < 6; j++) {
         pack_l[j] = intact_pack_l[j];
       }
     }
   }
   for (j = 0; j < 6; j++) {
     intact_pack_l[j] = pack_l[j];
   }
 }

if (pack_l[0] != '?') {
   for (j = 0; j < 6; j++) {
     pack_l[j] = intact_pack_l[j];
   }
 }
 for (j = 0; j < 6; j++) {
   old_buf_l[j] = buf_l[j];
 }

float pi=3.14159;
float encorderrad = Encoder_deg_new*(pi/180);
float AR=((pack_r[1] << 8) | pack_r[2]);
float FR=((pack_r[3] << 8) | pack_r[4])0.01;
float AL=((pack_l[1] << 8) | pack_l[2])0.01;
float FL=((pack_l[3] << 8) | pack_l[4])0.01;
float Rangrad=AR
(pi/180);
float Langrad=AL
(pi/180);
 float torque = (0.17
0.022539446/0.089013718)(FRcos(encorderrad-Rangrad)+FL*cos((encorderrad+pi)-Langrad));

//Serial.print(Encoder_deg_new);
//Serial.print(" ");
//Serial.println(360);

//      if((pack_l[0] == 200) &&
//         (pack_r[0] == 200)
//        &&(pack_l[1] < 180) &&
//        (pack_r[1] < 180) &&
//        (pack_l[2] != pack_l[1]||pack_l[0]) &&
//        (pack_r[2] != pack_r[1]||pack_r[0])
//        )
//        {
//
//
//
////      Serial.print(Encoder_deg_new);
////      Serial.print(" ");
//      
//      for (int i = 0; i < 6; i++) {    
//      Serial.print(pack_l[i]);
//      Serial.print("\t");
//      }
//      Serial.print(" ");
//      for (int i = 0; i < 6; i++) {
//      Serial.print(pack_r[i]);
//      Serial.print("\t");
//      }
//      Serial.println("");
//      Serial.println(AL);
////      Serial.print(" ");
////      Serial.print(FL);
////      Serial.print(" ");
//      Serial.println(AR*0.01);
//      Serial.print(" ");
//      Serial.println(FR);
////      Serial.print(" ");
////      Serial.println(torque);

//      }
//
//      else if((pack_l[0] != 200) ||
//         (pack_r[0] != 200)){
//         Serial.print("");
//         }

}

}

Tuani:
Also, can I use this for two SerialTransfers?

Absolutely. I use 3 instances of the SerialTransfer class at the same time in the code I use to fly an Arduino RC plane.

Before we continue, I need you to do a few things:

1.) Determine the exact current code you want to get working. You've provided two different versions of the master sketch and your receiver code won't even compile for me. When posting code, post all of it.
2.) Properly indent your code (ctr+t). Your current code is difficult to read without this.
3.) Use consistent and intuitive use of whitespace. Again, this really helps in readability since I'm not familiar with your codebase.

When you're done, post all of your current sketches and we'll see what's going wrong. As far as I can tell, there doesn't seem to be much of an issue, but then again, I haven't seen the exact code you're running.

Sorry for the confusion, I tried reducing the code to fit in one post, but that was not a good idea.

So first here are the pedal(slaves) codes:

Right:

#include <Wire.h>
#include <Kalman.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransfer;

#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

movingAvg mySensor0(10);
movingAvg mySensor1(10);
movingAvg mySensor2(10);
movingAvg mySensor3(10);

movingAvg myF0(10);
movingAvg myF1(10);
movingAvg myF2(10);
movingAvg myF3(10);

int SensorPin0 = A0; //analog pin 0
int SensorPin1 = A1;
int SensorPin2 = A2;
int SensorPin3 = A3;
int MeanDataforce = 0;
int SensorReading0 = 0;
int SensorReading1 = 0;
int SensorReading2 = 0;
int SensorReading3 = 0;
float F0 = 0;
float F1 = 0;
float F2 = 0;
float F3 = 0;
uint8_t F_h, F_l;


/* IMU Data */
int16_t accX, accY, accZ, tempRaw, gyroX, gyroY, gyroZ;
double accXangle, accYangle; // Angle calculate using the accelerometer
double temp; // Temperature
double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
double CurX;
uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

uint8_t pAngle_h, pAngle_l;

void setup() {

  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);

  mySensor0.begin();
  mySensor1.begin();
  mySensor2.begin();
  mySensor3.begin();

  myF0.begin();
  myF1.begin();
  myF2.begin();
  myF3.begin();

  /*MPU setup*/
  Wire.begin();
#if ARDUINO >= 157
  Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
  TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  /* Set kalman and gyro starting angle */
  while (i2cRead(0x3B, i2cData, 6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  kalmanX.setAngle(accXangle); // Set starting angle
  kalmanY.setAngle(accYangle);
  gyroXangle = accXangle;

  timer = micros();
}

void loop() {

  /* Update all the IMU values */

  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  gyroX = ((i2cData[8] << 8) | i2cData[9]);

  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  double gyroXrate = (double)gyroX / 131.0;

  gyroXangle += gyroXrate * ((double)(micros() - timer) / 1000000); // Calculate gyro angle without any filter

  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros() - timer) / 1000000); // Calculate the angle using a Kalman filter

  timer = micros();

  double CurX = (abs(kalAngleX - 90));

  /*FORCE SENSOR*/
  int SensorReading0 = analogRead(SensorPin0);
  int SensorReading1 = analogRead(SensorPin1);
  int SensorReading2 = analogRead(SensorPin2);
  int SensorReading3 = analogRead(SensorPin3);

  int avg0 = mySensor0.reading(SensorReading0);
  int avg1 = mySensor1.reading(SensorReading1);
  int avg2 = mySensor2.reading(SensorReading2);
  int avg3 = mySensor3.reading(SensorReading3);

  double F0 = 0.002594 * exp(0.0215 * avg0); //ok
  double F1 = 0.01507 * exp(0.01592 * avg1); //ok
  double F2 = 0.006178 * exp(0.0209 * avg2); //ok
  double F3 = 0.001196 * exp(0.020247 * avg3); //ok

  double MeanDataforce = (F0 + F1 + F2 + F3);

  //////PROCESSING DATA TO SEND

  F_h =  (uint8_t)(MeanDataforce);
  F_l = (uint8_t)(MeanDataforce * 100) % 100;

  pAngle_h = (uint8_t)(CurX);
  pAngle_l = (uint8_t)(CurX * 100) % 100;

  myTransfer.txBuff[0] = '

Cont.;
  myTransfer.txBuff[1] = pAngle_h;
  myTransfer.txBuff[2] = pAngle_l;
  myTransfer.txBuff[3] = F_h;
  myTransfer.txBuff[4] = F_l;
  myTransfer.txBuff[5] = '&';

myTransfer.sendData(6);

Serial.println(CurX);
  //  Serial.print(" ");
  //  Serial.print(pAngle_h);
  //  Serial.print("  ");
  //  Serial.print(pAngle_l);
  //  Serial.print(" ");
  //  Serial.print(MeanDataforce);
  //  Serial.print(" ");
  //  Serial.print(F_h);
  //  Serial.print(" ");
  //  Serial.println(F_l);
  //
  //
  //  Serial.print(" ");
  //  Serial.print(F0);
  //  Serial.print(" ");
  //  Serial.print(F1);
  //  Serial.print(" ");
  //  Serial.print(F2);
  //  Serial.print(" ");
  //  Serial.println(F3);
}


Cont.

Left:

#include <Wire.h>
#include <Kalman.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransfer;

#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf

Kalman kalmanX; // Create the Kalman instances
Kalman kalmanY;

movingAvg mySensor0(10);
movingAvg mySensor1(10);
movingAvg mySensor2(10);
movingAvg mySensor3(10);

movingAvg myF0(10);
movingAvg myF1(10);
movingAvg myF2(10);
movingAvg myF3(10);

int SensorPin0 = A0; //analog pin 0
int SensorPin1 = A1;
int SensorPin2 = A2;
int SensorPin3 = A3;
int MeanDataforce = 0;
int SensorReading0 = 0;
int SensorReading1 = 0;
int SensorReading2 = 0;
int SensorReading3 = 0;
float F0 = 0;
float F1 = 0;
float F2 = 0;
float F3 = 0;
uint8_t F_h, F_l;


/* IMU Data */
int16_t accX, accY, accZ, tempRaw, gyroX, gyroY, gyroZ;
double accXangle, accYangle; // Angle calculate using the accelerometer
double temp; // Temperature
double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
double CurX, CurY, CurZ;
uint32_t timer;
uint8_t i2cData[14]; // Buffer for I2C data

uint8_t pAngle_h, pAngle_l;


void setup() {


  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);

  mySensor0.begin();
  mySensor1.begin();
  mySensor2.begin();
  mySensor3.begin();

  myF0.begin();
  myF1.begin();
  myF2.begin();
  myF3.begin();

  /*MPU setup*/
  Wire.begin();
#if ARDUINO >= 157
  Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
  TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif

  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }

  /* Set kalman and gyro starting angle */
  while (i2cRead(0x3B, i2cData, 6));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  // atan2 outputs the value of -¥ð to ¥ð (radians) - see http://en.wikipedia.org/wiki/Atan2
  // We then convert it to 0 to 2¥ð and then from radians to degrees
  accYangle = (atan2(accX, accZ) + PI) * RAD_TO_DEG;
  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  kalmanX.setAngle(accXangle); // Set starting angle
  kalmanY.setAngle(accYangle);
  gyroXangle = accXangle;

  timer = micros();
}


void loop() {


  /* Update all the IMU values */

  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);

  gyroX = ((i2cData[8] << 8) | i2cData[9]);

  accXangle = (atan2(accY, accZ) + PI) * RAD_TO_DEG;

  double gyroXrate = (double)gyroX / 131.0;

  gyroXangle += gyroXrate * ((double)(micros() - timer) / 1000000); // Calculate gyro angle without any filter

  kalAngleX = kalmanX.getAngle(accXangle, gyroXrate, (double)(micros() - timer) / 1000000); // Calculate the angle using a Kalman filter

  timer = micros();

  double CurX = (abs(kalAngleX - 270));

  /*FORCE SENSOR*/
  int SensorReading0 = analogRead(SensorPin0);
  int SensorReading1 = analogRead(SensorPin1);
  int SensorReading2 = analogRead(SensorPin2);
  int SensorReading3 = analogRead(SensorPin3);

  int avg0 = mySensor0.reading(SensorReading0);
  int avg1 = mySensor1.reading(SensorReading1);
  int avg2 = mySensor2.reading(SensorReading2);
  int avg3 = mySensor3.reading(SensorReading3);

  double F0 = 0.003462 * exp(0.0195 * avg0); //GOOD
  double F1 = 0.00065 * exp(0.01889 * avg1); //good
  double F2 = 0.003954 * exp(0.01884 * avg2); //GOOD
  double F3 = 0.000777 * exp(0.02288 * avg3); //good

  double MeanDataforce = (F0 + F1 + F2 + F3);


  F_h =  (uint8_t)(MeanDataforce);
  F_l = (uint8_t)(MeanDataforce * 100) % 100;



  pAngle_h = (uint8_t)(CurX);
  pAngle_l = (uint8_t)(CurX * 100) % 100;

  myTransfer.txBuff[0] = '!';
  myTransfer.txBuff[1] = pAngle_h;
  myTransfer.txBuff[2] = pAngle_l;
  myTransfer.txBuff[3] = F_h;
  myTransfer.txBuff[4] = F_l;
  myTransfer.txBuff[5] = '?';

  myTransfer.sendData(6);

  //   Serial.print(CurX);
  //  Serial.print(" ");
  //  Serial.print(pAngle_h);
  //  Serial.print(" ");
  //  Serial.println(pAngle_l);
  //  Serial.print(" ");
  //  Serial.println(F_h);
  //
  //
  //  Serial.print(" ");
  //  Serial.print(F0);
  //  Serial.print(" ");
  //  Serial.print(F1);
  //  Serial.print(" ");
  //  Serial.print(F2);
  //  Serial.print(" ");
  //  Serial.println(F3);

}

Cont.

Finally the master code:

#include <stdio.h>
#include<movingAvg.h>
#include "SerialTransfer.h"

SerialTransfer myTransferR;
SerialTransfer myTransferL;

int buttonState = 0;
uint8_t buf_r[6], buf_l[6];
uint8_t old_buf_r[6], old_buf_l[6];
uint8_t pack_r[6], pack_l[6];
uint8_t intact_pack_r[6], intact_pack_l[6];


movingAvg encvel(10);
movingAvg enc(10);



int Encoder_pin[10] = {25, 27, 29, 31, 33, 35, 37, 39, 41, 43};
int Encoder_state[10];
int Encoder_sum = 0;
double Encoder_deg_new = 0, Encoder_deg_old = 0;
double E_vel_deg_new, E_vel_deg_old, Encoder_vel, Encoder_vel_rpm;
int Encoder_revcnt = 0;
double A_encoder = 0;
char Value[10];
unsigned int count = 0;
volatile byte state = 0;
double encavg = 0;
double torque = 0;

double Encoder_offset = 0;

float seno;
int frequencia;

unsigned long previousMicro = 0;
const long interval = 1000;

int flagr = 0;
int flagl = 0;

int temp;

boolean newData = false;

void setup() {


  for (int i = 0; i < 10; i++) {
    pinMode(Encoder_pin[i], INPUT);
  }

  Serial.begin(115200);

  //RIGHT PEDAL
  Serial3.begin(115200);
  myTransferR.begin(Serial3);

  //LEFT PEDAL
  Serial2.begin(115200);
  myTransferL.begin(Serial2);
  encvel.begin();
  enc.begin();


}


void loop() {

  //RECEIVE DATA FROM RIGHT PEDAL
  while (!Serial3.available());

  if (myTransferR.available()) {
    for (byte i = 0; i < myTransferR.bytesRead; i++)
    {

      buf_r[i] = myTransferR.rxBuff[i];

    }
  }
  else if (myTransferR.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransferR.status);
  }

  //RECEIVE DATA FROM LEFT PEDAL
  while (!Serial2.available());

  if (myTransferL.available()) {
    for (byte i = 0; i < myTransferL.bytesRead; i++)
    {

      buf_l[i] = myTransferL.rxBuff[i];

    }
  }
  else if (myTransferL.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransferL.status);
  }

  unsigned long currentMicro = micros();

  if ((unsigned long)(micros() - previousMicro) >= interval) {

    previousMicro = micros(); // keep in sync with the time



    //ENCODER CALCULATION
    Encoder_deg_old = Encoder_deg_new;
    E_vel_deg_old = E_vel_deg_new;

    for (int i = 0; i < 10; i++) {
      Encoder_state[i] = digitalRead(Encoder_pin[i]);
    }

    Encoder_sum = 0;

    int Encoder_cnt = 0;

    for (int Encoder_cnt = 0; Encoder_cnt < 10; Encoder_cnt++) {
      Encoder_sum += Encoder_state[Encoder_cnt] << Encoder_cnt;
    }
    int encavg = enc.reading(Encoder_sum);

    Encoder_deg_new = ((long)((((double)Encoder_sum * 0.3515625) - Encoder_offset + 360) * (long)(100))) % 36000 / 100.0;


    int i, j, p, val, k1_r, k2_r, k1_l, k2_l = 0;
    int t1_r, t2_r, t1_l, t2_l = 0;
    char checknumberL = '?';
    char checknumberR = '&';


    ///////////////////raw data check FROM PEDALS

    //RIGHT
    //    for (int i = 0; i < 6; i++) {
    //      Serial.print(buf_r[i]);
    //      Serial.print('\t');
    //    }
    //    Serial.print('\n');

    //LEFT
    for (int i = 0; i < 6; i++) {
      Serial.print(buf_l[i]);
      Serial.print('\t');
    }
    Serial.print('\n');


    //data processing

    /////////////////Right//////////////////////


    if (buf_r[5] == checknumberR) {
      for (j = 0; j < 6; j++) {
        pack_r[j] = buf_r[j];
      }
      for ( i = 0; i < 6 - 1; i++) {
        if ( pack_r[i] == checknumberR ) {
          for (j = 0; j < 6; j++) {
            pack_r[j] = intact_pack_r[j];
          }
        }
      }
      for (j = 0; j < 6; j++) {
        intact_pack_r[j] = pack_r[j];
      }
    }
    else {
      for (int k = 0; k < 6; k++) {
        if (buf_r[k] == checknumberR) {
          k1_r = k;
          k2_r = 4 - k;
          while (k1_r >= 0) {
            pack_r[5 - t1_r] = buf_r[k1_r];
            k1_r--; t1_r++;
          }
          while (k2_r >= 0) {
            pack_r[k2_r] = old_buf_r[5 - t2_r];
            k2_r--; t2_r++;
          }
          break;
        }
        else {
          ;
        }
      }
      for ( i = 0; i < 6 - 1; i++) {
        if ( pack_r[i] == checknumberR ) {
          for (j = 0; j < 6; j++) {
            pack_r[j] = intact_pack_r[j];
          }
        }
      }
      for (j = 0; j < 6; j++) {
        intact_pack_r[j] = pack_r[j];
      }
    }
    if (pack_r[0] != '

So, the problem is, even if I use only one slave(remove the other from the code), I receive the data, the -1 error appears a lot of times and after a little while the data stops updating and freezes.

Thank you so much for your time and patience. Hope you can help me find the problem.) {
      for (j = 0; j < 6; j++) {
        pack_r[j] = intact_pack_r[j];
      }
    }
    for (j = 0; j < 6; j++) {
      old_buf_r[j] = buf_r[j];
    }

///////////////////left////////////////////

if (buf_l[5] == checknumberL) {
      for (j = 0; j < 6; j++) {
        pack_l[j] = buf_l[j];
      }
      for ( i = 0; i < 6 - 1; i++) {
        if ( pack_l[i] == checknumberL ) {
          for (j = 0; j < 6; j++) {
            pack_l[j] = intact_pack_l[j];
          }
        }
      }
      for (j = 0; j < 6; j++) {
        intact_pack_l[j] = pack_l[j];
      }
    }
    else {
      for (int k = 0; k < 6; k++) {
        if (buf_l[k] == checknumberL) {
          k1_l = k;
          k2_l = 4 - k;
          while (k1_l >= 0) {
            pack_l[5 - t1_l] = buf_l[k1_l];
            k1_l--; t1_l++;
          }
          while (k2_l >= 0) {
            pack_l[k2_l] = old_buf_l[5 - t2_l];
            k2_l--; t2_l++;
          }
          break;
        }
        else {
          ;
        }
      }
      for ( i = 0; i < 6 - 1; i++) {
        if ( pack_l[i] == checknumberL ) {
          for (j = 0; j < 6; j++) {
            pack_l[j] = intact_pack_l[j];
          }
        }
      }
      for (j = 0; j < 6; j++) {
        intact_pack_l[j] = pack_l[j];
      }
    }

if (pack_l[0] != '?') {
      for (j = 0; j < 6; j++) {
        pack_l[j] = intact_pack_l[j];
      }
    }
    for (j = 0; j < 6; j++) {
      old_buf_l[j] = buf_l[j];
    }

//CONVERTING RECEIVED DATA
    float pi = 3.14159;
    float encorderrad = Encoder_deg_new * (pi / 180);
    float AR = (pack_r[1]) + (pack_r[2]) * 0.01;
    float FR = (pack_r[3]) + (pack_r[4]) * 0.01;
    float AL = (pack_l[1]) + (pack_l[2]) * 0.01;
    float FL = (pack_l[3]) + (pack_l[4]) * 0.01;
    float Rangrad = AR * (pi / 180);
    float Langrad = AL * (pi / 180);
    float torque = (0.17 * 0.022539446 / 0.089013718) * (FR * cos(encorderrad - Rangrad) + FL * cos((encorderrad + pi) - Langrad));

//Serial.print(Encoder_deg_new);
    //Serial.print(" ");
    //Serial.println(360);

//      if((pack_l[0] == 200) &&
    //        (pack_r[0] == 200)
    //        &&(pack_l[1] < 180) &&
    //        (pack_r[1] < 180) &&
    //        (pack_l[2] != pack_l[1]||pack_l[0]) &&
    //        (pack_r[2] != pack_r[1]||pack_r[0])
    //        )
    //        {
    ////      Serial.print(Encoder_deg_new);
    ////      Serial.print(" ");
    //
    //      for (int i = 0; i < 6; i++) {
    //      Serial.print(pack_l[i]);
    //      Serial.print("\t");
    //      }
    //      Serial.print(" ");
    //      for (int i = 0; i < 6; i++) {
    //      Serial.print(pack_r[i]);
    //      Serial.print("\t");
    //      }
    //      Serial.println("");
    //      Serial.println(AL);
    ////      Serial.print(" ");
    ////      Serial.print(FL);
    ////      Serial.print(" ");
    //      Serial.println(AR);
    //      Serial.print(" ");
    //      Serial.println(FR);
    ////      Serial.print(" ");
    ////      Serial.println(torque);

//      }
    //
    //      else if((pack_l[0] != 200) ||
    //        (pack_r[0] != 200)){
    //        Serial.print("");
    //        }

}

}


So, the problem is, even if I use only one slave(remove the other from the code), I receive the data, the -1 error appears a lot of times and after a little while the data stops updating and freezes.

Thank you so much for your time and patience. Hope you can help me find the problem.

-1 is a crc error - are there ever ANY packets that are successful? The reason I ask this is because I want to make sure there isn't a strange discrepancy between how the slaves are calculating the crc vs how the master is calculating crc.

Are you sure you're using the latest version of the library? What version are you running.

Also, if the transmitter is sending data faster than the receiver can read it, you'll get errors due to buffer overflow (as your title suggests). What is the TX data rate of one of the pedal devices? What is the overhead of your main code (I see a TON of nested for loops - bad bad bad!)?

As a point of style, you should use functions more often. They make your code more readable and portable. (i.e. the two massive for loop nests (one for each pedal) could be in their own function that you call twice - once for each pedal with an argument for what pedal you want to process.