Reading from the slave stops

So basically Im stuck in here Im trying to sending a data from master to slave. Theres no problem from transfer data from master to slave, the problem is when I put the tft.print in the if else statements in the slave part the reading stops eventually.

This is the output with commented tft.print

0 Data : 14.43

1 Data : 14.43

2 Data : 14.43

3 Data : 14.43

4 Data : 14.43

5 Data : 14.43
Voltage 1 : 14.43
Voltage 2 : 14.43
Voltage 3 : 14.43
Voltage 4 : 14.43
System Current : 14.43
Temperature : 14.43

And this is the output when I uncomment the lines tft.print in slave code from line 54-86

0 Data : 14.43

1 Data : 14.43

2 Data : 14.43

3 Data :

This is the master code stm32

#include <Wire_slave.h>
float y=14.43;
String str;
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  Serial.print("program starts");
}
void loop()
{
  for(int z=0;z<6;z++){
    int i=0;
    Serial.println("the");
    str=String(y)+"%";
    Wire.beginTransmission(8); // transmit to device #8
    while(str.charAt(i)!='%'){
      Wire.write(str.charAt(i)); // sends one byte
      Serial.print(str.charAt(i));
      i++;
    }
  Wire.endTransmission();    // stop transmitting
  }
  delay(2000);
}

Slave arduino mega with tft hx3857

#include <Wire.h>
#include <TFT_HX8357.h>
#include "Free_Fonts.h"

TFT_HX8357 tft = TFT_HX8357();
int i=0;
char c;
String data_values[5];
void setup() {
  tft.init();  
  tft.setRotation(1);
  
  tft.fillScreen(TFT_BLACK); // Clear screen to navy background
  
  tft.setTextDatum(TC_DATUM);
  tft.drawString("Cell 1 Voltage : ", 80, 70, 2);
  tft.drawString("Cell 2 Voltage : ", 275, 70, 2);
  tft.drawString("Cell 3 Voltage : ", 80, 100, 2);
  tft.drawString("Cell 4 Voltage : ", 275, 100, 2);
  tft.drawString("System Current : ", 80, 190, 2);
  tft.drawString("Temperature : ", 90, 220, 2);
  tft.setTextDatum(TC_DATUM);
  tft.setFreeFont(FSB9);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
//  Serial.println("asfagsd");
String str;
  while (1 <= Wire.available()) { // loop through all but the last
    c= Wire.read(); // receive byte as a character
    //Serial.print(c);         // print the character
    str+=c;
  }
  data_values[i]=str;
  Serial.println();
  Serial.print(i);
  Serial.print(" ");
  Serial.print("Data : ");
  Serial.println(data_values[i]);
  i++;
  if(i == 6){
    for(int data = 0; data < 6; data++){
        if(data == 0){
          Serial.print("Voltage 1 : ");
          Serial.println(data_values[data]);
          //tft.setCursor(170,70);
          //tft.print(data_values[data]);
        }
         else if(data == 1){
          Serial.print("Voltage 2 : ");
          Serial.println(data_values[data]);
          //tft.setCursor(370,70);
          //tft.print(data_values[data]);
        }
        else if(data == 2){
          Serial.print("Voltage 3 : ");
          Serial.println(data_values[data]);
          //tft.setCursor(170,100);
          //tft.print(data_values[data]);
        }
        else if(data == 3){
          Serial.print("Voltage 4 : ");
          Serial.println(data_values[data]);
          //tft.setCursor(370,100);
          //tft.print(data_values[data]);
        }
        else if(data == 4){
          Serial.print("System Current : ");
          Serial.println(data_values[data]);
          //tft.setCursor(150,190);
          //tft.print(data_values[data]);
        }
        else if(data == 5){
          Serial.print("Temperature : ");
          Serial.println(data_values[data]);
          //tft.setCursor(150,220);
          //tft.print(data_values[data]);
        }
        else{
          Serial.print("System error : ");
          Serial.println(data);
        }
    }
    i=0;
  }
}

Im debugging this for couple of hours already dont know the problem but maybe issues between serialprint or tft print maybe? Any idea of whats the problem or how to solve this problem

Well, for one thing, every time you have a "String" variable in a function a new "String" is created whenever that function is executed. So the memory for a new string slowly eats up all the available memory until your program crashes.

So, look into using "string" instead.

Paul

Paul_KD7HB:
Well, for one thing, every time you have a "String" variable in a function a new "String" is created whenever that function is executed. So the memory for a new string slowly eats up all the available memory until your program crashes.

So, look into using "string" instead.

Paul

So instead of declaring the str for it to be empty , should I set str into this str='' to be a blank value? Am i wrong or I am really wrong?

So this is another question why the program stops when I put the tft.print , but when it is commented or it does not exist in the program the reading is good eventually it doesnt stop at all.

Keep the receiveEvent() function as small as possible. Don't transfer readable data like strings, but use binary variables or an array or a struct.
Don't use tft functions in receiveEvent, no Serial functions, no delay.
You could fill a buffer with the command, and process that command in the loop().

Can you describe what kind of data that you want to send from one Arduino board to the other one ? Only one number ?

Eventually I fixed my problem by declaring str as a blank value again

Koepel:
Can you describe what kind of data that you want to send from one Arduino board to the other one ? Only one number ?

Base from the master code i posted I will send a float data to a slave. The way that I used is to cast the float into string and send it to slave character by character. This is the method just I know, there is some forum says use a union method and struct but im not really good at programming. It takes time to understand that kind of thing wanna be programmer thought

You could transfer the float. In the loop() of the Slave Arduino you could convert it to text for the display.

Do you know I2C_Anything ?
You will see an example there how to use received data in the loop(). The variable 'haveData' tells if new data has arrived. That way you can do all the tft functions in the loop().

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R