I’ve used this code to write angles in the eeprom of the esp32C3 and update them on button press.
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <EEPROM.h>
MPU6050 mpu6050(Wire);
// EEPROM address to store reference angles
int eeAddressX = 0;
int eeAddressY = sizeof(float);
int eeAddressZ = 2 * sizeof(float);
// Variables to store reference angles
float refAngleX = 0.0;
float refAngleY = 0.0;
float refAngleZ = 0.0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
// Read reference angles from EEPROM
EEPROM.get(eeAddressX, refAngleX);
EEPROM.get(eeAddressY, refAngleY);
EEPROM.get(eeAddressZ, refAngleZ);
}
void loop() {
mpu6050.update();
Serial.print("angleX : ");
Serial.print(mpu6050.getAngleX() - refAngleX);
Serial.print("\tangleY : ");
Serial.print(mpu6050.getAngleY() - refAngleY);
Serial.print("\tangleZ : ");
Serial.println(mpu6050.getAngleZ() - refAngleZ);
// Check if reset button is pressed
if (digitalRead(3) == LOW) {
delay(50); // Debounce delay
if (digitalRead(3) == LOW) {
calibrateAngles(); // Reset reference angles to 0.0
}
}
}
void saveReferenceAngles() {
EEPROM.put(eeAddressX, refAngleX);
EEPROM.put(eeAddressY, refAngleY);
EEPROM.put(eeAddressZ, refAngleZ);
}
void calibrateAngles() {
const int readings = 100;
float sumX = 0.0;
float sumY = 0.0;
float sumZ = 0.0;
for (int i = 0; i < readings; i++) {
mpu6050.update();
sumX += mpu6050.getAngleX();
sumY += mpu6050.getAngleY();
sumZ += mpu6050.getAngleZ();
delay(10);
}
refAngleX = sumX / readings;
refAngleY = sumY / readings;
refAngleZ = sumZ / readings;
saveReferenceAngles();
}
I want to clear the emulated EEPROM of the esp32 c3. How can I do it? moreover How will I confirm it that the EEPROM is cleared?
thanks.