# EEPROM Writing with Functions

**URL:** <https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979>\
**Category:** Storage\
**Created:** [October 4, 2024, 1:55pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979 "2024-10-04T13:55:04Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 11:17pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/21 "2024-10-04T23:17:52Z")

</div>

> [@malklor](#):
>
> I am not sure why other were able to get the original code to work on there system

i suppose, bc they written right values in EEPROM with working sketch and then bad sketch just reads right values from EEPROM, so seems everything works.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [October 4, 2024, 11:17pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/22 "2024-10-04T23:17:59Z")

</div>

Here is an example using .put() and .get() with the names of the struct arrays as proposed.

EDIT: This is not correct. It does not appear to read the data if the write is commented out of the sketch and it is reloaded.

```arduino
#include <EEPROM.h>

struct highscore {
  byte attempts;
  char name[4];
};

struct highscore highScores[5];

struct highscore dftHighScores[5] = {
  {10, "AAA"},
  {20, "BBB"},
  {30, "CCC"},
  {40, "DDD"},
  {50, "EEE"}
};

//this will get filled by the data from .get()
struct highscore read_dftHighScores[5] = {
  {},
  {},
  {},
  {},
  {}
};

void setup() {
  Serial.begin(9600);
  delay(2000);

  // Setup Default High Scores
  Serial.println("Setup Default HighScores");
  writeHighScore(dftHighScores);

  //Read BackHigh Scoores
  Serial.println("ReadBack Default HighScores");
  readHighScore(read_dftHighScores);
}

void loop() {}

void readHighScore(highscore read_dftHighScores[])
{
  EEPROM.get(50, read_dftHighScores);
  delay(250);
  for (int i = 0; i < 5; i++)
  {
    HighScoreLog(read_dftHighScores[i]);
    Serial.println("+++++++++++++++++++++++");
  }
}

void writeHighScore(highscore writeScore[])
{

  for (int i = 0; i < 5; i++)
  {
    HighScoreLog(writeScore[i]);
    Serial.println("+++++++++++++++++++++++");
  }

  EEPROM.put(50, writeScore);
  delay(250);
}

void HighScoreLog(struct highscore temp)
{
  Serial.println();
  Serial.print(" Name = ");
  Serial.println(temp.name);
  Serial.print(" Attempts = ");
  Serial.println(temp.attempts);
}
```

Output:

```arduino
16:14:59.047 -> Setup Default HighScores
16:14:59.047 -> 
16:14:59.047 -> Name = AAA
16:14:59.093 -> Attempts = 10
16:14:59.093 -> +++++++++++++++++++++++
16:14:59.139 -> 
16:14:59.139 -> Name = BBB
16:14:59.139 -> Attempts = 20
16:14:59.139 -> +++++++++++++++++++++++
16:14:59.186 -> 
16:14:59.186 -> Name = CCC
16:14:59.186 -> Attempts = 30
16:14:59.233 -> +++++++++++++++++++++++
16:14:59.233 -> 
16:14:59.233 -> Name = DDD
16:14:59.233 -> Attempts = 40
16:14:59.279 -> +++++++++++++++++++++++
16:14:59.279 -> 
16:14:59.279 -> Name = EEE
16:14:59.320 -> Attempts = 50
16:14:59.320 -> +++++++++++++++++++++++
16:14:59.552 -> ReadBack Default HighScores
16:14:59.784 -> 
16:14:59.784 -> Name = AAA
16:14:59.830 -> Attempts = 10
16:14:59.830 -> +++++++++++++++++++++++
16:14:59.876 -> 
16:14:59.876 -> Name = BBB
16:14:59.876 -> Attempts = 20
16:14:59.876 -> +++++++++++++++++++++++
16:14:59.922 -> 
16:14:59.922 -> Name = CCC
16:14:59.922 -> Attempts = 30
16:14:59.922 -> +++++++++++++++++++++++
16:14:59.967 -> 
16:14:59.967 -> Name = DDD
16:14:59.967 -> Attempts = 40
16:15:00.014 -> +++++++++++++++++++++++
16:15:00.014 -> 
16:15:00.014 -> Name = EEE
16:15:00.014 -> Attempts = 50
16:15:00.060 -> +++++++++++++++++++++++

```

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 11:30pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/23 "2024-10-04T23:30:53Z")

</div>

output is fine, but still not working. `read_dftHighScores` still empty

```arduino
#include <EEPROM.h>

struct highscore {
  byte attempts;
  char name[4];
};

// highscore highScores[5];

highscore dftHighScores[5] = {
  {10, "AAA"},
  {20, "BBB"},
  {30, "CCC"},
  {40, "DDD"},
  {50, "EEE"}
};

//this will get filled by the data from .get()
highscore read_dftHighScores[5] = { {}, {}, {}, {}, {}};

void setup() {
  Serial.begin(9600);

  writeHighScore(dftHighScores); // Setup Default High Scores

  //Read BackHigh Scoores
  Serial.println("ReadBack Default HighScores");
  readHighScore(read_dftHighScores);
  for (int i = 0; i < 5; i++) HighScoreLog(read_dftHighScores[i]);
}

void loop() {}

void readHighScore(highscore read_dftHighScores[])
{
  EEPROM.get(50, read_dftHighScores);
  delay(250);
}

void writeHighScore(highscore writeScore[])
{
  Serial.println("++++++++++++");
  EEPROM.put(50, writeScore);
  delay(250);
}

void HighScoreLog( highscore temp)
{
  Serial.print(" Name = ");
  Serial.print(temp.name);
  Serial.print(" Attempts = ");
  Serial.println(temp.attempts);
}

```

BUT ! it works when

```arduino

void readHighScore(highscore read_d[]) // read_d disappear after return
{
  EEPROM.get(50, read_d);
  delay(250);
  for (int i = 0; i < 5; i++)
  {
    HighScoreLog(read_dftHighScores[i]=read_d[i]); // << ---------- so we write temporary to global and print out
    Serial.println(" ******************");
  }
}

```

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 11:44pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/24 "2024-10-04T23:44:37Z")

</div>

I think you are right when I was testing on my hardware I only tested the bad never the good. So for others, the second run of the bad only read never wrote cause the flag was set and good data was already in eeprom.

That solves that mystery now too!

Nice detective work.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 2, 2025, 11:45pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/25 "2025-04-02T23:45:09Z")

</div>

Closed on April 2, 2025, 11:45pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979.md?page=1)
