user1645617
user1645617

Reputation: 1

Writing Data to Arduino EEPROM

This is a follow-up to the post here - Writting data to the Arduino's onboard EEPROM I just tried using the snippets in the URL but wouldn't work. Please help me fix the below error.

write_to_eeprom.cpp:8:5: error: expected unqualified-id before '[' token
write_to_eeprom.cpp: In function 'void setup()':
write_to_eeprom.cpp:12:16: error: 'stringToWrite' was not declared in this scope
write_to_eeprom.cpp: In function 'void loop()':
write_to_eeprom.cpp:22:33: error: invalid conversion from 'uint8_t {aka unsigned char}' to 'char*' [-fpermissive]
write_to_eeprom.cpp: In function 'void EEPROM_write(void*, byte)':
write_to_eeprom.cpp:32:32: error: 'void*' is not a pointer-to-object type

Here is the code

#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
char[] stringToWrite = "Test";
void setup() {
  lcd.begin(16, 2);
  delay(5000);
  EEPROM_write(stringToWrite, strlen(stringToWrite));
}

void loop() {
  delay(10000);
  int addr = 0;
  byte datasize = EEPROM.read(addr++);
  char stringToRead[0x20];          // allocate enough space for the string here!
  char * readLoc = stringToRead;
  for (int i=0;i<datasize; i++) {
    readLoc = EEPROM.read(addr++);
    readLoc++;
  }
}
// Function takes a void pointer to data, and how much to write (no other way to know)
// Could also take a starting address, and return the size of the reach chunk, to be more generic
void EEPROM_write(void * data, byte datasize) {
  int addr = 0;
  EEPROM.write(addr++, datasize);
  for (int i=0; i<datasize; i++) {
    EEPROM.write(addr++, data[i]);
  }
}

Upvotes: 0

Views: 2493

Answers (1)

bobwki
bobwki

Reputation: 954

Well, you need to fix your code:

line 8 -- [] needs to go AFTER stringToWrite line 12 -- should get better after fixing line 8

line 22 -- you need to dereference readLoc. add a '*' before it.

line 32 -- your parameter "data" is a pointer to void, which has no size. Because of that, you will not be able to use it as an array. You could change the declaration to:

void EEPROM_write(char * data, byte datasize)

That fixes the compiler errors. Taking a quick look at the semantics of the code seems to be doing what you want. Good luck.

Upvotes: 0

Related Questions