Reading settings into an array

Hi

I am planning a new project for my terrarium and was wondering if I could do the following:

Create a comma separated string in a text editor and save it as file 'settings.txt'. This file basically contains only one string, nothing more.

I would then like to load the file contents (one string) as values for an array in the sketch.

Int Settings = { file contents here }.

Is this possible using a mini SD card reader?

The goal is to update a set of settings (when needed) without removing the arduino or disconnecting any of it's sensors to get it hooked up to the computer again.

There a are multiple examples of reading and writing to an SD so that will not be the problem, however I wonder how to get that content between the curly brackets before anything is executed.

Any hints?

Thanks in advance!

You can't. You need to assign values after the declaration. It will be something like

// settings
int Settings[5];

void setup()
{
  // read settings from file
  ...
  // parse
  ...
  // assign
  Settings[0] = ...;
  Settings[1] = ...;
  Settings[2] = ...;
  Settings[3] = ...;
  Settings[4] = ...;
}

Thanks Sterretje,

So I would have to assign a value to each 'Setting[n]'?

If I read a file like that there would be no need for an array then is there? I mean, instead of assigning a file value to Settings[0], I could also parse it to the correct Integer like 'Temperature = Read Value'.

Or did I miss your point?

Thanks.

snewpers:
If I read a file like that there would be no need for an array then is there? I mean, instead of assigning a file value to Settings[0], I could also parse it to the correct Integer like 'Temperature = Read Value'.

That is correct. Well, except for your example syntax. But the concept is correct.

Thank you Aarg,

I was really trying to figure out how to get that array to work and couldn't get it to compile. It would have been nice if it would have worked. I planned on a simple PHP site where I could adjust the settings and download the file with the string.

I now have to figure out how to get te correct data assigned to the integer. We don't want humidity [85]and temperature 34C mixed up :smiley:

How many data items?

About 30-40 datasettings to configure sequences for light, heat, moisture and airflow.

providing you always send data in the same order and always send the full load of data it should be fairly easy.

For example
Byte
0-1 Temp
1-2 Humidity
3 Light on / Off

Call the full set of data package and then ensure that every time you make any changes the full package is used. So if you wanted to change temp only you would modify that integer and then send all the other information as the same values as before.

Note that the above style of bytes assumes you are sending data in binary not ascii format but the same principle applies but maybe always used commas or line ending to seperate the data for parsing :slight_smile:

Thanks sizer

I don't really understand what you were typing there, even though it looked pretty easy :slight_smile:

It works, but would be error prone if anything has to be added and also tricky to edit the data file.

This is not as easy as I hoped it would be :frowning: (for once)…

Anyway, after searching a lot more on this forum I found a post with this code:

//
myFile = SD.open("test2.txt"); 
   while (myFile.available()){
     for (fieldIndex = 0; fieldIndex <11; fieldIndex ++){
       Main_Array[fieldIndex] = myFile.parseInt();}
       Serial.print( fieldIndex);
       Serial.println(" fields received:");
       for(int i=0; i< 11; i++){
         Serial.println(Main_Array[i]);}
   }
   myFile.close();
}
  void loop()
  {
  }

In this example they are using a CSV file (where mine would only have one line wit data instead of multiple entries). Is it correct that there are 12 ‘fields’ imported into the Main_Array? Is it 'int Main_Array[12]={0,2,3,…11} after importing the file from the SD? That would be my solution since Ido have a fixed number of fields (only numbers). All the data does is set daytime/nightime, some temp values and various on/off values (0 or 1). There rest is done inside the sketch.

Would this work for me?

Yep, something like that will work. If you use a number of #define statements, you can also access the array elements by 'name'.

#define TEMPERATURE 0   // first element of array
#define HUMIDITY 1      // second element of array
...
...

void loop()
{
  int temperature = yourArray[TEMPERATURE];
}