SD card data need to store in internal EEPROM

Hello,how can store the sd card data in EEPROM based on event./
I made some code(with the help of forum) it will display the sd card data in lcd when i press the button.Here is the code

#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
char Str1[15];
void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      
      lcd.print(c);
      
      Serial.print(c);
      
      
      }
   dataFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

Now i want to stored the data in EEPROM which was displayed in serial monitor as well as lcd.(last data stored in sd card).
In each button press data will update on same EEPROM ADDRESS.

Please advice me what are the modification in my program i have to be done for adopting these changes.

thank you.

Have you looked at the EEPROM examples in the IDE ?

UKHeliBob:
Have you looked at the EEPROM examples in the IDE ?

After reffering Read/Write EEPROM ,I have made some changes. Here is my code:-

#include <EEPROM.h>
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
//char Str1[15];
int baseAddr = 0;

void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      EEPROM.update(baseAddr,c);
      Serial.print(c);
      delay(500);
      c = EEPROM.read(baseAddr);
      Serial.print("eeprom value is");
      Serial.print(c);
      lcd.print(c); 
      
      }
  
   dataFile.close();
  }
  
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

Serialmonitor out put is here:-

Initializing SD card...Card initialized.
Data LOG
606
datalog.txt
6eeprom value is60eeprom value is06eeprom value is6
eeprom value is
eeprom value is
File exists.
Successfully removed file.

EEPROM value shown one by one i want result like this "eeprom value is606".

Why did this display like this,how i can solve this issue.

LCD display also show the unspecified character after the value(image attached)

@OP

You are currently here:

1. You press Button-1 (with DPin-7), data from A0-pin is acquired, and the data is stored in SD Card. The same chunk of data is also shown onto Serial Monitor.

2. Now, you want that the data of Step-1 should also be stored in the internal EEPROM (starting at location 0x0010) of Atmega328P MCU of the UNO Board. Hints:
(1) include EEPROM.h library at suitable place of your sketch.
(2) include this code: EEPROM.put(0x0010, dataString);. //writing data into EEPROM
(3) read back data from EEPROM and show on Serial Monitor: EEPROM.get(arg1, arg2); Serial.println(arg);.

Hope, you can proceed!

When you write to and read from the EEPROM shouldn't you be updating baseAddr ? Otherwise the byte will be written to and read from the same EEPROM location each time and each location can only hold one byte

GolamMostafa:
@OP

You are currently here:

1. You press Button-1 (with DPin-7), data from A0-pin is acquired, and the data is stored in SD Card. The same chunk of data is also shown onto Serial Monitor.

2. Now, you want that the data of Step-1 should also be stored in the internal EEPROM (starting at location 0x0010) of Atmega328P MCU of the UNO Board. Hints:
(1) include EEPROM.h library at suitable place of your sketch.
(2) include this code: EEPROM.put(0x0010, dataString);. //writing data into EEPROM
(3) read back data from EEPROM and show on Serial Monitor: EEPROM.get(arg1, arg2); Serial.println(arg);.

Hope, you can proceed!

1==statement is correct.
2==when i press DPin2 data will read from sd card and print to serial monitor as well as write to EEPROM address.
1==EEPROM.h library inclueded.
2== i want to store in eeprom only data from sd card.
3=="EEPROM.get(arg1, arg2); Serial.println(arg);" please give more details sir, i cant get what you intend

UKHeliBob:
When you write to and read from the EEPROM shouldn't you be updating baseAddr ? Otherwise the byte will be written to and read from the same EEPROM location each time and each location can only hold one byte

i
for example value is 606.and eeprom addres=0
i think writting in eeprom like this

first write 6 in addres 0
secnd write 0 in addres 0
third write 6 in address 0

is it correct? so iwant to write each value in different location and print in single line.

i have try to learn the program,please give the more clue or example code so i can easily understood.

first write 6 in addres 0
secnd write 0 in addres 0
third write 6 in address 0

is it correct? so iwant to write each value in different location and print in single line.

You have described exactly what you are doing wrong.
Increment the address each time you write a character to the EEPROM

6 to address 0
0 to address 1
6 to address 2

But how will you know how many characters there are when you come to read them back ?
Will there always be 3 ?

UKHeliBob:
You have described exactly what you are doing wrong.
Increment the address each time you write a character to the EEPROM

6 to address 0
0 to address 1
6 to address 2

But how will you know how many characters there are when you come to read them back ?
Will there always be 3 ?

add for() loop for address increment here is the code:-

#include <EEPROM.h>
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
//char Str1[15];
int baseAddr = 0;

void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");

    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
      char c = dataFile.read();
      for (int i=0;i<=4;){
      EEPROM.write(baseAddr+i,c);
      delay(500);
      Serial.print(c);
      i++;
      }
      c = EEPROM.read(baseAddr);
      Serial.print("eeprom value is");
      Serial.print(c);
      lcd.print(c); 
      
      }
  
   dataFile.close();
  }
  
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

serial o/p is here

Initializing SD card...Card initialized.
Data LOG
182
datalog.txt
1eeprom value is18eeprom value is82eeprom value is2
eeprom value is
eeprom value is
File exists.
Successfully removed file.

