Disclamer: I am new to using this forum, so if there is anything I've done improperly, I'd love to know. Thanks.
Hi. I am having a problem trying to write some values to my arduino's EEPROM, and then get last few values to print on the serial monitor when I unplug the arduino, and plug it back in (the last 200 values before unplugging are to be printed when plugged back in). However, my code is not returning the values that I am intending to have returned.
The variable I'm doing this with is called accelMag, and the values that are printed to the serial monitor when I print the accelMag are right around 1.00 or 0.99. However, this is what I'm getting from the EEPROM:
This is the part of my code that is using the EEPROM:
void rememberData() { // goal is to write last 200 accelMag values to the EEPROM, adress 0-199
if (adress < 200) { // adress starts at zero, add one for each time
EEPROM.put(adress, accelMag); // accelMag should be a float
++adress;
}
else { // if have already written to 200 adresses, go back and update
adress2 = adress%200; // if adress is above 200, I want to update the adress 200 below it with the current value (only writing to first 200 adresses)
EEPROM.put(adress2, accelMag); // accelMag should be a float
++adress;
}
}
void readEEPROM() { // printing past values of EEPROM before starting to record new values
while (number < 200) { // only for adresses that were written to
EEPROM.get(number, adressValue); // adressValue is the value (float) written at the adress "number" (adress 0-199)
Serial.println(adressValue); // print each value from written adresses
++number;
}
}
If there is anything you need to know about the entire code, here is an explanation of the project and the full code compiled.
I am using an arduino uno to make a project where a MPU-6050 is being used to work with values from its accelerometer. I am trying to get the total magnitude vector of the linear acceleration, and store it to the EEPROM of the board, so that if I use the board with a battery, when I plug it back into my computer, I can see the last 200 magnitudes of the total linear accelerations.
If there are any areas in this code that could be improved, or you have a better idea of how to remember the last 200 accelMag values and print them when the board is plugged back in, please let me know. I am new to coding arduino.
Full Code:
#include <Wire.h>
#include <EEPROM.h>
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
float accelMag; //maxAccel
int adress = 0, adress2;
int number = 0;
float adressValue;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU();
readEEPROM();
}
void loop() {
recordAccelRegisters();
calcMag();
rememberData();
printData();
delay(50);
}
void setupMPU(){
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processAccelData();
}
void processAccelData(){
gForceX = accelX / 15074.0;
gForceY = accelY / 15074.0;
gForceZ = accelZ / 15074.0;
}
void calcMag() {
accelMag = sqrt(sq(gForceX)+sq(gForceY)+sq(gForceZ));
}
void printData() {
Serial.print(accelMag);
}
void rememberData() { // goal is to write last 200 accelMag values to the EEPROM, adress 0-199
if (adress < 200) { // adress starts at zero, add one for each time
EEPROM.put(adress, accelMag); // accelMag should be a float
++adress;
}
else { // if have already written to 200 adresses, go back and update
adress2 = adress%200; // if adress is above 200, I want to update the adress 200 below it with the current value (only writing to first 200 adresses)
EEPROM.put(adress2, accelMag); // accelMag should be a float
++adress;
}
}
void readEEPROM() { // printing past values of EEPROM before starting to record new values
while (number < 200) { // only for adresses that were written to
EEPROM.get(number, adressValue); // adressValue is the value (float) written at the adress "number" (adress 0-199)
Serial.println(adressValue); // print each value from written adresses
++number;
}
}
Thanks in advance