#include <EEPROM.h> // Include the EEPROM library
void setup() {
// Start serial communication at 9600 baud rate
Serial.begin(9600);
// Wait for the Serial Monitor to open
while (!Serial);
// Print a welcome message to the user
Serial.println("EEPROM Access");
Serial.println("Commands:");
Serial.println("read [address] - Read data from EEPROM at the given address");
Serial.println("write [address] [value] - Write value to EEPROM at the given address");
}
void loop() {
// Check if data is available to read from the serial input
if (Serial.available()) {
String command = Serial.readStringUntil('\n'); // Read input command
// Trim any leading or trailing spaces
command.trim();
// If the command is "read"
if (command.startsWith("read")) {
// Extract the EEPROM address (after the "read" command)
int address = command.substring(5).toInt();
// Check if the address is within the valid range (0-1023 for Arduino Uno)
if (address >= 0 && address <= 1023) {
byte value = EEPROM.read(address); // Read the value from EEPROM
Serial.print("Value at address ");
Serial.print(address);
Serial.print(": ");
Serial.println(value);
} else {
Serial.println("Invalid address. Please enter an address between 0 and 1023.");
}
}
// If the command is "write"
else if (command.startsWith("write")) {
// Find the position of the space after "write"
int spaceIndex = command.indexOf(' ', 6);
// Extract the EEPROM address and value
int address = command.substring(6, spaceIndex).toInt();
int value = command.substring(spaceIndex + 1).toInt();
// Check if the address is within the valid range
if (address >= 0 && address <= 1023) {
EEPROM.write(address, value); // Write the value to EEPROM
Serial.print("Wrote value ");
Serial.print(value);
Serial.print(" to address ");
Serial.println(address);
} else {
Serial.println("Invalid address. Please enter an address between 0 and 1023.");
}
}
// Handle invalid commands
else {
Serial.println("Invalid command. Use 'read' or 'write'.");
}
}
}