out put is erratic,how can make in to a single line out put..

      char c = dataFile.read();
      for (int i = 0; i <= 4;)
      {
        EEPROM.write(baseAddr + i, c);
        delay(500);
        Serial.print(c);
        i++;
      }

You only ever read once from the SD card so c never changes but you save it 5 times (why 5 ?) Check how a for loop works
Then you read a single value from EEPROM which will be the last one saved, ie the 5th character read from the SD

If you are only expecting 3 characters then only read 3 not 5
Read each character inside the for loop then save it to EEPROM
To read the characters from EEPROM you need another for loop
Actually that last statement is not true but I dare not introduce other ways of doing it until you have this one working.

UKHeliBob:

      char c = dataFile.read();

for (int i = 0; i <= 4;)
     {
       EEPROM.write(baseAddr + i, c);
       delay(500);
       Serial.print(c);
       i++;
     }



You only ever read once from the SD card so c never changes but you save it 5 times (why 5 ?) Check how a for loop works
Then you read a single value from EEPROM which will be the last one saved, ie the 5th character read from the SD

If you are only expecting 3 characters then only read 3 not 5
Read each character inside the for loop then save it to EEPROM
To read the characters from EEPROM you need another for loop
Actually that last statement is not true but I dare not introduce other ways of doing it until you have this one working.

character size will vary 0 to 4/-

/*Connections
    SD card Module - Arduino UNO
              MOSI - Pin 11
              MISO - Pin 12
              CLK  - Pin 13
              CS   - Pin 10
   Button to Pin 7 and GND with internal Pull-up.
   Three 10KΩ Potentiometers to Analog Pins A0, A1 and A2.
*/
#include <EEPROM.h>
#include <Wire.h> 
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int chipSelect = 10;
const int butPin = 7;
bool butState;
const int butlcd = 2;
bool butState1;
int analogPin=A0;
char Str1[5];
int baseAddr = 0;

void setup() 
{
  lcd.begin(16,2);
  lcd.backlight();
  pinMode(butPin,INPUT_PULLUP);
  pinMode(butlcd,INPUT_PULLUP);
  pinMode(analogPin,INPUT);
  Serial.begin(9600);
  while (!Serial)
  {
    ;
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed, or not present");
    while (1);
  }
  Serial.println("Card initialized.");
}

void loop() 
{
  butState = digitalRead(butPin);
  delay(1000);
  if (butState==1)
  {
   //Default state. Do Nothing. 
  }
  else
  {
    Serial.println("Data LOG");
    String dataString = "";
    
      int sensor = analogRead(analogPin);
      dataString += String(sensor);
      

   File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile)
   {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
   }
   else 
   {
    Serial.println("error opening datalog.txt");
   }}
   butState1 = digitalRead(butlcd);
 delay(500);
  if (butState1==0)
  {
  readu();
  }}
 
 void readu()
  {
 
 File dataFile  = SD.open("datalog.txt");
  if (dataFile) {
    Serial.println("datalog.txt");
//delay(1000);
    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
       char c = dataFile.read();
      
      for (int i=0;i<=3;){
      EEPROM.write(baseAddr+i,c);
      delay(500);
      Serial.print(c);
      i++;
     
      }
    
      c = EEPROM.read(baseAddr);
      Serial.print("eeprom value is");
      Serial.print(c);
      lcd.print(c);
      
      }
  
   dataFile.close();
  }
  
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
 delay(1000);
 if (SD.exists("datalog.txt")) { // if "file.txt" exists, fill will be deleted
    Serial.println("File exists.");
    if (SD.remove("datalog.txt") == true) {
      Serial.println("Successfully removed file.");
    } else {
      Serial.println("Could not removed file.");
    }
  }
  }

serial monitor shows

Initializing SD card...Card initialized.
Data LOG
183
datalog.txt
1111eeprom value is18888eeprom value is83333eeprom value is3



eeprom value is



eeprom value is
File exists.
Successfully removed file.

i think this read 4 time that means 1111 in differnt address.totaly confused..please make the correction

i think this read 4 time that means 1111 in differnt address.totaly confused..please make the correction

First put right your very basic mistake

      char c = dataFile.read();
      for (int i = 0; i <= 3;)
      {
        EEPROM.write(baseAddr + i, c);
        delay(500);
        Serial.print(c);
        i++;
      }

How many times do you read a character from the file ?

@OP

OK! It looks like that you need to gather some basic understandings on the steps to be followed to perform data read/write operation on internal EEPROM of ATmega328P MCU of the Arduino UNO Board. We are doing so having seen your interaction with both with @UKHeliBob and @GolamMostafa although you could not exactly follow their directives.

