2

I am developing a pill reminder for Electronics Final year project. I need to store the name of the pill, the number of times it is taken, the hours it is taken during and whether it is currently active or not. I created a class pill as below, and stored the pills in an array:

class Pill{
    public:
        String pillName = "Nothing";
        boolean pillTaken = true;
        int hours[6]; 
        boolean active = false;
        int count = 0; 
};

Pill pills[6];

Now I want to persist this data in the Arduino EEPROM, how can I write the array of pills to the EEPROM and read the data into memory each time program starts. Also, I need to update the array each time a command to modify a pill is received, putting the new values.

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
  • Not sure if you got my response for the comment you posted on my answer. I didn't @ your name, so this message should give you a notification. – Chris A Apr 27 '16 at 07:20

2 Answers2

3

@Galarzaa90 has pointed you to the right information, however, the EEPROM lib will not work with the String class.

Why?

Because the actual String data is not stored in the objects memory space, it simply contains a pointer to some dynamic memory elsewhere.

If you save a String object you will save its length, buffer size and pointer to data. However, when you restart your duino and load the String from EEPROM, the pointer will be pointing to... anywhere but where you expect.

You'll need to use a cstring/ char array which means the string data is actually stored inside the Pill class.

Chris A
  • 1,475
  • 3
  • 18
  • 22
  • Can I store the `pillName` variable as a char array, and read it back into a `String` because I need to use the functionality of the String? My solution was to create a different class which stores the name as an array and save it to EEPROM, then read it and copy over the information to my `pills` class – Edmore M Gonese Digolodollarz Apr 27 '16 at 01:40
  • You can't read it directly, however if `pillName` it is a null terminated string you can do something like this: `String myString = pillName;` once `pillName` has been loaded from the EEPROM. – Chris A Apr 27 '16 at 02:30
  • I had to add a `\0` as the final character for it to work, and it worked. I am not sure if that is the way to go? – Edmore M Gonese Digolodollarz Apr 30 '16 at 11:25
  • Yeah, the String class expects a null terminator on its inputs. It uses it to determine the length of the cstring to copy. – Chris A Apr 30 '16 at 11:42
  • @EdmoreMGoneseDigolodollarz also, you can see this is default with strings: `sizeof("test")` will equal 5 as it implicitly adds the null character at the end. – Chris A Apr 30 '16 at 11:45
2

EEPROM.put(address, data) let's you store any type of data in the EEPROM. And EEPROM.get(address, data) returns any object from the EEPROM.

#include <EEPROM.h>
[...]
class Pill{
  public:
    String pillName = "Nothing";
    boolean pillTaken = true;
    int hours[6]; 
    boolean active = false;
    int count = 0; 
};
Pill pills[6];
//Assigning data to pills array in here...
[...]
EEPROM.put(address,pills)
[...]
//Getting saved pills array
Pill savedPills[]; //I'm not sure if you'd have to initialize the size...
EEPROM.get(address,savedPills)

You can check complete examples in arduino.cc:

Galarzaa90
  • 182
  • 3
  • 14