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?