Storing bytes, int, float, String values ​​on Arduino without losing data when power supply is stopped with EEPROM memory

posted in: Microcontroller | 0

EEPROM is a non-volatile memory chip commonly used in computers and mobile devices to store a small amount of data that needs to be modified. EEPROM belongs to the category of “memory that does not lose data when power is stopped”. ماهي لعبة الروليت

EEPROM on Arduino has a capacity of 1024 bytes, each byte will correspond to an address. العب بوكر  Supports erase/write up to 100,000 times, which means that the EEPROM memory will be damaged beyond this number of times.

The delay between reads/writes should be 10ms. لعبة بلاك جاك اون لاين

1. Store bytes

#include <EEPROM.h>

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);

  EEPROM.write(address,10);
  delay(10);
  value = EEPROM.read(address);
  Serial.println(value);
}

void loop() {
  // read a byte from the current address of the EEPROM
  
}

Write command

EEPROM.write(Address,Value);

Read command

EEPROM.read(Address);

2. Store Int

The Int type is 2 bytes in size, so it must be written to two consecutive registers.

#include <EEPROM.h>
void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  int number = 32000;
  int adress=0; 
  //Viết giá trị vào EEPROM
 
  EEPROM.write(adress, number/256);
  delay(10);
  EEPROM.write(adress+1, number%256);
  delay(10);
  
  int readint;
  readint = int(EEPROM.read(adress))*256;
  delay(10);
  readint |= int(EEPROM.read(adress+1));
    Serial.print("Giá trị viết vào : ");
    Serial.println(readint);
}

void loop() {
  // read a byte from the current address of the EEPROM
  
}

Write command

EEPROM.write(adress, number/256);
delay(10);
EEPROM.write(adress+1, number%256);
delay(10);

Read command

int readint;
readint = int(EEPROM.read(adress))*256;
delay(10);
readint |= int(EEPROM.read(adress+1));

3. Store float

The float type is 4 bytes in size, so it must be written to 4 consecutive registers.

#include <EEPROM.h>


void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  float number = 234.678;
  int adress=0; 
  //Viết giá trị vào EEPROM
  union {
    float fval;
    byte bval[4];
  } floatAsBytes;

  floatAsBytes.fval = number;
  EEPROM.write(adress, floatAsBytes.bval[0]);
  EEPROM.write(adress+1, floatAsBytes.bval[1]);
  EEPROM.write(adress+2, floatAsBytes.bval[2]);
  EEPROM.write(adress+3, floatAsBytes.bval[3]);
  
  float readfloat;
  union u_tag {
    byte b[4];
    float fval;
  } u;
  u.b[0] = EEPROM.read(adress);
  u.b[1] = EEPROM.read(adress+1);
  u.b[2] = EEPROM.read(adress+2);
  u.b[3] = EEPROM.read(adress+3);

  readfloat = u.fval;
    Serial.print("Giá trị viết vào : ");
    Serial.print(readfloat,4);
}

void loop() {
  // read a byte from the current address of the EEPROM
  
}

Write command

union {
    float fval;
    byte bval[4];
  } floatAsBytes;

  floatAsBytes.fval = number;
  EEPROM.write(adress, floatAsBytes.bval[0]);
  EEPROM.write(adress+1, floatAsBytes.bval[1]);
  EEPROM.write(adress+2, floatAsBytes.bval[2]);
  EEPROM.write(adress+3, floatAsBytes.bval[3]);

Read command

float readfloat;
  union u_tag {
    byte b[4];
    float fval;
  } u;
  u.b[0] = EEPROM.read(adress);
  u.b[1] = EEPROM.read(adress+1);
  u.b[2] = EEPROM.read(adress+2);
  u.b[3] = EEPROM.read(adress+3);

  readfloat = u.fval;

4. Store String

First to save a String value, you need to specify its maximum length here, I set it to 32.

#include <EEPROM.h>
void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  String str = "SinhVienDien";
  int adress =0;
  //Viết giá trị str vào EEPROM
 
  for (int i = adress; i < adress+str.length(); ++i)
  {
      EEPROM.write(i, str[i]);
      delay(10);
  }
   for (int i = adress+str.length(); i < adress+32; ++i)
  {
      EEPROM.write(i, 0);
      delay(10);
  }
  
  String readstr="";
    for (int i = adress; i < adress+32; ++i)
    {
        readstr += char(EEPROM.read(i));
        delay(10);
    }
    Serial.print("Giá trị viết vào : ");
    
    readstr.trim();
    Serial.println(readstr);
}

void loop() {
  // read a byte from the current address of the EEPROM
  
}

Write command

String str = "SinhVienDien";
  int adress =0;
  //Viết giá trị str vào EEPROM
 
  for (int i = adress; i < adress+str.length(); ++i)
  {
      EEPROM.write(i, str[i]);
      delay(10);
  }
   for (int i = adress+str.length(); i < adress+32; ++i)
  {
      EEPROM.write(i, 0);
      delay(10);
  }

Read command

 String readstr="";
    for (int i = adress; i < adress+32; ++i)
    {
        readstr += char(EEPROM.read(i));
        delay(10);
    }
    
    readstr.trim();

Leave a Reply

Your email address will not be published.