only getting value 255 from EEPROM

Hi i got a problem and cant figure it out.

I got my potentiometer hooked up (outer pins to 5v and ground and the middle one to A0). What I want to do is send the data I get from the potentiometer over i2c to the slave. the slave gets the same values in as the master sends. After that I want to save the collected data from the master in EEPROM and then later read thos values out of EEPROM. The problem is when I read out the EEPROM I only get 255 as values which I not expect (even after setting all the addresses from EEPROM to 0 using a code) and i dont know why could you guys help?

Down below the codes i use for it.

Here are the codes i use.

Set everything to 0 arduino forum.pdf (172 KB)

EEPROM master side arduino forum.pdf (177 KB)

EEPROM slave side write funtion arduino forum.pdf (176 KB)

EEPROM read function arduino forum.pdf (175 KB)

Please post your code here using code tags as advised in read this before posting a programming question

EEPROM.length() ?? Not heard of that.

int val = Wire.read();
EEPROM.write(addr, val);

EEPROM.write() only writes 1 byte. An int is 2 bytes.
Do you see a problem ?

Have a look at the EEPROM.put() and EEPROM.get() functions

Sorry for that. Never seen that topic anywere thx.

to set the addreses to zero

#include <EEPROM.h> // hier word de bibliotheek ingevoegd zodat het programma de commandos kent.

void setup() {

  pinMode(13, OUTPUT); // hier geef je aan dat 13 een output is.
  
  

  for (int i = 0 ; i < EEPROM.length() ; i++) { // hier zeg je als integer i kleiner is dan EEPROM.length dan moet i 1 hoger worden.
    EEPROM.write(i, 0);
  }

  
  digitalWrite(13, HIGH); // als controle van het programma gaat de led aan als het programma klaar is met alles op 0 zetten. 
}

void loop() {

}

the reading function

#include <EEPROM.h> // hier voeg je de benodigde bibliotheek in zodat het programma de commandos herkent

// begint met het lezen van de eerste byte (adres 0) van de EEPROM
int address = 0;
int value;

void setup() {

  Serial.begin(9600); // hier opent de poort zich
  while (!Serial) { // wacht tot de poort geconnect is. alleen voor ''native'' usb poorten.
    ; 
  }
}

void loop() {
  // read a byte from the current address of the EEPROM
  value = EEPROM.read(address); // hier leest die het actuele adres van de EEPROM

  Serial.print(address);  // hier word het adres geprint
  Serial.print("\t");    // hier word een horizontale tab ingevoegd
  Serial.print(value, DEC); // hier word gezegd print de waarde in decimalen
  Serial.println();        // hier word een blank stuk ingevoegd 


  address = address + 1; // hier geeft die een voorwaarde aan, het adres is het huidige adres +1
  if (address == EEPROM.length()) { // hier word een voorwaarde aangegeven, als het adres gelijk is aan EEPROM.length dan is het adres 0. 
    address = 0;
    
  }

  delay(500);
}

the master side

#include <Wire.h> // hier is de benodigde bibliotheek er bij gevoegd

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int ledPin = 12;
int x = 0;
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  Wire.begin(); // hier word aangegeven dat dit de master is
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);


}
void loop()  
{
  x = 11;
  while (x > 1) 
  {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
     Wire.beginTransmission(9); // verstuurd data naar apparaat 9
     Wire.write(outputValue);              // stuurt data (X)
     Wire.endTransmission();    // stop met data versturen
     Serial.println(outputValue);
     delay(500); // vertraging van versturen data halve sec   
     Serial.println("\n");
     Serial.println(x);
     delay(500);
     x--;
  }   
}

the slave side

// Include the required Wire library for I2C
#include <Wire.h>
#include <Wire.h>
#include <EEPROM.h>


int addr = 0;
int x = 0;
void setup() {


  // hier word de slave aangemerkt (9)
  Wire.begin(9);
  // hier word bepaald wat er gedaan moet worden met de ontvangen info
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}
void receiveEvent(int bytes)
{
  x = Wire.read();    // hier leest die een karakter van de i2c


}
void loop() {
  Wire.read();
  // hier leest die een karakter van de i2c
  delay(2);
  Serial.println(x);


  int val = Wire.read();

  EEPROM.write(addr, val);
  addr = addr + 1;
  delay(1000);
}

