Why happen this, arduino UNO

I enter this text in the terminal of the arduino IDE

text -> 2023-03-24 17:50;10180049;100;20190306;40;100;4;8;A;0;35;a;12.8;A;0;35;c;12.8;A;0;35;h;13.3;A;0;35;l;12.1;B;0;11;a;4.0;B;0;11;c;4.2;B;0;11;h;4.4;B;0;11;l;3.3;236C

what I do is detect the value of the variables a, c, h, l that is embedded in the text

when I print the variables there is no problem it shows me all the variables:

void loop() {
  while (Serial.available() > 0) {
    char c = Serial.read();
    if (c != '\n') {
      if (light != false) {
        if (c != ';' && !isspace(c)) {
          data += c;
        } else {
          if (index == 0 && data != "RS") {
            light = false;
          } else {
            if (index == 1) {date = data;}
            if (index == 2) {hour = data;}
            if (index == 3) {retri_id = data;}
            if (index == 4) {retri_charge = data;}
            if (index == 5) {pub_id = data;}
            if (index == 7) {pub_charge = data;}
            if (data == "A" || data == "B" || data == "C" || data == "D") {port = data;}
            if (data == "a" || data == "c" || data == "h" || data == "l") {
              variable = data;
              light_var = true;
              index_var = index + 1;  
            }
            if (light_var == true && index == index_var) {
              value = data;
              light_var = false;
              light_up = true;
            }
            if (light_up == true) {
              Serial.println(value);
              light_up = false;
            }

            data = "";
            index++;
          }
        }
      }
    } else {
      data = "";
      index = 0;
      light = true;
      Serial.println("... Finish");
    }  
  }

}

Screenshot 2023-05-21 at 20.02.11

but when what I do is first form a string and then print it in if (light_up == true) {} , it doesn't print all of them:

           if (light_up == true) {
              //Serial.println(value);
              String urix = date + "|" + hour + "|" + retri_id + "|" + retri_charge + "|" + pub_id + "|" + pub_charge + "|" + port + "|" + variable + "|" + value + "\"";
              Serial.println(urix);
              light_up = false;
            }

What can be ?

How short are you in memory usage (SRAM)?

All your String object manipulation will eat up RAM quickly, then the Arduino will crash.

Best to use C-strings instead (zero terminated character arrays) and the <string.h> library functions.

I’m reading on my phone so can’t see well the nesting and you did not indent the code
It also might be a timing issue with the way you do the reading as you are not guaranteed to read everything in one go. The loop may loop so whatever you collected needs to survive the loop looping

is it possible to shorten this mess? is it not enough to send "202303241750Aa12.8h13.3l12.1Ba4.0c4.2\n" ?

Why do you want to do that? For printing there is no reason to first collect everything in a String or string; just print where it is needed.

Note:
The way you use String results in a stack of temporary String objects being created and they all consume memory. You're better of using a construction like

String urix;
urix +=date;
urix+="|"
...
...

You may reserve sufficient memory space for your String beforehand. That will prevent copying the whole lot when there is a lack of space (leaving a hole in your heap).

1 Like

SRAM 2k bytes

Thanks, I'll try your suggestion.

how I do that ?

I can't cut it, it's what I receive from an external system

Why do I need to build a string that must be sent through a SIM7600 module and when I send it as I receive it, it is cut off and does not send all the information, I will try you suggestion.

You mean indented code? That’s done in the IDE by pressing ctrlT on a PC or cmdT on a Mac

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.