Skip to main content
Arduino

Return to Answer

replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

Actually this answer was copied from another thread another thread.

/*
 StackExchange Question: "How to Store 2 analogRead values and read using EEPROM for Arduino uno?"
 Question Id: 15910 (httphttps://arduino.stackexchange.com/questions/15910/how-to-store-2-analogread-values-and-read-using-eeprom-for-arduino-uno)
 Author: Anurag S. Vasanwala ([email protected])
 Tested on Arduino Version 1.6.7
 Description: This sketch helps to store and read, Integer & Double to and from built-in EEPROM.
 Objectives:
 + Write Integer @ Specified Address (Fn: EEPROM_Write_Int)
 + Conversion from Integer to Byte Array
 + Store Bytes
 + Read Integer from Specified Address (Fn: EEPROM_Read_Int)
 + Read Bytes
 + Conversion from Byte Array to Integer
 + Write Double @ Specified Address (Fn: EEPROM_Write_Double)
 + Conversion from Double to Byte Array
 + Store Bytes
 + Read Double from Specified Address (Fn: EEPROM_Read_Double)
 + Read Bytes
 + Conversion from Byte Array to Double
 */
#include <EEPROM.h>
// MACRO : Combines FROM_BYTES into TO_OBJECT of datatype "DATATYPE"
#define CombineBytes(FROM_BYTES, DATATYPE, TO_OBJECT) \
 TO_OBJECT = *((DATATYPE *)FROM_BYTES);
// MACRO : Splits FROM_DATA of DATATYPE into equivalent byte array TO_BYTES
#define GetBytes(FROM_DATA, DATATYPE, TO_BYTES) \
 *((DATATYPE *)TO_BYTES) = FROM_DATA;
