Last active
May 2, 2024 03:25
-
-
Save tranchausky/0f929769e0008df1e6d7e7173ab0d63a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <EEPROM.h> | |
const int EEPROM_SIZE = 512; // EEPROM size in bytes | |
const int MAX_STRING_LENGTH = 50; // Maximum string length | |
void setup() { | |
Serial.begin(115200); | |
EEPROM.begin(EEPROM_SIZE); // Initialize EEPROM | |
} | |
void loop() { | |
int int1 = 123; | |
int int2 = -456; | |
String myString = "Hello, EEPROM!"; | |
// Write data to EEPROM | |
writeDataToEEPROM(int1, int2, myString); | |
// Read back and print data from EEPROM | |
int readInt1, readInt2; | |
String readString; | |
readDataFromEEPROM(readInt1, readInt2, readString); | |
Serial.print("Integers read from EEPROM: "); | |
Serial.print(readInt1); | |
Serial.print(", "); | |
Serial.println(readInt2); | |
Serial.print("String read from EEPROM: "); | |
Serial.println(readString); | |
delay(1000); | |
} | |
void writeDataToEEPROM(int int1, int int2, const String &myString) { | |
int address = 0; | |
// Write integers to EEPROM | |
EEPROM.put(address, int1); | |
address += sizeof(int1); // Move to the next address | |
EEPROM.put(address, int2); | |
address += sizeof(int2); // Move to the next address | |
// Write string to EEPROM | |
for (int i = 0; i < myString.length(); ++i) { | |
EEPROM.write(address + i, myString[i]); | |
} | |
EEPROM.write(address + myString.length(), '\0'); // Null-terminate the string | |
// Commit changes to EEPROM | |
EEPROM.commit(); | |
} | |
void readDataFromEEPROM(int &int1, int &int2, String &myString) { | |
int address = 0; | |
// Read integers from EEPROM | |
EEPROM.get(address, int1); | |
address += sizeof(int1); // Move to the next address | |
EEPROM.get(address, int2); | |
address += sizeof(int2); // Move to the next address | |
// Read string from EEPROM | |
char buffer[MAX_STRING_LENGTH]; | |
int bytesRead = 0; | |
while (true) { | |
char character = EEPROM.read(address + bytesRead); | |
if (character == '\0' || bytesRead >= MAX_STRING_LENGTH - 1) { | |
break; | |
} | |
buffer[bytesRead++] = character; | |
} | |
buffer[bytesRead] = '\0'; // Null-terminate the string | |
myString = String(buffer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <EEPROM.h> | |
const int EEPROM_SIZE = 512; // EEPROM size in bytes | |
void setup() { | |
Serial.begin(115200); | |
EEPROM.begin(EEPROM_SIZE); // Initialize EEPROM | |
} | |
void loop() { | |
int int1 = 123; | |
int int2 = -456; | |
// Write integers to EEPROM | |
writeIntsToEEPROM(int1, int2); | |
// Read back and print integers from EEPROM | |
int readInt1; | |
int readInt2; | |
readIntsFromEEPROM(readInt1, readInt2); | |
Serial.print("Integers read from EEPROM: "); | |
Serial.print(readInt1); | |
Serial.print(", "); | |
Serial.println(readInt2); | |
delay(1000); | |
} | |
void writeIntsToEEPROM(int int1, int int2) { | |
// Check if EEPROM has enough space | |
if (sizeof(int1) * 2 <= EEPROM_SIZE) { // Check if there's enough space for both integers | |
// Write integers to EEPROM | |
int address = 0; | |
EEPROM.put(address, int1); | |
address += sizeof(int1); // Move to the next address | |
EEPROM.put(address, int2); | |
// Commit changes to EEPROM | |
EEPROM.commit(); | |
} else { | |
Serial.println("EEPROM does not have enough space to save the integers."); | |
} | |
} | |
void readIntsFromEEPROM(int &int1, int &int2) { | |
// Read back integers from EEPROM | |
int address = 0; | |
EEPROM.get(address, int1); | |
address += sizeof(int1); // Move to the next address | |
EEPROM.get(address, int2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment