Help understanding char and char arrays && EEPROM

Hello,

This is probably very simple to most of you but I can't get my head around formatting char arrays in Arduino IDE. I understand the basics of char's and use them often in serial comms. I have searched and the only solutions that worked revolve around converting to strings etc, but I have read that is bad practice and wish to keep memory usage low. On my PCB I developed I included a DS1337 and AT24CX EEPROM wired to ATMEGA328P SCL/SDA.

Time/date is char[20], activity/status char, and I wish to add a separator, e.g. "-" between the two. I wish to combine the three into a char array (msg) that can be written to an EEPROM address. Such as address 30, as long as this array does not exceed 30 bytes there will be no overlap. It works when I simply use;

char msg[] = "This is a message";

Here is the code, I have tried to make it easy to read by removing other jargon.

struct time_t time;
char time_str[20];

.....
readRTC();
writeAddress("Idle");
.....


void readRTC() {
    if (ds1337_read_time(&time)) {
      Serial.println("Unable to read time--an I2C error has occured.");
    } else {
      // The time has been read successfully--print it.
      format_time_str(&time, time_str);
    }
}

void writeAddress(char activity) {
    long addressCounter = mem.readLong(10);
    addressCounter += 30;
    Serial.begin(115200);
    char msg[] = time_str & "-" & activity
    Serial.println("EEPROM: Writing: ");
    Serial.println(msg);
    mem.writeChars(addressCounter,msg,sizeof(msg));
    mem.writeLong(10,addressCounter);
}

I am not super switched on as I am new to EEPROM registries and am currently studying on the best practices to avoid blank addresses, basically, I keep a long stored in address 10, this tracks the last recorded address written. The address is then pulled, +30 added, and new data is written. Am I approaching this the wrong way? I thought about using sizeof, adding this to the address counter and writing the next char's however I am unsure how to then read and parse data when required.

Any input much appreciated if you have the time to help ofcourse.

It would help if you posted your sketch. It doesn't appear you are using the Arduino EEPROM library.

I suppose you know this is not valid C/C++ syntax:

    char msg[] = time_str & "-" & activity

You would have to do something like this:

    char msg[31];
    strcpy(msg, time_str);
    strcat(msg, "-");
    strcat(msg, activity);

Mate you’re a champion.

The program is way too long to post here and I thought it would discourage some readers so I kept it brief. However, you picked up exactly what I was after. Thanks mate.

Library is #include <AT24CX.h>, as it is an external EEPROM on a PCB I wasn’t sure if the default EEPROM library from Arduino would attempt to program the internal or external storage.

Thanks again,

buono97:
Mate you’re a champion.

The program is way too long to post here and I thought it would discourage some readers so I kept it brief. However, you picked up exactly what I was after. Thanks mate.

Library is #include <AT24CX.h>, as it is an external EEPROM on a PCB I wasn’t sure if the default EEPROM library from Arduino would attempt to program the internal or external storage.

Thanks again,

Great! Hope it works for you.