title: Reading and Writing Structs to and from Arduino's EEPROM
body: <p>I'm trying to write data structures defines in C to my Arduino Uno board's non-volatile memory, so the values of the struct will be retained after the power goes off or it is reset.
  To my understanding, the only way to do this (while the sketch is running) would be to write to arduino's EEPROM. Although I can write individual bytes (sets a byte with value 1 at address 0): </p>
  
  <pre><code>eeprom_write_byte(0,1);
  </code></pre>
  
  <p>I am stuck trying to write a whole struct:</p>
  
  <pre><code>typedef struct NewProject_Sequence {
      NewProject_SequenceId sequenceId;
      NewProject_SequenceLength maxRange;
      NewProject_SequenceLength minRange;
      NewProject_SequenceLength seqLength;
      NewProject_SceneId sceneList[5];
  } NewProject_Sequence;
  </code></pre>
  
  <p>Because of the EEPROM's limit of 100,000 writes, I don't want to write to the Arduino in a loop going through each byte, for this will probably use it up pretty fast. Does anyone know a more efficient way of doing this, either with EEPROM or if there's a way to write to PROGMEM while the sketch is running? (without using the Arduino Library, just C).</p>
  
  <p><strong>RESOLVED</strong>
  I ended up writing two custom functions -- eepromWrite and eepromRead. They are listed below:</p>
  
  <pre><code>void eepromRead(uint16_t addr, void* output, uint16_t length) {
      uint8_t* src; 
      uint8_t* dst;
      src = (uint8_t*)addr;
      dst = (uint8_t*)output;
      for (uint16_t i = 0; i &lt; length; i++) {
          *dst++ = eeprom_read_byte(src++);
      }
  }
  
  void eepromWrite(uint16_t addr, void* input, uint16_t length) {
      uint8_t* src; 
      uint8_t* dst;
      src = (uint8_t*)input;
      dst = (uint8_t*)addr;
      for (uint16_t i = 0; i &lt; length; i++) {
          eeprom_write_byte(dst++, *src++);
      }
  }
  </code></pre>
  
  <p>The would be implemented like this: </p>
  
  <pre><code>uint16_t currentAddress;
  struct {
      uint16_t x;
      uint16_t y;
  } data;
  
  struct {
  
  } output;
  uint16_t input
  
  eepromWrite(currentAddress, data, sizeof(data);
  eepromRead(currentAddress, output, sizeof(data));
  </code></pre>