1. EEPROM is a block of non-volatile memory space in the ATmega328P MCU. There are in total 1024 memory locations in this space. Every memory location has an address, and each memory location can hold 8-bit data (1-byte). The address of the 1st location is: 0 (0x0000), the address of the next location is 1 (0x0001 = 0x0000 + 0x00010,..., and the address of the last location is 1023 (0x03FF). The conceptual view of the EEPROM space is given below in Fig-1.
eeprom-1.png
Figure-1: Conceptual view of the EEPROM memory space of ATmega328P MCU

2. Data write operation with the EEPROM occurs 1-byte at a time, and the write operation takes about 5 ms time. The instruction to write a data byte into a memory location is:

EEPROM.write(arg1, arg2);

where:
arg1 (argument1) is the address of the target memory location into which we wish to write a data byte.
arg2 (argument2) is the 8-bit data that we wish to be written into the target memory location.

Assume that we wish to write 0x23 (0x indicates that the number that follows this preamble is in hexadecimal form) into memory location 0x0010. The instruction is:

EEPROM.write(0x0010, 0x23);

The above instruction will take about 5 ms to complete, and then the next instruction will be completed. Hence, it is a 'blocking code'.

3. Data read operation also takes place 1-byte at a time. To verify that the 'write operation' of Step-2 has taken place correctly, let us read back the data from location 0x0010 and display on Serial Monitor. The instruction is:

byte x = EEPROM.read(0x0010); //the data that has come from location 0x0010 will be saved in x.

4. Upload the following sketch and observe the result.

#include<EEPROM.h>

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0x0010, 0x23);
  byte x = EEPROM.read(0x0010);
  Serial.print(x, HEX); //shows: 23
}

void loop()
{

}

5. If you agree that the above steps are helpful, then I would like to tell you to do the following exercise and post the sketch.

Write a program to store the contents of the following array into EEPROM starting at location 0x0020. Use for() loop and sizeof operator in your sketch to reduce the number of code lines.

byte myData [] = {0x31, 0x32, 0x33, 0x34, 0x35};

eeprom-1.png

GolamMostafa:
@OP

5. If you agree that the above steps are helpful, then I would like to tell you to do the following exercise and post the sketch.

[/quote

very much helpfull.

i have trying to make the program using for() but not get success,but i made the program without loop
program is here

#include<EEPROM.h>

byte myData [] = {0x31, 0x32, 0x33, 0x34, 0x35};
int addr = 0x0020;
void setup()
{
  Serial.begin(9600);
  EEPROM.write(0x0020, 0x31);
  byte x = EEPROM.read(0x0020);
  Serial.println(x,HEX);
EEPROM.write(0x0021, 0x32);
  byte y = EEPROM.read(0x0021);
  Serial.println(y,HEX);
EEPROM.write(0x0022, 0x33);
  byte z = EEPROM.read(0x0022);
  Serial.println(z,HEX);
EEPROM.write(0x0023, 0x34);
  byte a = EEPROM.read(0x0023);
  Serial.println(a,HEX);
  EEPROM.write(0x0024, 0x35);
  byte b = EEPROM.read(0x0024);
  Serial.println(b,HEX);

}

void loop()
{

/*for (int i=0;i<4;){
addr=i+1;
EEPROM.write(addr, myData);
  byte x = EEPROM.read(addr);
  Serial.print(x,HEX); //shows: 23

}*/}




while using for() little bit confusing.

serial o/p



31
32
33
34
35




while using for() little bit confusing sir you can see in the program what i made with for();

@OP

1. Good job; I would like to offer you K++.

2. To write 5 data bytes of the array, you have executed this instruction: EEPROM.write(locationAddress, dataByte); for 5 times. We can reduce the number of code line using for() loop and sizeof operator this way:

#include<EEPROM.h>
byte myData[] = {0x31, 0x32, 0x33, 0x34, 0x35};
unsigned locationAddress = 0x0020;
void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < sizeof(myData); i++) //sizeof(myData) = 5 = number of bytes in the array
  {
    EEPROM.write(locationAddress, myData[i]);
    locationAddress++;            //next location
  }
  locationAddress = 0x0020;
  for (int i = 0; i < 5; i++)
  {
    byte x = EEPROM.read(locationAddress);
    Serial.println(x, HEX);
    locationAddress++;
  }
}

void loop()
{

}

Hope the codes are self explanatory.

3. You can reduce the number of code lines in the sketch of Step-2 by performing read operation just after write operation:

#include<EEPROM.h>
byte myData[] = {0x31, 0x32, 0x33, 0x34, 0x35};
unsigned locationAddress = 0x0020;
void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < sizeof(myData); i++) //sizeof(myData) = 5 = number of bytes in the array
  {
    EEPROM.write(locationAddress, myData[i]);  //write this byte and the next byte until done
    byte x = EEPROM.read(locationAddress);      //read this byte and next byte until done
    Serial.println(x, HEX);
    locationAddress++;      //next location
  }
}

void loop()
{

}

Hope the codes are self explanatory.

4. Do the following exercise:
Connect a Button (K1) at DPin-7 with internal pull-up resistor enabled; short 3.3V point of your UNO/NANO with A0-pin. Write program so that the value of A0 (in hex form) is acquired and saved into EEPROM when K1 is pressed. (The hex value of the signal of A0-pin is about : (1023/5)*3.3 = 02A3+/-2).

4th point not clear

sreekanthmp:
4th point not clear

If you are not English native, then translate it into your mother tongue; the meaning would be very clear.