[SOLVED] Arduino Sketch won't run if serial.print lines are removed

So, here's the thing...
I added a couple of lines to my code, to print my free memory (serial.print) because I'm having some memory leak issues. However, when I REMOVE those lines, arduino just won't run the code at all.

Yes, the code is messy, long, and not good by any means. I'm a junior programmer, and this is just a simple game I'm trying to make, using a 8x8 LED matrix.

Some classes I have defined

class Bicho
{
  public:
  short int cerebro[][8];
  short int energia;
  short int pos_x;
  short int pos_y;
  short int age;
  short int pensar
  {
    // procesamiento de inputs para devolver output
  };
  Bicho(){
    for (int i = 0; i<8; i++){
      for (int o = 0; o<4; o++){
        cerebro[o][i] = random(-10,11);
      }
    }
    energia = 1000;
    pos_y = random(0,8);
    pos_x = random(0,8);
    age = 0;
  }
};
class Nutriente
{
  public:
  uint8_t energia;
  uint8_t mutageno;
  uint8_t pos_x;
  uint8_t pos_y;
  uint8_t color;
  Nutriente(){
    if (random(10)<=7){
      energia = 100;
      mutageno = 3;
      pos_y = random(0,8);
      pos_x = random(0,8);
      color = 96; 
    }else{
      energia = 30;
      mutageno = 10;
      pos_y = random(0,8);
      pos_x = random(0,8);
      color = 64;
    }
  }
};

This is the main file

#include "FastLED.h"
#include "Bicho.h"
#include "Nutriente.h"
#include <ArduinoSTL.h>
#include <vector>
#include <MemoryFree.h>
#define DATA_PIN    6
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    64
#define BRIGHTNESS  30
#define SPEED 10

/*
Red (0..) "HUE_RED"
Orange (32..) "HUE_ORANGE"
Yellow (64..) "HUE_YELLOW"
Green (96..) "HUE_GREEN"
Aqua (128..) "HUE_AQUA"
Blue (160..) "HUE_BLUE"
Purple (192..) "HUE_PURPLE"
Pink(224..) "HUE_PINK"
 */

CRGB leds[NUM_LEDS];
std::vector<Bicho> bichos;
std::vector<Nutriente> nutrientes;

void setup() {
  Serial.begin(115200);
  bichos.insert(new Bicho());
  bichos.insert(new Bicho());
  //bichos.insert(new Bicho());
  
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  nutrientes.insert(new Nutriente());
  
  // tell FastLED about the LED strip configuration & config brightness
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
}

void moveBichos(){

  for (int b = 0; b < bichos.size(); b++){
    int randx = 0;
    int randy = 0;
    
    if (random(0,2)>=1){
      randx = random(-1,2);
    }else{
      randy = random(-1,2);
    }
  
    bichos[b].pos_x += randx;
    bichos[b].pos_y += randy;
  
    if (bichos[b].pos_x + randx > 7){
      bichos[b].pos_x = 7;
    }else if(bichos[b].pos_x + randx < 0){
      bichos[b].pos_x = 0;
    }
    if (bichos[b].pos_y + randy > 7){
      bichos[b].pos_y = 7;
    }else if(bichos[b].pos_y + randy < 0){
      bichos[b].pos_y = 0;
    }
    if (!(randx == 0 && randy == 0)){
      bichos[b].energia -= 3;
    }
    bichos[b].age += 1;
  }
}

void comerYMatar(){
  for (int b = 0; b < bichos.size(); b++){
    for (int n = 0; n < nutrientes.size(); n++){
      if (bichos[b].pos_x == nutrientes[n].pos_x && bichos[b].pos_y == nutrientes[n].pos_y){
        bichos[b].energia += nutrientes[n].energia;
        nutrientes[n] = nutrientes.back();
        nutrientes.pop_back();        
        n--;
        std::vector<Nutriente> tmp(nutrientes);
        nutrientes.swap(tmp);
      }
    }
    if (bichos[b].energia <=0){
      bichos[b] = bichos.back();
      bichos.pop_back();
      b--;
      std::vector<Bicho> tmp(bichos);
      bichos.swap(tmp);
    }
  }
}

void loop() {
  // Esto es solo para monitorear la RAM mientras la app está en desarrollo
  EVERY_N_MILLISECONDS(100){
    Serial.print("SRAM: ");
    Serial.println(freeMemory());
  }

  EVERY_N_SECONDS(2){
    spawnNutriente();
  }
  
  EVERY_N_MILLISECONDS(100 - SPEED){
    FastLED.clear();
    
    printNutrientes();
    moveBichos();
    comerYMatar();
    printBichos();

    FastLED.show();
  }
}

void printBichos(){
  for (uint8_t b = 0; b < bichos.size(); b++){
      printCoordsColor(bichos[b].pos_x, bichos[b].pos_y, 160);
    }
}

void spawnNutriente(){
  if (nutrientes.size()<20){
      nutrientes.insert(new Nutriente());
  }
}

void printNutrientes(){
  for (int n = 0; n < nutrientes.size(); n++){
      printCoordsColor(nutrientes[n].pos_x, nutrientes[n].pos_y, nutrientes[n].color);
    }
}

void printCoordsColor(int pos_x, int pos_y, uint8_t color){
  int led = pos_y%2==0?pos_x+pos_y*8:(7-pos_x)+pos_y*8;
  leds[led] = CHSV( color , 200, 255);
}

The lines I'm referring to, are specifically these;

EVERY_N_MILLISECONDS(100){
    Serial.print("SRAM: ");
    Serial.println(freeMemory());
  }

If I remove those lines, the code just won't run. It will compile, but won't run. I wanted to get rid of those lines in order to save a little more memory... but, why is this happening?

What exactly do you mean by "it won't run"? What do you expect to happen and at what point does the real output differ from your expectations?
If pin 13 is still available replace the Serial.print's with code to toggle pin 13. This pin is connected to the built-in arduino led. This way, you can place debugging output without using the Serial class.

The arduino is connected to a NeoPixel LED RGB Matrix. If I upload the code to the arduino, it will run and make the LEDs turn on and "appear to move".
However if remove the lines I said, the matrix won't turn on. Even If I replace the Serial.print("SRAM: "); Serial.println(freeMemory()); lines with a simple "Serial.println("hello"); it won't do it. It won't turn on the matrix, and I don't get anything in the serial monitor.

short int cerebro[][8];

This is wrong.

Okay, how so? I just tried to do a multidimensional array of short ints. What's wrong with it and what can I do to fix it?

Just include the number of rows also. Like so,

short int cerebro[4][8];

Funny thing, if I change it to [4][8], the board won't run the code. Same issue as removing those two lines I mentioned. This is driving me crazy, this should not make the arduino freeze on boot... =(

What arduino are you using? How low are you on memory?

When first running it, memory is about 900B, but slowly going down and in about 5 minutes it crashes at about 120B memory (I assume because of the heap colliding with the stack). I'm using an Arduino UNO R3 (not an official one, but never had any troubles.. at least not like this)

smeagol44:
When first running it, memory is about 900B, but slowly going down and in about 5 minutes it crashes at about 120B memory (I assume because of the heap colliding with the stack).

You're wasting your time looking at Serial.print. The memory leak is your problem. Somewhere, you are consuming and/or corrupting memory - creating too many object instances, and never destroying them, over-running arrays, writing using bad pointers, etc. Figure out why memory is being consumed, fix that, and your problem will go away.

Regards,
Ray L.

RayLivingston:
You're wasting your time looking at Serial.print. The memory leak is your problem. Somewhere, you are consuming and/or corrupting memory - creating too many object instances, and never destroying them, over-running arrays, writing using bad pointers, etc. Figure out why memory is being consumed, fix that, and your problem will go away.

Regards,
Ray L.

Well, then, I'm not sure how to fix it. I have two vectors of objects, and I'm pretty sure that even if the size doesn't change much (between 10 and 20) the capacity does. And I've googled like mad to find a solution, but not sure how should I do it. The lines where I try to destroy de objects (as of right now) are:

nutrientes[n] = nutrientes.back();
nutrientes.pop_back();        
n--;
std::vector<Nutriente> tmp(nutrientes);
nutrientes.swap(tmp);

and

bichos[b] = bichos.back();
bichos.pop_back();
b--;
std::vector<Bicho> tmp(bichos);
bichos.swap(tmp);

Any help in that? =( I'm pretty new at C++

smeagol44:
I have two vectors of objects [...]

Don't use vector's. They are implemented using dynamic memory allocation, which results in memory fragmentation until you run out of memory. Instead, create an array, capable of holding the maximum amount of elements you expect.

Changing everything to arrays was a bit complicated, but in the end, it solved every single problem. I guess I still have a long road with C++/Arduino...
Thank you so much, guys. Marking this as solved.