How to touch up data from the EEPROM

How to touch up data from the EEPROM

EEPROM is a memory whose values are kept when the board is turned off. In this example it is showed how to write and read from the EEPROM. In our products, Ardbox  has 1KB and M-Duino  has 4KB of EEPROM

This example we will see how to use functions EEPROM.write(), EEPROM.read(),EEPROM.put() and EEPROM.get().

The first example code is implemented how to store a byte from analog input 0 into the EEPROM using the EEPROM.write(). EEPROM.write() store a byte to the EEPROM:

#include <EEPROM.h>

int addr = 0; // current adress in the EEPROM

void setup() {
  /** Empty setup. **/
}

void loop() {
  /***
    Need to divide by 4 because analog inputs range from
    0 to 1023 and each byte of the EEPROM can only hold a
    value from 0 to 255.
  ***/

  int val = analogRead(0) / 4;
  /***
    Write byte val to addres addr of the EEPROM.
  ***/
  EEPROM.write(addr, val); 
  /***
    Increment value addr to store next byte 
    to the EEPROM.
  ***/
  addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;
  }

  delay(100);
}

 

The second example code is implemented how to read a byte from de EEPROM using the EEPROM.read() function:

#include <EEPROM.h>

int address = 0;
byte value;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Ardbox only
  }
}

void loop() {
  // read a byte from the current address of the EEPROM
  value = EEPROM.read(address);

  // print value to the serial port
  Serial.print(address);
  Serial.print("\t");
  Serial.print(value, DEC);
  Serial.println();

  address = address + 1;
  if (address == EEPROM.length()) {
    address = 0;
  }

  delay(500);
}

 

The third example code is implemented how to use the EEPROM.put(). EEPROM.put() is ha function that allow to write into the EEPROM, but with EEPROM.put() you can write a more complex data to the EEPROM like a structure, float, etc.:

#include <EEPROM.h>

struct MyObject {
  float field1;
  byte field2;
  char name[10];
};

void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Ardbox only
  }

  float f = 123.456f;  //Variable to store in EEPROM.
  int eeAddress = 0;   //Location we want the data to be put.


  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, f);

  Serial.println("Written float data type!");

  /** Put is designed for use with custom structures also. **/

  //Data to store.
  MyObject customVar = {
    3.14f,
    65,
    "Working!"
  };

  eeAddress += sizeof(float); //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, customVar);
  Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
}

void loop() {
  /* Empty loop */
}

 

The forth example code is implemented how to us the EEPROM.get(). It is similar that the EEPROM.read(), but instead of read one byte with EEPROM.get() you can get more complex data as a structure:

#include <EEPROM.h>

void setup() {

  float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0; //EEPROM address to start reading from

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Ardbox only
  }
  Serial.print("Read float from EEPROM: ");

  //Get the float data from the EEPROM at position 'eeAddress'
  EEPROM.get(eeAddress, f);
  Serial.println(f, 3);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

  secondTest(); //Run the next test.
}

struct MyObject {
  float field1;
  byte field2;
  char name[10];
};

void secondTest() {
  int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.

  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get(eeAddress, customVar);

  Serial.println("Read custom object from EEPROM: ");
  Serial.println(customVar.field1);
  Serial.println(customVar.field2);
  Serial.println(customVar.name);
}

void loop() {
  /* Empty loop */
}

 

See more information on the official web page of Arduino examples from libraries.

See also:

HOW TO USE STEPPER MOTOR WITH ARDBOX ANALOGHOW TO CONNECT AND CONFIGURE A LOAD CELL USING HX711 TRANSMITTER WITH OUR EQUIPMENT’SHOW TO GENERATE A DIFFERENT PWM’S SIGNALS WITH OURS EQUIPMENT’SHOW TO USE THE WATCHDOGRS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIESHOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’SHOW TO CONNECT A SD CARD TO A PLCHOW TO UPLOAD A BOOTLOADER ON A ARDBOX/M-DUINO PLC

Comments are closed.