// Writes Integer's equivalent bytes to Address and Address + 1
void EEPROM_Write_Int(unsigned int Address, int Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, int, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads two bytes from Address and Address + 1 and returns equivalent int
int EEPROM_Read_Int(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 int Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 // Call macro to combine bytes into equivalent to int
 CombineBytes(Temp_Buffer, int, Result);
 
 return Result;
}
// Writes Double's equivalent bytes to Address, Address + 1, Address + 2 and Address + 3
void EEPROM_Write_Double(unsigned int Address, double Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, double, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads four bytes from Address, Address + 1, Address + 2 and Address + 3 and returns equivalent double
double EEPROM_Read_Double(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 double Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 
 // Call macro to combine bytes into equivalent to double
 CombineBytes(Temp_Buffer, double, Result);
 
 return Result;
}
void setup()
{
 // Initialize Serial @ 9600 baud
 Serial.begin(9600);
 // Print size of datatype just for reference
 /* Serial.print("Size of byte = ");
 Serial.println(sizeof(byte));
 Serial.print("Size of int = ");
 Serial.println(sizeof(int));
 Serial.print("Size of float = ");
 Serial.println(sizeof(float));
 Serial.print("Size of double = ");
 Serial.println(sizeof(double)); */
 int myInt1 = -290;
 int myInt2;
 double myDbl1 = 23.41;
 double myDbl2;
 // Write myInt1 @ 100 memory location; It requires 2 consequtive memory location to store integer
 EEPROM_Write_Int(100, myInt1);
 // Read integer located @ memory location 100 and assign it to myInt2
 myInt2 = EEPROM_Read_Int(100);
 Serial.println(myInt2);
 // Write myInt1 @ 200 memory location; it requires 4 consequtive memory location to store double
 EEPROM_Write_Double(200, myDbl1);
 // Read double located @ memory location 200 and assign it to myDbl2
 myDbl2 = EEPROM_Read_Double(200); 
 Serial.println(myDbl2);
}
void loop()
{
 
}

Actually this answer was copied from another thread.

/*
 StackExchange Question: "How to Store 2 analogRead values and read using EEPROM for Arduino uno?"
 Question Id: 15910 (http://arduino.stackexchange.com/questions/15910/how-to-store-2-analogread-values-and-read-using-eeprom-for-arduino-uno)
 Author: Anurag S. Vasanwala ([email protected])
 Tested on Arduino Version 1.6.7
 Description: This sketch helps to store and read, Integer & Double to and from built-in EEPROM.
 Objectives:
 + Write Integer @ Specified Address (Fn: EEPROM_Write_Int)
 + Conversion from Integer to Byte Array
 + Store Bytes
 + Read Integer from Specified Address (Fn: EEPROM_Read_Int)
 + Read Bytes
 + Conversion from Byte Array to Integer
 + Write Double @ Specified Address (Fn: EEPROM_Write_Double)
 + Conversion from Double to Byte Array
 + Store Bytes
 + Read Double from Specified Address (Fn: EEPROM_Read_Double)
 + Read Bytes
 + Conversion from Byte Array to Double
 */
#include <EEPROM.h>
// MACRO : Combines FROM_BYTES into TO_OBJECT of datatype "DATATYPE"
#define CombineBytes(FROM_BYTES, DATATYPE, TO_OBJECT) \
 TO_OBJECT = *((DATATYPE *)FROM_BYTES);
// MACRO : Splits FROM_DATA of DATATYPE into equivalent byte array TO_BYTES
#define GetBytes(FROM_DATA, DATATYPE, TO_BYTES) \
 *((DATATYPE *)TO_BYTES) = FROM_DATA;
// Writes Integer's equivalent bytes to Address and Address + 1
void EEPROM_Write_Int(unsigned int Address, int Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, int, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads two bytes from Address and Address + 1 and returns equivalent int
int EEPROM_Read_Int(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 int Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 // Call macro to combine bytes into equivalent to int
 CombineBytes(Temp_Buffer, int, Result);
 
 return Result;
}
// Writes Double's equivalent bytes to Address, Address + 1, Address + 2 and Address + 3
void EEPROM_Write_Double(unsigned int Address, double Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, double, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads four bytes from Address, Address + 1, Address + 2 and Address + 3 and returns equivalent double
double EEPROM_Read_Double(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 double Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 
 // Call macro to combine bytes into equivalent to double
 CombineBytes(Temp_Buffer, double, Result);
 
 return Result;
}
void setup()
{
 // Initialize Serial @ 9600 baud
 Serial.begin(9600);
 // Print size of datatype just for reference
 /* Serial.print("Size of byte = ");
 Serial.println(sizeof(byte));
 Serial.print("Size of int = ");
 Serial.println(sizeof(int));
 Serial.print("Size of float = ");
 Serial.println(sizeof(float));
 Serial.print("Size of double = ");
 Serial.println(sizeof(double)); */
 int myInt1 = -290;
 int myInt2;
 double myDbl1 = 23.41;
 double myDbl2;
 // Write myInt1 @ 100 memory location; It requires 2 consequtive memory location to store integer
 EEPROM_Write_Int(100, myInt1);
 // Read integer located @ memory location 100 and assign it to myInt2
 myInt2 = EEPROM_Read_Int(100);
 Serial.println(myInt2);
 // Write myInt1 @ 200 memory location; it requires 4 consequtive memory location to store double
 EEPROM_Write_Double(200, myDbl1);
 // Read double located @ memory location 200 and assign it to myDbl2
 myDbl2 = EEPROM_Read_Double(200); 
 Serial.println(myDbl2);
}
void loop()
{
 
}

Actually this answer was copied from another thread.

/*
 StackExchange Question: "How to Store 2 analogRead values and read using EEPROM for Arduino uno?"
 Question Id: 15910 (https://arduino.stackexchange.com/questions/15910/how-to-store-2-analogread-values-and-read-using-eeprom-for-arduino-uno)
 Author: Anurag S. Vasanwala ([email protected])
 Tested on Arduino Version 1.6.7
 Description: This sketch helps to store and read, Integer & Double to and from built-in EEPROM.
 Objectives:
 + Write Integer @ Specified Address (Fn: EEPROM_Write_Int)
 + Conversion from Integer to Byte Array
 + Store Bytes
 + Read Integer from Specified Address (Fn: EEPROM_Read_Int)
 + Read Bytes
 + Conversion from Byte Array to Integer
 + Write Double @ Specified Address (Fn: EEPROM_Write_Double)
 + Conversion from Double to Byte Array
 + Store Bytes
 + Read Double from Specified Address (Fn: EEPROM_Read_Double)
 + Read Bytes
 + Conversion from Byte Array to Double
 */
#include <EEPROM.h>
// MACRO : Combines FROM_BYTES into TO_OBJECT of datatype "DATATYPE"
#define CombineBytes(FROM_BYTES, DATATYPE, TO_OBJECT) \
 TO_OBJECT = *((DATATYPE *)FROM_BYTES);
// MACRO : Splits FROM_DATA of DATATYPE into equivalent byte array TO_BYTES
#define GetBytes(FROM_DATA, DATATYPE, TO_BYTES) \
 *((DATATYPE *)TO_BYTES) = FROM_DATA;
// Writes Integer's equivalent bytes to Address and Address + 1
void EEPROM_Write_Int(unsigned int Address, int Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, int, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads two bytes from Address and Address + 1 and returns equivalent int
int EEPROM_Read_Int(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 int Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 // Call macro to combine bytes into equivalent to int
 CombineBytes(Temp_Buffer, int, Result);
 
 return Result;
}
// Writes Double's equivalent bytes to Address, Address + 1, Address + 2 and Address + 3
void EEPROM_Write_Double(unsigned int Address, double Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, double, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads four bytes from Address, Address + 1, Address + 2 and Address + 3 and returns equivalent double
double EEPROM_Read_Double(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 double Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 
 // Call macro to combine bytes into equivalent to double
 CombineBytes(Temp_Buffer, double, Result);
 
 return Result;
}
void setup()
{
 // Initialize Serial @ 9600 baud
 Serial.begin(9600);
 // Print size of datatype just for reference
 /* Serial.print("Size of byte = ");
 Serial.println(sizeof(byte));
 Serial.print("Size of int = ");
 Serial.println(sizeof(int));
 Serial.print("Size of float = ");
 Serial.println(sizeof(float));
 Serial.print("Size of double = ");
 Serial.println(sizeof(double)); */
 int myInt1 = -290;
 int myInt2;
 double myDbl1 = 23.41;
 double myDbl2;
 // Write myInt1 @ 100 memory location; It requires 2 consequtive memory location to store integer
 EEPROM_Write_Int(100, myInt1);
 // Read integer located @ memory location 100 and assign it to myInt2
 myInt2 = EEPROM_Read_Int(100);
 Serial.println(myInt2);
 // Write myInt1 @ 200 memory location; it requires 4 consequtive memory location to store double
 EEPROM_Write_Double(200, myDbl1);
 // Read double located @ memory location 200 and assign it to myDbl2
 myDbl2 = EEPROM_Read_Double(200); 
 Serial.println(myDbl2);
}
void loop()
{
 
}
Source Link

EEPROM can store one byte at a location. To store Integer variable, it requires two consecutive location. For Double, it requires four consecutive memory location. I have written function to store and read, int & double to and from built-in EEPROM. As per your question, you can modify it as per your need.

Actually this answer was copied from another thread.

/*
 StackExchange Question: "How to Store 2 analogRead values and read using EEPROM for Arduino uno?"
 Question Id: 15910 (http://arduino.stackexchange.com/questions/15910/how-to-store-2-analogread-values-and-read-using-eeprom-for-arduino-uno)
 Author: Anurag S. Vasanwala ([email protected])
 Tested on Arduino Version 1.6.7
 Description: This sketch helps to store and read, Integer & Double to and from built-in EEPROM.
 Objectives:
 + Write Integer @ Specified Address (Fn: EEPROM_Write_Int)
 + Conversion from Integer to Byte Array
 + Store Bytes
 + Read Integer from Specified Address (Fn: EEPROM_Read_Int)
 + Read Bytes
 + Conversion from Byte Array to Integer
 + Write Double @ Specified Address (Fn: EEPROM_Write_Double)
 + Conversion from Double to Byte Array
 + Store Bytes
 + Read Double from Specified Address (Fn: EEPROM_Read_Double)
 + Read Bytes
 + Conversion from Byte Array to Double
 */
#include <EEPROM.h>
// MACRO : Combines FROM_BYTES into TO_OBJECT of datatype "DATATYPE"
#define CombineBytes(FROM_BYTES, DATATYPE, TO_OBJECT) \
 TO_OBJECT = *((DATATYPE *)FROM_BYTES);
// MACRO : Splits FROM_DATA of DATATYPE into equivalent byte array TO_BYTES
#define GetBytes(FROM_DATA, DATATYPE, TO_BYTES) \
 *((DATATYPE *)TO_BYTES) = FROM_DATA;
// Writes Integer's equivalent bytes to Address and Address + 1
void EEPROM_Write_Int(unsigned int Address, int Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, int, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads two bytes from Address and Address + 1 and returns equivalent int
int EEPROM_Read_Int(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(int);
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 int Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 // Call macro to combine bytes into equivalent to int
 CombineBytes(Temp_Buffer, int, Result);
 
 return Result;
}
// Writes Double's equivalent bytes to Address, Address + 1, Address + 2 and Address + 3
void EEPROM_Write_Double(unsigned int Address, double Data)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 // Call macro to store byte array into Temp_Buffer from Data
 GetBytes(Data, double, Temp_Buffer);
 // Write number of bytes to memory location specified in Address and so on
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 EEPROM.write(Address + Index, Temp_Buffer[Index]);
 }
}
// Reads four bytes from Address, Address + 1, Address + 2 and Address + 3 and returns equivalent double
double EEPROM_Read_Double(unsigned int Address)
{
 // Get the size of datatype to write number of bytes
 const byte SizeOf_DataType = sizeof(double);
 
 // Create tem buffer to get Bytes of Datatype
 byte Temp_Buffer[SizeOf_DataType];
 
 double Result;
 // Read number of bytes from memory location
 for(byte Index = 0; Index < SizeOf_DataType; Index++)
 {
 Temp_Buffer[Index] = EEPROM.read(Address + Index);
 }
 
 // Call macro to combine bytes into equivalent to double
 CombineBytes(Temp_Buffer, double, Result);
 
 return Result;
}
void setup()
{
 // Initialize Serial @ 9600 baud
 Serial.begin(9600);
 // Print size of datatype just for reference
 /* Serial.print("Size of byte = ");
 Serial.println(sizeof(byte));
 Serial.print("Size of int = ");
 Serial.println(sizeof(int));
 Serial.print("Size of float = ");
 Serial.println(sizeof(float));
 Serial.print("Size of double = ");
 Serial.println(sizeof(double)); */
 int myInt1 = -290;
 int myInt2;
 double myDbl1 = 23.41;
 double myDbl2;
 // Write myInt1 @ 100 memory location; It requires 2 consequtive memory location to store integer
 EEPROM_Write_Int(100, myInt1);
 // Read integer located @ memory location 100 and assign it to myInt2
 myInt2 = EEPROM_Read_Int(100);
 Serial.println(myInt2);
 // Write myInt1 @ 200 memory location; it requires 4 consequtive memory location to store double
 EEPROM_Write_Double(200, myDbl1);
 // Read double located @ memory location 200 and assign it to myDbl2
 myDbl2 = EEPROM_Read_Double(200); 
 Serial.println(myDbl2);
}
void loop()
{
 
}

This code is successfully tested on Arduino Version "1.6.7".

AltStyle によって変換されたページ (->オリジナル) /