0

I appreciate if someone can help me. I have a function that counts the time and I would like to store values according to the day.

For example: day_7.11.2015 = [gain = 100.23, cost = 78.0 ...] day_8.11.2015 = ...

I do not know how can I create an array with these values taking as input the day, month and year. Does anyone have any idea?

String test = "day_" + String(day) + "." + String(month) + "." + String(year); float String(test)[] = {11.2, 10, 20, 31.2}; Serial.println(test);

Thanks a lot!

  • What is your end goal? Is this an Arduino-related question? Please edit question and state the general idea before obscure details. – James Waldby - jwpat7 Nov 7 '15 at 16:09
0

What you're talking about is often called a Hash or a Map or sometimes both together as a HashMap.

Unfortunately it's a facility that isn't supported in the Arduino C++ language

There are map objects available in normal C++, but they are incredibly expensive to use with regards to memory and they just aren't practical for use on a small microcontroller with memory measured in the kilobytes.

The closest you can probably get is to "invert" the storage, so you have a list of values that happen to be in date order (because that's when you added them) but the "key", a simple sequential number, doesn't define the date. Instead you store the date inside the array along with the data - assuming you really need to know the date.

A good concept to get to grips with for this is the struct. This is a custom data type which you define and can store a number of other different data types within itself. You can then make an array or linked list out of those new data types.

For instance, you may have:

typedef struct {
    int year;
    byte month;
    byte day;
    float gain;
    float cost;
} Data;

You can then create variables of type "Data" and assign values to the items within it:

Data myEntry;
myEntry.year = 2015;
myEntry.month = 11;
myEntry.day = 7;
myEntry.gain = 100.23;
myEntry.cost = 78.0;

Or assign them directly at creation time:

Data myEntry = {2015, 11, 7, 100.23, 78.0};

If you wanted to store 100 days' worth of data you could use an array:

Data myArray[100];

Then you access each slice:

myArray[0].year = 2015;
... etc ...

I mentioned above "assuming you really need to know the date". Most of the time with this kind of method you really don't need to store the date. If you have one entry per day and you know when you started gathering data then you know that each entry is one day more than the one before it - so you know that the tenth entry is ten days later than when you started gathering data. Alternatively, if you have a 100 entry circular array (that is, when you reach the end of the array you start filling from the bottom again, so you keep the last 100 days' worth of data) and you know what the date is today you can count backwards from today to work out what date each entry is.

  • Hi Majenko, I tried to solve the problem with structs, but I'm not sure how it works. I could not even run the examples I found on the internet. Your example worked here. But I can not access the values that I want. How should I do if I want to access the value of the variable cost that I created? Is printing another value when I try :| – user14660 Nov 7 '15 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.