Hi! I'm trying to generate four digit random number and I want them so store in EEPROM so I can use it as
a password for my project , but every time I run and get the value from the address, they do not match each other. Here's the code.
#include<EEPROM.h>
int x=0;
void setup() {
Serial.begin(9600);
x = random(1000,5555);
Serial.println(x);
EEPROM.put(3,x);
Serial.println("FROM EEPROM");
Serial.println(EEPROM.read(3));
}
void loop() {
}
I'm still new to arduino so any form of help is greatly appreciated.
Thank you.
I think that you need to use EEPROM.get() to retrieve the int data. EEPROM.read(3) reads only one byte from address 3.
#include<EEPROM.h>
int x=0;
void setup() {
Serial.begin(9600);
x = random(1000,5555);
Serial.println(x);
EEPROM.put(3,x);
int data;
Serial.println("FROM EEPROM");
Serial.println(EEPROM.get(3, data));
}
void loop() {
}
I test this and get the same number every time. That is because the random function is pseudo random. Add randomSeed(analogRead(A0)); in setup, before you call random, to make the randomness start at a different number each time the Arduino is reset.
I think that you need to use EEPROM.get()
THANK YOUUU!! It did print the same data,
At first I thought EEPROM.get would just retrieve but not write data that's why I didn't use it , sorry.