Make arduino create variables

Hi guys just a simple curiosity, I was thinking about making a menu for an irrigation sistem. Part of the menu would ask the user to enter how many times do they want the soil sensor to measure moisture (ex. If they set it to 24, it would check once per hour).

I was thinking of making the arduino create new variables for the number of times the soil sensor will go off, so the arduino can store each reading in a new variable.

My question is: is there a way to make the arduino create new variables as needed? If so, how? If not, how could i go about doing this?

Thanks a lot.

You can use an array; it needs to big enough to store the maximum numbet of readings per day (or week or month or whatever).

E.g float data[24] allows you to store 24 readings (e.g. one per hour for a day). Use the appropriate type.

You could make a function which takes the array size as a parameter. Then, inside your function you could create the array with that size and do all reading + calculation there.

Depending on how many you want to store, you might run out of memory.
Keeping a log like this is more a job for an SD card in my view.
Look up data logger shields on ebay.

arduino_new:
You could make a function which takes the array size as a parameter. Then, inside your function you could create the array with that size and do all reading + calculation there.

This approach can cause problems in the small memory of an Arduino. It is better to create the arrays at compile time and make them as big as the largest that will be required.

...R

boylesg:
Depending on how many you want to store, you might run out of memory.
Keeping a log like this is more a job for an SD card in my view.
Look up data logger shields on ebay.

+1

...R

Robin2:
This approach can cause problems in the small memory of an Arduino. It is better to create the arrays at compile time and make them as big as the largest that will be required.

...R

The array will be created on stack so it won't hurt.

arduino_new:
The array will be created on stack so it won't hurt.

Do you mean create or declare ?
If the latter then the array will not be available to the rest of the program

arduino_new:
The array will be created on stack so it won't hurt.

The stack shares SRAM with everything else.

...R

Donsa:
Hi guys just a simple curiosity, I was thinking about making a menu for an irrigation sistem. Part of the menu would ask the user to enter how many times do they want the soil sensor to measure moisture (ex. If they set it to 24, it would check once per hour).

I was thinking of making the arduino create new variables for the number of times the soil sensor will go off, so the arduino can store each reading in a new variable.

My question is: is there a way to make the arduino create new variables as needed? If so, how? If not, how could i go about doing this?

Thanks a lot.

why would you need a bunch of new variables to store an interval?

arduino_new:
The array will be created on stack so it won't hurt.

It will not be reported by the IDE. But that does not mean that it will work :wink: Something in the line of below is simply calling for trouble.

void setup()
{
  Serial.begin(115200);
  Serial.print("A5 = "); Serial.println(A5);
}

void loop()
{
  byte data[2048];

  for(int cnt=0;cnt<2048;cnt++)
  {
    data[cnt]++;
    Serial.print(data[cnt], HEX);
  }
}

It does not even print the value of A5.

  for(int cnt=0;cnt<2048;cnt++)

Patient:
Doc, it hurts when I do this...

Doctor:
Well, don't do that!

creating an array on the stack isn't a problem, until you do something dumb... like a) creating a monster of an array (demonstrated) or b) reading/writing beyond the end of your array (same problem in the heap);

it is common to see an array created in a function (e.g. a string buffer) and Arduino can handle that very easily. You have a stack, so you may use it.

If there is not defined an upper or max limit for the taken measurements in this case variables or elements in array then there is very high possibility to run out of any memory available. How is it possible to deal with infinite saved values in finite storage?
First i would determine whats the limit that makes sense for your project then will go from there if 10 readings then make array of 10 elements of appropriate type if 1000 etc then decide if toucan fit in flash if not fit in eeprom(taking into consideration max write cycles), if thats not enough then sd shield and so on.

Thanks guys, so helpful! Ill go with an array like many suggested. I just didnt want to do create the variables, of course i can write 24 variables and leave them there waiting, but i thought there was a better way to do it. Also thanks for the sd card tip, i was thinking about something like that but i didnt know where to start.