Store accelerometer data in a memory and read later

Well, I'm collecting data from an accelerometer and storing it in FRAM memory using the FRAM.h library.

The idea seems right, but I'm not getting it right.

Basically, I have a method that runs after setup and stores the data in a memory address, and another that reads the data every 5 seconds. The problem is that in the read method, the values are coming up with a correct value and then a null value.
Maybe there's a mistake in the memory addresses I'm using.

Writing method:

unsigned long writeMicroseconds;
int current_sample = 0;
void writeVibrationInformation() {
  data.rms[0] = 0.0;
  data.rms[1] = 0.0;
  data.rms[2] = 0.0;
  for (int i = 0; i < ACC_DATA_RATE; i++) {
    writeMicroseconds = micros();

    Vibration vibration = readVibration();

    if ((current_sample >= acc_sample_factor)) {
      if (current_sample <= SAMPLES)
        fram.writeFloat(i*sizeof(float), sqrt(vibration.x * vibration.x + vibration.y * vibration.y + vibration.z * vibration.z));
        printLog(String(sqrt(vibration.x * vibration.x + vibration.y * vibration.y + vibration.z * vibration.z)), true);
      current_sample = 0;
    } else
      current_sample++;

    data.rms[0] = (data.rms[0] + (sqrt((vibration.x * vibration.x)) / ACC_DATA_RATE));
    data.rms[1] = (data.rms[1] + (sqrt((vibration.y * vibration.y)) / ACC_DATA_RATE));
    data.rms[2] = (data.rms[2] + (sqrt((vibration.z * vibration.z)) / ACC_DATA_RATE));

    while (micros() < (writeMicroseconds + sampling_period_us)) {}
  }
}

Reading Method:

unsigned long readMilliseconds;
byte address = 0;

bool energy_save = false;

float temperatureValue;
int current_package = 0;
int reset_counter = 0;
bool sendDataDelay = false;
void readAndSendFRAMData() {

  if (energy_save) {
    readMilliseconds = millis();
    sendDataDelay = true;
    energy_save = false;
  } else if (millis() > (readMilliseconds + data_sender_period)) {
    sendDataDelay = true;
  }

  if (sendDataDelay) {
    bool report = false;

    if (hasFRAM) {

      int start = TRANSMISSION_DATA_PACKAGE * current_package;
      int end = (start + TRANSMISSION_DATA_PACKAGE);

      for (int i = start; i < end; i++) {
        address = i * 4;// * sizeof(float);
        float value = 0.0;
        if (address < sizeInBytes) {
          value = fram.readFloat(address);
        }
        vibrationPackage.dataPackage[(i - start)] = value;
      }

      strncpy(vibrationPackage.key, SENSOR_KEY, sizeof(vibrationPackage.key));

      vibrationPackage.type = 'P';

      vibrationPackage.start = start;
      vibrationPackage.end = min(end, SAMPLES);

      printVibrationPackage(vibrationPackage);

      radio.powerUp();

      int a = 0;
      int countSendMaxTries = 120;
      if (start == 0)
        countSendMaxTries = 60;

      while (!report) {
        report = radio.write(&vibrationPackage, sizeof(vibrationPackage));
        delay(20);
        reset_counter++;
        if (reset_counter >= countSendMaxTries) {
          radio.powerDown();
          if (start != 0)
            resetFunc();
          else {
            a++;
            if (a >= 10)
              resetFunc();
            LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
          }
        }
      }
    }

    readMilliseconds = millis();
    sendDataDelay = false;

    if (report) {
      temperatureValue += readTemperature() / package_factor;
      current_package++;
      reset_counter = 0;
    } else {
      reset_counter++;
      if (reset_counter >= 80)
        resetFunc();
    }
  }

  radio.powerDown();
  energy_save = true;
}

Values received from the accelerometer and recorded in the fram:

9.26, 9.19, 8.89, 8.32
9.19, 8.88, 9.04, 8.97
9.21, 8.91, 8.94, 8.89
9.04, 8.97, 9.12, 9.84
8.97, 8.88, 9.19, 9.03
8.55, 8.97, 9.28, 9.11
8.84, 9.04, 8.81, 8.80
8.43, 9.05, 9.21, 8.96
9.11, 8.79, 8.79, 8.89

Data package values received from fram reading:

VibrationPackage:
Size: 31 Bytes
Type: P
Key: 0000A
Data Package: 0.00 9.26 0.00 9.19 0.00 
A: 0
B: 5


Recebido: VibrationPackage:
Size: 31 Bytes
Type: P
Key: 0000A
Data Package: 8.89 0.00 8.32 0.00 9.19 
A: 5
B: 10


Recebido: VibrationPackage:
Size: 31 Bytes
Type: P
Key: 0000A
Data Package: 0.00 8.88 0.00 9.04 0.00 
A: 10
B: 15


Recebido: VibrationPackage:
Size: 31 Bytes
Type: P
Key: 0000A
Data Package: 8.97 0.00 9.21 0.00 8.91 
A: 15
B: 20


Recebido: VibrationPackage:
Size: 31 Bytes
Type: P
Key: 0000A
Data Package: 0.00 8.94 0.00 8.89 0.00 
A: 20
B: 25

Here you can see that in the data package values between the correct values there is always a 0.00

what microcontroller and FRAM are you using?

Not enough information posted.

I suggest to write a short program to read and write data from FRAM, and test it, to make sure you understand the addressing scheme.

FRAM: 256 K (32 K × 8) Bit I2C (MB85RC256V)
And the Microcontroller: Atmega 328p

I assume you are using a UNO
looks as though you are using the String class - this is not recommended on low power low SRAM micros such as the UNO - the memory can become fragmented leading to unpredictable problems and possible system crash
use C type char arrays

also as @jremington suggests write a simple test program then once it is working worry about writing complex data structures

I'm actually saving float values ​​in memory, I just realized that I didn't put the vibraPackage struct in the post, but the values ​​are all floats.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.