I used a NodeMCU V3 before but because of some limitations related to analog inputs, I had to switch to any of ESP32 series.
Now I have a ESP32-WROOM-32E.
In my context, I want to optimize memory writting by using random numbers to adress the values. It before worked like this:
\\This is a pseudo code, just to give an idea
EEPROM.begin(500);
int seed = analogRead(0);
randomSeed(seed);
for(int i=0;i<10;i++){
int value = random(0,500+1);
EEPROM.write(value,i); \\this writes the value i in a random adress
\\EEPROM.commit();
Serial.println(value);
}
randomSeed(seed);
for(int i=0;i<10;i++){
Serial.println(EEPROM.read(random(0,500+1)));
}
The output would be the series of 10 random numbers twince. (If you ignore the chances of rewritting 'i' in the same adress)
I want this same thing in my new board, but it doesn't work.
Code I used to test:
void setup(){
Serial.begin(115200);
int semente = analogRead(4);
delay(3000);
randomSeed(semente);
for(int i =0; i<10; i++){
Serial.println(random(5,15));
}
Serial.println("\n");
randomSeed(semente);
for(int i =0; i<10; i++){
Serial.println(random(5,15));
}
}
void loop(){
}
Remove the int from this line. Because of that int, you created a new local variable in setup(), which ceases to exist, and it's value lost, when setup() ends.
In loop() your code is using a global variable also called semente, which doesn't get assigned a value by any of the code you posted, so is probably zero. When that is given to seed(), random() produces a different sequence
What's the reason for doing this at random addresses? It seems like it would make more sense to start at one end and keep moving the address up by the size of the data until you get to the end and start over. That way the wear is levelled across all the cells and it's easy to figure out where the data is.
However, by doing that, the first adresses will be written way more than the furthest adresses, and you know the flash memory have a limited writting..
You can say "ok but yet still the adress where you save the random seed will be written in a high rate too" and you're right. I have ideas to fix that.