Erasing external eeprom chip

Hello i have a 512 eeprom chip it's a 24LC512k bite eeprom chip This is like the one i got same one and This is the datasheet on it. also i got a sketch example i found online

#include <Wire.h>
#define eeprom1 0x50    //Address of 24LC256 eeprom chip
#define WRITE_CNT 1

unsigned char rdata[32];

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();  
 
  unsigned int i;
  // define large string of data to be written
  char str_data[]={"hi\n hi all\n hi all  "};
  

  // Work out length of data
  char str_len=0;  
  do{ str_len++; } while(str_data[str_len]);  
  
  // Write out data several times consecutively starting at address 0
  for(i=0;i<WRITE_CNT;i++) writeEEPROM(eeprom1,i*str_len,str_data);

  // read back the data 28 bytes at a time
  // reading data doesn't suffer from the page boundary rules
//  Serial.println("DATA READ");
  for(i=0;i<10;i++) {
    readEEPROM(eeprom1, (i*28), rdata, 28);
    Serial.write(rdata,28);
  }  

}
 
void loop(){
}
 
void writeEEPROM(int deviceaddress, unsigned int eeaddress, char* data) 
{
  // Uses Page Write for 24LC256
  // Allows for 64 byte page boundary
  // Splits string into max 16 byte writes
  unsigned char i=0, counter=0;
  unsigned int  address;
  unsigned int  page_space;
  unsigned int  page=0;
  unsigned int  num_writes;
  unsigned int  data_len=0;
  unsigned char first_write_size;
  unsigned char last_write_size;  
  unsigned char write_size;  
  
  // Calculate length of data
  do{ data_len++; } while(data[data_len]);   
   
  // Calculate space available in first page
  page_space = int(((eeaddress/64) + 1)*64)-eeaddress;

  // Calculate first write size
  if (page_space>16){
     first_write_size=page_space-((page_space/16)*16);
     if (first_write_size==0) first_write_size=16;
  }   
  else 
     first_write_size=page_space; 
    
  // calculate size of last write  
  if (data_len>first_write_size) 
     last_write_size = (data_len-first_write_size)%16;   
  
  // Calculate how many writes we need
  if (data_len>first_write_size)
     num_writes = ((data_len-first_write_size)/16)+2;
  else
     num_writes = 1;  
     
  i=0;   
  address=eeaddress;
  for(page=0;page<num_writes;page++) 
  {
     if(page==0) write_size=first_write_size;
     else if(page==(num_writes-1)) write_size=last_write_size;
     else write_size=16;
  
     Wire.beginTransmission(deviceaddress);
     Wire.write((int)((address) >> 8));   // MSB
     Wire.write((int)((address) & 0xFF)); // LSB
     counter=0;
     do{ 
        Wire.write((byte) data[i]);
        i++;
        counter++;
     } while((data[i]) && (counter<write_size));  
     Wire.endTransmission();
     address+=write_size;   // Increment address for next write
     
     delay(6);  // needs 5ms for page write
  }
}
 
void readEEPROM(int deviceaddress, unsigned int eeaddress,  
                 unsigned char* data, unsigned int num_chars) 
{
  unsigned char i=0;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));   // MSB
  Wire.write((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
 
  Wire.requestFrom(deviceaddress,num_chars);
 
  while(Wire.available()) data[i++] = Wire.read();

}

I was able to write to the eeprom chip with characters char str_data[]={"hi\n hi all\n hi all "}; on line 14 in there is placed a \n it goes to new line. The problem I'm having i wrote a lot of stuff to it and i didn't know how to get rid of the text so i wrote over top of it with space bar same amount as the text characters and added some smaller text for text.

The problem I'm having is that what i over write the old stuff in serial monitor the new text is to the left but there is a a lot space to the right so the bottom bar moves to the right here is a screenshot. i attached to this post. I'm not sure how to get rid of all that space now or how to erase everything i looked at the datasheet and i can't find nothing on clearing out the eeprom. Can someone please help me?

screenshot.png

Writing code to block fill the EEPROM is the easiest code you could possibly write for EEPROM. If you can't do that, what is the point of even playing with it?

i did this

  char str_data[]={"                        \n                             \n                           "};

and when i did that it rewrote to the block and cleared out the text but it left a lot of space
to the right. not sure how to get rid of all that space now there.

If you're writing null-terminated strings to the EEPROM, there is no need to erase anything. Just overwrite whatever is there. What is the purpose of your EEPROM storage? It looks like you want to store strings, so you shouldn't have any problem.

I'm not really storing anything.. Just learning to write to it and clear it out. That is what it is about for me I'm learning.

Use the A- button, gets rid of the formatting that adds the color tags and stuff when copy/paste is used.

Funny thing is i didn't add that to there and first time i just notice that i will change it thank you.

First of all sry for reviving this topic, but i was led here by google with the exact same problem trying to correct it the same way...

  1. the example you "started" from had a write count of 5
    #define WRITE_CNT 1 <- was changed

  2. because of that:

// Serial.println("DATA READ");
for(i=0;i<10;i++) {
readEEPROM(eeprom1, (i*28), rdata, 28);
Serial.write(rdata,28);
}

Spews out 5 pages

  1. looks like the page size on your chip is 128 bytes, the example assumes 64 bytes

// Calculate space available in first page
page_space = int(((eeaddress/64) + 1)*64)-eeaddress;

How would one go about getting the read function to stop at a null?