UKHeliBob: int val = Wire.read(); EEPROM.write(addr, val);

EEPROM.write() only writes 1 byte. An int is 2 bytes. Do you see a problem ?

Have a look at the EEPROM.put() and EEPROM.get() functions

thx for this goign to look in to it now.

Ah, I see .length() is a new method in EEPROM.

Why the delays on the slave side? Isn't that just going to miss events?

UKHeliBob: int val = Wire.read(); EEPROM.write(addr, val);

EEPROM.write() only writes 1 byte. An int is 2 bytes. Do you see a problem ?

Have a look at the EEPROM.put() and EEPROM.get() functions

I have looked in to it and I get what you are saying, thats why I divided the integer values by 4 to get 1 byte (which is in the code ''the master side'') so EEPROM.write could send it.

MarkT: Ah, I see .length() is a new method in EEPROM.

Why the delays on the slave side? Isn't that just going to miss events?

did the delay cause otherwise the slave got the same value multiple times which i didn't want. the master is sending out eg 100, 193, 183 etc and the slave picked up 100, 100, 193, 193, 183, 183 and with the delay he just picked up 100, 193, 183. If my reasoning for the delay is not correct let me know

otherwise the slave got the same value multiple times which i didn't want

That's a bug, it should be fixed, not worked around.

delay() usually causes problems of its own, such as missing a reading you do want, or interfering with other code on the slave.

MarkT: That's a bug, it should be fixed, not worked around.

delay() usually causes problems of its own, such as missing a reading you do want, or interfering with other code on the slave.

the master sends data once every second (which is a value of the potentiometer) to the slave. Is this maybe a reason why the slave got the same data multiple times? I thought i had fixed it by putting the delay in but you say i just worked around it how do you suggest i fix it?

I just started with arduino and am new to programming thx for the help.

From the documentation, the EEPROM memory has a specified life of 100,000 write/erase cycles. It sounds like you're planning to write to the EEPROM a lot, so that's something to keep in mind.

JPlanck: From the documentation, the EEPROM memory has a specified life of 100,000 write/erase cycles. It sounds like you're planning to write to the EEPROM a lot, so that's something to keep in mind.

i knew it had 100,000 write/erase cycles haha that is my next step trying to restrict the write/erase cycles, but i want to have it working first then finetuning. What are your thoughts on that? help/tips are always welcome.

AngelinaNoLee: my next step trying to restrict the write/erase cycles

Use EEPROM.update()

kenwood120s: Use EEPROM.update()

thanks im goign to look into it. Appreciated.

Hi I found out why ik kept getting the value 255, its the standard value from the EEPROM. After i upload the read function code it overwrites the saved data on the EEPROM with 255 to all addresses. This is because the new sketch ''trows'' away the old EEPROM and overwrites it.

Hope other people with the same issue see this post and know why they have the problem.

BUT... their is a solution to it you could buy a defice to alternaty the eefuse which i dont recommend because its very hard to do and you could brick your arduino. Bricking means that your arduino cant read sketches anymore so it is pretty useless then. you could unbrick it by uploading a new bootloader that is also very hard to do. if you want to know hwo this all works just google ''how to change the eefuse of a arduino'' and for the unbricking google ''how to unbrick a arduino (arduino name)''

Thanks everyone for commenting and trying to help appreciate it.

Hi I found out why ik kept getting the value 255,

Did you also correct your erroneous attempt to write an int into a location that can only hold a byte ?

Which Arduino do you have? Standard fuse settings are to preserve the EEPROM values. Do you know how the incorrect fuse settings are on your processor?

It is possible to reburn the boot loader and change the fuse settings with another Arduino, and you will not need an icsp custom programmer.

https://www.arduino.cc/en/Tutorial/ArduinoISP

UKHeliBob: Did you also correct your erroneous attempt to write an int into a location that can only hold a byte ?

I did that, I mapped the value from the sensor only from 0 - 255 which this line of code   outputValue = map(sensorValue, 0, 1023, 0, 255); so it could be send over the EEPROM.write function. What that basicly did is it divided the values by 4 so it could be mappen in only one byte. Is this the good way or do you suggest an other way?