Global variables

Hi,

I'm trying to read data from a .txt file to a global variable, but it's not working. After i'm uploading the following code (The variable AAA is daclared as a global variable: String BBB;)

void readFile() {
  Serial.println("Opening data file");
  File dataFile = FileSystem.open("/mnt/sda1/arduino/config.txt", FILE_READ);
  Serial.println("Start reading data from file");

  while (dataFile.available()) {
    String BBB = dataFile.readString();
    Serial.println(BBB);
    AAA = BBB;
    Serial.println(AAA);
    Serial.println("AAA variable isn't printed");
  }
  dataFile.close();
}

The output on the serial monitor is :

Opening data file
Start reading data from file
Data from confix.txt file

AAA variable isn't printed

As you can see AAA variable isn't printed to the serial monitor, while BBB variable is printed to the serial monitor. Is anybody have a clue why is that happening? *** Also i tried to read the data stright to the global variable AAA, and i got the same problem.

Thanks.

This is only a snippet of your code, please post the whole sketch. It's likely the actual problem is elsewhere, probably in the definitions of your variables (or something else that conflicts with them.) The error is likely to be very subtle.

ShapeShifter: This is only a snippet of your code, please post the whole sketch. It's likely the actual problem is elsewhere, probably in the definitions of your variables (or something else that conflicts with them.) The error is likely to be very subtle.

The whole code is about 550 lines :\ I think it somehow related to memory.

OK, maybe it is memory. But without seeing the code, how is anybody going to be able to help?

ShapeShifter: OK, maybe it is memory. But without seeing the code, how is anybody going to be able to help?

Now i'm sure that it's a memory problem. The string that is in config.txt is:

EMS_SERVER_IP=82.80.151.219;EMS_PORT=8002;UPDATES_SERVER_IP=0;UPDATES_PORT=0;NETWORK=1;MIXED=0;STAND_ALONE=0;SAMPLING_TIME_INTERVAL=30000;SENSOR_PIN=1;MANUAL_CHOICE=0;MAX_THRESHOLD=255;MIN_THRESHOLD=112;TOLERANCE=10;HEADER_0B_TO_2B=01,00,48;CPM_ID=51,51,51,51,51,01;CPM_CONFIG=00,00,100,04,00,00,00,01;CPM_INFO_1B_TO_7B=00,00,00,00,00,00,00;RESERVED=00,00,00,00,00,00,00;SU_ID=10,00,00,01;DT1_5B_TO_11B=210,00,05,03,00,00,00;

so when i cut the string to:

EMS_SERVER_IP=82.80.151.219;EMS_PORT=8002;UPDATES_SERVER_IP=0;UPDATES_PORT=0;NETWORK=1;MIXED=0;STAND_ALONE=0;

it works fine.

how can i overcome this issue?

vicmerlis: how can i overcome this issue?

By making the string shorter like you just did? :grin:

It's hard to tell without seeing the rest of the code. What are you doing with the string? Can you read it and process it in small chunks? It looks like you are reading a bunch of network parameters: can you do this processing and network communications on the Linux side of things where you aren't so restricted by memory?

It's still strange that you couldn't read that data directly into the AAA variable. Are you sure there isn't something up with the declaration of that variable? And why do you need two copies of such a long string?

Without seeing your code, or knowing a lot more about what you're trying to do, it's hard to make any suggestions. All anybody can do is talk in generalities.

In general, my code need to pull configurations from txt file so i don't need to change the configurations through the code, i can change them from a web interface (that push the configurations by php to the txt file).

My solution how to save memory: i decided to convert most of the code functions to python scripts, beacause of the memory limit (28kbit). Till now the code reachted to 82% of memory.

Any suggestions how to save memory?

Thanks.