I am trying to save long numbers to eeprom when I push a button, but when I display the number, it just shows a number around 29149, every time. I have tried using different boards, nano and uno, and have the same result. Here is my code, any suggestions?
#include <EEPROM.h>
int button1 = 8;
int a1 = EEPROM.read(18);
int b1 = EEPROM.read(17);
int c1 = EEPROM.read(16);
int d1 = EEPROM.read(15);
int e1 = EEPROM.read(14);
int f1 = EEPROM.read(13);
int g1 = EEPROM.read(12);
int h1 = EEPROM.read(11);
int i1 = EEPROM.read(10);
int j1 = EEPROM.read(9);
int k1 = EEPROM.read(8);
int l1 = EEPROM.read(7);
int m1 = EEPROM.read(6);
int o1 = EEPROM.read(5);
int p1 = EEPROM.read(4);
int q1 = EEPROM.read(3);
int r1 = EEPROM.read(2);
int s1 = EEPROM.read(1);
int t1 = EEPROM.read(0);
int y = ((100000000000000000+s1)+(10000000000000000+r1)+(1000000000000000+q1)
+(100000000000000+p1)+(10000000000000+o1)+(1000000000000+m1)+(100000000000+l1)+(10000000000+k1)
+(1000000000+j1)+(100000000+i1)+(10000000+h1)+(1000000+g1)+(100000+f1)+(10000+e1)+(1000+d1)+(100+c1)+(10+b1)+a1);
int x = y;
void setup(){
Serial.begin(9600);
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println();
Serial.println("Solving...");
pinMode(button1, INPUT_PULLUP);
}
void loop(){
if(x % 2 == 0){
x = x/2;
}
else{
x=3*x;
x++;
}
if(x<y){
Serial.println(y);
y++;
x=y;
}
if(digitalRead(button1) == false){
unsigned n = y;
Serial.println("Saving...");
EEPROM.write(18, (n / 1U) % 10);
EEPROM.write(17, (n / 10U) % 10);
EEPROM.write(16, (n / 100U) % 10);
EEPROM.write(15, (n / 1000U) % 10);
EEPROM.write(14, (n / 10000U) % 10);
EEPROM.write(13, (n / 100000U) % 10);
EEPROM.write(12, (n / 1000000U) % 10);
EEPROM.write(11, (n / 10000000U) % 10);
EEPROM.write(10, (n / 100000000U) % 10);
EEPROM.write(9, (n / 1000000000U) % 10);
EEPROM.write(8, (n / 10000000000U) % 10);
EEPROM.write(7, (n / 100000000000U) % 10);
EEPROM.write(6, (n / 1000000000000U) % 10);
EEPROM.write(5, (n / 10000000000000U) % 10);
EEPROM.write(4, (n / 100000000000000U) % 10);
EEPROM.write(3, (n / 1000000000000000U) % 10);
EEPROM.write(2, (n / 10000000000000000U) % 10);
EEPROM.write(2, (n / 100000000000000000U) % 10);
EEPROM.write(0, (n / 1000000000000000000U) % 10);
delay(2000);
Serial.print("Saved at: ");
Serial.println(y);
delay(2000);
}
}
use put() and get() and you can deal with your long directly
Do you expect this to be considered as a binary number?
it's not. and it's way too big for an int
1. What is the size of a long-type number in hex base and decimal base?
2. What is the size of a byte-type number in hex base and decimal base?
3. How many bytes of data are written into EEPROM when the following code is executed:
EEPROM.write(ar1, arg2);
Regardless of how you are reading the EEPROM, your method of reconstructing a long from individual bytes is flawed. Usually that would be done with bit shifting, or reading the individual bytes into a byte array, then memcpy into a long. (Possible to use a union, but that may be dependent on the compiler). Your method of saving the number to eeprom is also fairly unusual.
put and get are the easiest method.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.