Memory Issues

Hello Everyone!

I’m working in a project where I have to save some data with a module GY 521 then plug the arduino to my computer (I’m using batteries) to send saved data. So far im testing the saving data part plugged to my computer and it works fine at first but then it starts to behave weird. This is my serial output:

14:25:32.467 -> X = 17240 | Y = 252 | Z = 1296 | i = 126
14:25:32.865 -> X = 17108 | Y = 304 | Z = 1196 | i = 127
14:25:33.230 -> X = 17080 | Y = 280 | Z = 1204 | i = 128
14:25:33.628 -> X = 17092 | Y = 384 | Z = 1292 | i = 129
14:25:33.993 -> X = 17176 | Y = 424 | Z = 1260 | i = 130
14:25:34.391 -> X = 17180 | Y = 356 | Z = 1288 | i = 131
14:25:34.789 -> X = 17248 | Y = 388 | Z = 1192 | i = 132
14:25:35.153 -> X = 17240 | Y = 328 | Z = 1308 | i = 133
14:25:35.552 -> X 8 ⸮C | Y = 412 | Z = 1308 | i = 134
14:25:35.950 -> X  3[C | Y = 320 | Z = 1192 | i = 135
14:25:36.314 -> X  4⸮C | Y = 364 | Z = 1152 | i = 136
14:25:36.712 -> X  3⸮C | Y = 340 | Z = 1040 | i = 137
14:25:37.077 -> X  3[C | Y = 356 | Z = 1236 | i = 138
14:25:37.476 -> X  3⸮C | Y = 332 | Z = 1344 | i = 139
14:25:37.840 -> X  38C | Y = 364 | Z = 1172 | i = 140
14:25:38.238 -> X  3B | Y = 388 | Z = 1208 | i = 141
14:25:38.636 -> X  3KC | Y = 364 | Z = 1148 | i = 142
14:25:39.001 -> X  3_C | Y = 392 | Z = 1240 | i = 143
14:25:39.399 -> X  3⸮C | Y = 376 | Z = 1264 | i = 144
14:25:39.763 -> X  3⸮C | Y = 360 | Z = 1220 | i = 145
14:25:40.162 -> X  34C | Y = 260 | Z = 1260 | i = 146

I save that data and print it later but it does not save data after i=133.
Its not a sensor problem I’ve tested it for a long period and it never stops sending data. I suppose it is a memory issue but I dont know why if it works fine at first and arduino compiler does not warn me about the memory, it tels me “Sketch uses 6050 bytes (18%) of program storage space. Maximum is 32256 bytes.
Global variables use 418 bytes (20%) of dynamic memory, leaving 1630 bytes for local variables. Maximum is 2048 bytes.” so i guess problem is inside the code itself.

Those are my codes (its mandatory to use cpp):

ino code:

#include <Wire.h>
#include "Gy521.h"

Gy521 gy = Gy521();
int16_t **data;

int data_size = 200;

void setup() {
  Serial.begin(115200);
  //Serial.println(F("Serial Ready"));
  gy.init();
  //Serial.println(F("Gy ready"));

  data = (int16_t**)malloc(data_size * sizeof(int16_t*));
  for (int i = 0; i < data_size; i++)
    data[i] = (int16_t*)malloc(3 * sizeof(int16_t));
  //Serial.println(F("Memory ready"));

  pinMode(7, OUTPUT);

}
void loop() {
  int i;
  for (i = 0; i < data_size; i++) {
    gy.read_data(data[i]);
    Serial.print(F("X = ")); Serial.print(data[i][0]);
    Serial.print(F(" | Y = ")); Serial.print(data[i][1]);
    Serial.print(F(" | Z = ")); Serial.print(data[i][2]);
    Serial.print(F(" | i = ")); Serial.println(i);

    digitalWrite(7, LOW);
    delay(333);

    digitalWrite(7, HIGH);
    delay(50);
  }
  digitalWrite(7, LOW); i = 0;
  for (i = 0; i < data_size; i++) {
    Serial.print(F("Accelerometer: "));
    Serial.print(F("X = ")); Serial.print(data[i][0]);
    Serial.print(F(" | Y = ")); Serial.print(data[i][1]);
    Serial.print(F(" | Z = ")); Serial.print(data[i][2]);
    Serial.print(F(" | i = ")); Serial.println(i);

  }
  digitalWrite(7, HIGH);
  while (1) {
    gy.read_data(data[0]);
    Serial.print(F("Accelerometer: "));
    Serial.print(F("X = ")); Serial.print(data[0][0]);
    Serial.print(F(" | Y = ")); Serial.print(data[0][1]);
    Serial.print(F(" | Z = ")); Serial.print(data[0][2]);
    Serial.print(F(" | time = ")); Serial.print(millis());
    Serial.print(F(" | i = ")); Serial.println(i);
    delay(333);
  };
}

Gy521.h:

#ifndef Gy521_h
#define Gy521_h
#include <Wire.h>
#include "Arduino.h"

class Gy521{
        private:
            int16_t *_Ac;
            int16_t* _Gy;
            const uint8_t MPU = 0x68;
            uint8_t _addrs;
        public:
            Gy521();
            Gy521(uint8_t);
            void init();
            void read_data(int16_t*);
            int16_t* accelerometer_data();
            int16_t* gyroscope_data();
};

#endif

Gy521.cpp

#include "Gy521.h"
#include "Arduino.h"
#include <Wire.h>

Gy521::Gy521(){
	//normal constructor
	_addrs = MPU;
	_Ac = (int16_t*)malloc(3*sizeof(int16_t));
	_Gy = (int16_t*)malloc(3*sizeof(int16_t));
	
}
Gy521::Gy521(uint8_t addrs){
	//in case gyroscope uses a diferent addrs
	_addrs = addrs;
}

void Gy521::init(){
	Wire.beginTransmission(_addrs);
	Wire.write(0x6b);
	Wire.write(0);
	Wire.endTransmission(true);
}
void Gy521::read_data(int16_t *data){

	Wire.beginTransmission(_addrs);
	Wire.write(0x3B);
	Wire.endTransmission(false);
	Wire.requestFrom(_addrs, 12, true);
	_Ac[0] = Wire.read()<<8|Wire.read();
	_Ac[1] = Wire.read()<<8|Wire.read();
	_Ac[2] = Wire.read()<<8|Wire.read();
	_Gy[0] = Wire.read()<<8|Wire.read();
	_Gy[1] = Wire.read()<<8|Wire.read();
	_Gy[2] = Wire.read()<<8|Wire.read();
	data[0] = _Ac[0];
	data[1] = _Ac[1];
	data[2] = _Ac[2];
	//data[3] = millis();

}
int16_t* Gy521::accelerometer_data(){
	return _Ac;
}
int16_t* Gy521::gyroscope_data(){
	return _Gy;
}

Thank you so much for any help! :smiley: :smiley:

First, you're blindly using whatever malloc returns, without ever checking to see if you've run out of memory. Not a good idea...

Second, have you calculated how much memory your arrays are using? You have 200 pointers to int16_t*, and 600 int16_t, but only 1630 TOTAL bytes of free ram.

Why you're doing dynamic allocation at all, given that the array size is fixed, is a mystery to me.

Thats the only way I know to do it, is there any other way to make it work without the pointers?

meletchirino:
Thats the only way I know to do it, is there any other way to make it work without the pointers?

Simply do away with the dynamic allocation, and define a 2D global array. Do that, and your REAL problem will become instantly obvious.

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