Internal mega EEPROM

I want to know how to get the previous state after reset arduino. Here I want to use arduino mega EEPROM.
I wrote the example code like below. Please consider this as an example.

//proximity sensors
#define s1 2
#define s2 3
#define s3 4
#define s4 5
#define s5 6

int array[]={s1, s2, s3, s4,s5};

#define L1 7 //led outpts
#define L2 8
#define L3 9
#define L4 10
#define L5 11
#define L6 12

void setup(){
Serial.begin(9600);
pinMode(s1, INUPUT);
pinMode(s2, INUPUT);
pinMode(s3, INUPUT);
pinMode(s4, INUPUT);
pinMode(s5, INPUT);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
pinMode(L5, OUTPUT);
pinMode(L6,OUTPUT);

digitalWrite(L1, HIGH);
array[j]=EEPROM.read(0);
EEPROM.update(0,0);
}

void loop(){
Serial.println("Sensor values printing");
for(int j=0; j<5; j++){
EEPROM.update(0, array[j];
Serial.print(array[j];
}

if (S1==HIGH){
digitalWrite(L2, HIGH);
}else{
digitalWrite(L2, LOW);
}

if (S2==HIGH){
digitalWrite(L3, HIGH);
}else{
digitalWrite(L3, LOW);
}

if (S3==HIGH){
digitalWrite(L4, HIGH);
}else{
digitalWrite(L4, LOW);
}

if (S4==HIGH){
digitalWrite(L5, HIGH);
}else{
digitalWrite(L5, LOW);
}

if (S5==HIGH){
digitalWrite(L6, HIGH);
}else{
digitalWrite(L6, LOW);
}
}

I want to read and write the array values to EEPROM and according to that on or off the leds.
Above is my method to approach that.
But I didnt get any result through that approach.
Kindly appreciate your help for this.
Thanks in advance.

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

int array[]={s1, s2, s3, s4,s5};

So, array is an int

An int takes 2 bytes of storage

array[j]=EEPROM.read(0);

The read function reads a single byte

Can you see a problem (apart from the fact that j is undefined, that is) ?

UKHeliBob:
Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

ok ...i told already this is an example so thats why i posted like that. :slight_smile:

//proximity sensors
#define s1 2
#define s2 3
#define s3 4
#define s4 5
#define s5 6

int array[]={s1, s2, s3, s4,s5};

#define L1 7  //led outpts
#define L2 8
#define L3 9
#define L4 10
#define L5 11
#define L6 12

void setup(){
Serial.begin(9600);
pinMode(s1, INUPUT);
pinMode(s2, INUPUT);
pinMode(s3, INUPUT);
pinMode(s4, INUPUT);
pinMode(s5, INPUT);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
pinMode(L5, OUTPUT);
pinMode(L6,OUTPUT);

digitalWrite(L1, HIGH);
array[j]=EEPROM.read(0);
EEPROM.update(0,0);
}

void loop(){
Serial.println("Sensor values printing");
for(int j=0; j<5; j++){
EEPROM.update(0, array[j];
Serial.print(array[j];
}

if (S1==HIGH){
digitalWrite(L2, HIGH);
}else{
digitalWrite(L2, LOW);
}

if (S2==HIGH){
digitalWrite(L3, HIGH);
}else{
digitalWrite(L3, LOW);
}

if (S3==HIGH){
digitalWrite(L4, HIGH);
}else{
digitalWrite(L4, LOW);
}

if (S4==HIGH){
digitalWrite(L5, HIGH);
}else{
digitalWrite(L5, LOW);
}

if (S5==HIGH){
digitalWrite(L6, HIGH);
}else{
digitalWrite(L6, LOW);
}
}

UKHeliBob:

int array[]={s1, s2, s3, s4,s5};

So, array is an int

An int takes 2 bytes of storage

array[j]=EEPROM.read(0);

The read function reads a single byte

Can you see a problem (apart from the fact that j is undefined, that is) ?

see this example i followed this youtube video ...he also used integer data type not a byte

I want to know the correct method to do this
:slight_smile:

The easiest way to write arbitrary types to EEPROM is EEPROM.put(); to retrieve them, use EEPROM.get().

for(int j=0; j<5; j++){

EEPROM.update(0, array[j];
Serial.print(array[j];
}

This does not make sense, EEPROM cell 0 will always contain array[4]. And if all array elements contain different values, you will wear out your cell 0 in no time. If you want to update EEPROM with an array, the below should do (not tested)

EEPROM.put(0, array);

This will write 5 array elements to EEPROM cells 0, 1, 2, .. 4

There seems a lot more wrong with your code. I suggest that you compile your code and fix the errors before posting; this possibly will also make it easier for us to understand what you're trying to achieve.

Take note of what is said at about 4:28 in that video. He says one thing and does another and does not follow his own advice regarding the size of an int and what the write() function does

Take a look at EEPROM.put() and EEPROM.get() These functions can save and retrieve variables of any type

pinMode(s3, INUPUT); oops

UKHeliBob:
Take note of what is said at about 4:28 in that video. He says one thing and does another and does not follow his own advice regarding the size of an int and what the write() function does

Take a look at EEPROM.put() and EEPROM.get() These functions can save and retrieve variables of any type

Thanks

sterretje:
The easiest way to write arbitrary types to EEPROM is EEPROM.put(); to retrieve them, use EEPROM.get().
This does not make sense, EEPROM cell 0 will always contain array[4]. And if all array elements contain different values, you will wear out your cell 0 in no time. If you want to update EEPROM with an array, the below should do (not tested)

EEPROM.put(0, array);

This will write 5 array elements to EEPROM cells 0, 1, 2, .. 4

There seems a lot more wrong with your code. I suggest that you compile your code and fix the errors before posting; this possibly will also make it easier for us to understand what you're trying to achieve.

Thanks

TheMemberFormerlyKnownAsAWOL:
pinMode(s3, INUPUT); oops

why ?

Raweesha:
why ?

What did the compiler say?

TheMemberFormerlyKnownAsAWOL:
What did the compiler say?

:slight_smile: in void loop it should be simple but here i want the answer to my main question
anyway pointout the faults in program is good

if (S5==HIGH){
digitalWrite(L6, HIGH);
}else{
digitalWrite(L6, LOW);
}

aka
digitalWrite (L6, S5);etc :wink: