how to use basic EEPROM ?

anyone can teach me how to use eeprom basically....tnx..
newbie.
step by step...
here my sample proj.
sw1 on led1 on
sw2 on led2 on led1 off
sw3 on led3 on led2 off..then
sw1 on led1 on led3 off....

if didnt follow the sequence (sw1 - sw2 - sw3) ledx will on...and when it turn off still ledx will on...only key reset will turn off the ledx....tnx

Why do you think that you need to use EEPROM to achieve the required sequence ?

in order to inform that the operator didnt follow the sequence...only the authorize will reset

geofmen,

I am no expert on anything.
but used EEPROM for my EasyVR3 project.

just see whether you can apply my code
to get what you want. I have an led in the picture too.

extract what you need from below code - you might think this is all crap!!!!

sorry for being scanty.


#include <EEPROM.h>

/* inside loop

String newName;

String names[10] = { "name1", "name2", "name3", "name4" };

read mic value and store in newName;
int micValue = analogRead(micPin);
Serial.println(micValue, DEC);
//delay(400);

for (int i = 0; i > 10; i++) {
names = micValue;

  • newName = names[]; // store newName in names array at available index;*

  • Serial.println(newName);*

  • }*

  • delay(1000);*

  • EEPROM.write(addr, "newName[]"); // write this name in EEPROM*
    int value = EEPROM.read(address);
    Serial.println(newName);

  • Serial.println(names[0]);*

  • Serial.println(names[1]);*

  • Serial.println(names[2]);*

  • Serial.println(names[3]);*

  • int ledGreen = 7;*

  • int ledRed = 13;*

  • pinMode(ledGreen, OUTPUT);*

  • pinMode(ledRed, OUTPUT);*

  • EEPROM.write(addr, "names[1]");*

  • Serial.print("wrote this at address ");*

  • Serial.print(addr);*

  • Serial.print(" value " );*

  • Serial.println(names[1]);*

  • digitalWrite(ledRed, HIGH);*

  • delay(1000);*

  • digitalWrite(ledRed, LOW);*

  • delay(1000);*

  • addr = addr + 0; // just want to stay at the same address*

  • if (addr == EEPROM.length()) {*

  • addr = 0;*

  • }*

  • delay(10000);*

  • // value = EEPROM.read(address);*

  • val = EEPROM.read(addr);*

  • Serial.println();*

  • Serial.print("read from this address ");*

  • Serial.print(addr);*

  • Serial.print(" value " );*

  • Serial.println(names[1]);*

  • // Serial.print(val, DEC);*

digitalWrite(ledGreen, HIGH);

  • delay(1000);*
  • digitalWrite(ledGreen, LOW);*
    _ delay(1000);*/_

}

The above nonsense illustrates why people are expected to use code tags when posting code.

sorry im beginner i cant understand anything to the code...
thats why i need to learn step by step...thank you...

Have you looked at the examples from the EEPROM library ?

AI see no answer to my question in reply #1

UKHeliBob

what i mean in #1 i need the eeprom to read the last sequence..
and when the the ledx is on when it turns off the power supply and turn on the power supply again still the ledx will on...tnx..
if code is possible i need it...thank you..
im a beginner..no knowledge in programing

im a beginner..no knowledge in programing

If someone writes the program for you then you will learn nothing.

Try the examples and see if you can understand how they save an load data to/from the EEPROM. Then think about how and where you might apply these principles to you project

What will be saved ?
When will it be saved ?
What will be read read back ?
When will it be read back ?

geofmen - if code is possible i need it...thank you..
im a beginner..no knowledge in programing

If you have no knowledge of programming - I wonder why you
attempt on EEPROM which needs a bit of experience in programming and techniques.

can someone advise you on an alternate route to achieve your objective?

I am pretty sure there would be an easier way to do this and I
am surprised why you want to use EEPROM first and foremost.

well about this! I am not surprised at all!!!

Posted by jremington

  • Today at 04:41 am
    Quote

The above nonsense illustrates why people are expected to use code tags when posting code.

iknow the basic..but when using eeprom i really dont know how used some code...thats why im asking for basic eeprom program to deeply understand..tnx for ur comment and concern i appreciated it..

Is this basic enough ?

#include <EEPROM.h>

void setup()
{
  Serial.begin(115200);
  int eepromAddress = 0;
  EEPROM.write(eepromAddress, 123);
  byte theData = EEPROM.read(eepromAddress);
  Serial.println(theData);
}

void loop()
{
}

here code..when i sw on the led on and after i turn off the power supply still led on..
tnx..

#include <EEPROM.h>
int sw = 2;
int led = 5;

int ledstate = 0;

void setup()
{
  pinMode(sw, INPUT_PULLUP);
  pinMode(led, OUTPUT);


  checkledstate();
  EEPROM.write(ledstate, HIGH);
}

void loop()
{
  if (digitalRead(sw) == HIGH) {
    digitalWrite(led, HIGH);
  }
}
void checkledstate()
{
  ledstate = EEPROM.read(ledstate);
  if (digitalRead(led) == HIGH) {
    digitalWrite(led, HIGH);
  }
}
  ledstate = EEPROM.read(ledstate);

What address in the EEPROM is this reading from and why ?
Why is ledstate an int when the EEPROM.read() returns a byte ?
Have you tried printing ledstate after you read it from EEPROM ?
How is the input pin wired. Do you have a pulldown resistor in place ?

  EEPROM.write(ledstate, HIGH);

Where in the EEPROM do you think this writes to, what does it write and why ?

actually i do trial and error coz i really dont know how to use eeprom wat is the exact code to be used..
i appreciate u..tnx. i need further explanation to my code using eeprom

here my new edit of my code..

#include <EEPROM.h>
int sw = 2;
int led = 5;
int reset = 3;

int ledstate;

void setup()
{
  pinMode(sw, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  digitalWrite(led, ledstate);

  checkledstate();
  
}

void loop()
{
  if (digitalRead(sw) == HIGH) {
    digitalWrite(led, HIGH);
  }
  if (digitalRead(reset) == HIGH) {
    digitalWrite(led, LOW);
  }
  EEPROM.update(0, ledstate);
}
void checkledstate()
{
  ledstate = EEPROM.read(0);
  if (digitalRead(ledstate) == 1) {
    digitalWrite(led, HIGH);
  }
  if (digitalRead(ledstate) == 0) {
    digitalWrite(led, LOW);
  }
}

i used INPUT_PULLUP.... normally closed on my switch connected to ground...

geofmen,

for a beginner, u r smarter than most experts here ha ha.

as for one guy who called my code nonsense (I have enough tags) - his IQ may be resting on negative scale.

as EEPROM coding is coding against READ ONLY MEMORY(ROM)
which is normally a - no go zone - really,

having said that your code

checkledstate();
EEPROM.write(ledstate, HIGH);

the above code is in SETUP and executed only ONCE.

in the LOOP you don't check what's happening at the byte of the address.

I don't want to spoon feed you as that will never help you.
(just follow the code I sent you and study it).

so do a "read" at the specific byte (address) and
set the LED.

you said you do trial and error - well most of aren't smart - sometimes we
also do this.

try the above suggestion geofman. also check the values you get using serial.print and println.
(this is like debugging)

sunil

EEPROM coding is coding against READ ONLY MEMORY(ROM)
which is normally a - no go zone - really,

Why do you say that ?

EEPROM is perfect for saving program states in order to resume in the same state as when the system was powered off or reset as long as it is done right, of course.

UKHeliBob -

EEPROM – It stands for Electrically Erasable Programmable Read-Only Memory . It is similar to EPROM, except that in this, the EEPROM is returned to its initial state by application of an electrical signal, in place of ultraviolet light . Thus, it provides the ease of erasing, as this can be done, even if the memory is positioned in the computer. It erases or writes one byte of data at a time .
Uses – It is used for storing the computer system BIOS.

this is why tend not to go there. if anything goes wrong the BIOS might be affected(?) I am no expert to conform this though. well why go there at all when you can achieve your objectives using the memory and loading your program/sketch at run time. I have seen guys messing up with registers and finally messing up the operating system. anyway I respect your view Bob.