Function with multiple arrays causing Arduino Nano to reset repeatedly

Im currently putting my finishing touches on a project which involves Serial communication between an arduin and a raspberry. For development i used an arduino Mega. But want my finished project to run on a Nano. However it seems like the Function "led.displayNumbers();" is causing my nano to reset. The function is only called once the "packetHandler();" has finished which manages the incoming data from the raspi. I assume that the nano is running out memory due to the amount operations on those arrays there which would explain why it is running fine on the mega but im not sure so im asking for help.

My Code:

#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixels(120, 5, NEO_GRB + NEO_KHZ800); 
byte digitTimeValue[12];
int timeChangeTest[12];
int skipCount[6];
volatile byte dataArray[28];
byte dataBuffer[30];
int lastTime;
int lastTime2;
long delay2IncreaseTest;
long delay2[13]; 
bool receiveInProgress;
//Clock class ###########################################################################################################

class Clock {
  
  public:

  void getRawTime() {
    digitTimeValue[5] = dataArray[5] % 10;
    digitTimeValue[4] = dataArray[5] / 10;
    digitTimeValue[3] = dataArray[4] % 10;
    digitTimeValue[2] = dataArray[4] / 10;
    digitTimeValue[1] = dataArray[3] % 10;
    digitTimeValue[0] = dataArray[3] / 10;
    digitTimeValue[11] = dataArray[2] % 10;
    digitTimeValue[10] = dataArray[2] / 10;
    digitTimeValue[9] = dataArray[1] % 10;
    digitTimeValue[8] = dataArray[1] / 10;
    digitTimeValue[7] = dataArray[0] % 10;
    digitTimeValue[6] = dataArray[0] / 10;
Serial.println("test");
    if(delay2IncreaseTest != millis()) {
      for(int i = 0; i <= 12; ++i) {
        ++delay2[i];
      }
      delay2IncreaseTest = millis();
    }
  }

  bool timeChanged(int timeType) {
    if(timeChangeTest[timeType] != digitTimeValue[timeType]) {
      timeChangeTest[timeType] = digitTimeValue[timeType];
      return true;
    }
    else {
      return false;
    }
  }
  
  void resetDelay(int slot) {
    delay2[slot] = 0;
  }
  
  int delayExecution(int slot, int delayVal) {
    if(delay2[slot] > delayVal && delayVal != 0) {
      delay2[slot] = 0;
      return true;
    }
    else {
      return false;
    }
  }

  int skip(int slot, int times) {
    ++skipCount[slot];
    if(skipCount[slot] > times) {
      skipCount[slot] = 0;
      return true;
    }
    else {
      return false;
    }
  }
  private:
};

//led class ###########################################################################################################

class LED {
  int digitValue[12];
  bool controlState[12];
  int fade = 0;
  int offset = 1;
  int modeTest;
  int legacyColor[240][3];
  int explosionArray[2][6];
  int cycles;
  
public:
  void initialize() {
    for(int i = 0; i <= 11; ++i) {
      digitValue[i] = 0;
      controlState[i] = false;
    }
  }
  void setDigitValues() {
    Clock clk;
    for(int i = 0; i <= 11; ++i) {
      if(clk.timeChanged(i) && i != 4 && i != 5) {
        controlState[i] = true;
      }
      if(controlState[i] && clk.delayExecution(i, 10)) {
        --digitValue[i];
          if(digitValue[i] < 0) {
            digitValue[i] = 9;
          }
      }
      else if(!controlState[i]) {
        digitValue[i] = digitTimeValue[i];
      }
      if(digitValue[i] == digitTimeValue[i]) {
        controlState[i] = false;
      }
    }
  }

  void displayNumbers() {
    Clock clk1;
    lastTime = digitTimeValue[5];
    lastTime2 = digitTimeValue[3];
    pixels.clear();
    int colorData[6][3];
    int x = 6;
    if(dataArray[24] == 3 || dataArray[24] == 4) {
      switch(fade) {
          case 0:
            if(colorData[5][0] - dataArray[25] > 0 || colorData[5][2] + dataArray[25] < 253) {
              colorData[5][0] = colorData[5][0] - dataArray[25];
              colorData[5][2] = colorData[5][2] + dataArray[25];
              if(colorData[5][2] >= 253) {
                fade = 1;
              }
            }
            else {
              fade = 1;
              colorData[5][0] = 0;
              colorData[5][2] = 253;
            }
          break;
          
          case 1:
            if(colorData[5][2] - dataArray[25] > 0 || colorData[5][1] + dataArray[25] < 253) {
              colorData[5][2] = colorData[5][2] - dataArray[25];
              colorData[5][1] = colorData[5][1] + dataArray[25];
              if(colorData[5][1] >= 253) {
                fade = 2;
              }
            }
            else {
              fade = 2;
              colorData[5][2] = 0;
              colorData[5][1] = 253;
            }
          break;
          
          case 2:
            if(colorData[5][1] - dataArray[25] > 0 || colorData[5][0] + dataArray[25] < 253) {
              colorData[5][1] = colorData[5][1] - dataArray[25];
              colorData[5][0] = colorData[5][0] + dataArray[25];
              if(colorData[5][0] >= 253) {
                fade = 0;
              }
            }
            else {
              colorData[5][1] = 0;
              colorData[5][0] = 253;
              fade = 0;
            }
          break;
        }
        for(int i = 0; i <= 2; ++i) {
          for(int j = 239; j >= 1; --j) {
            legacyColor[j][i] = legacyColor[j - 1][i];
          }
          legacyColor[0][i] = colorData[5][i];
        }
    }
    switch(dataArray[24]) {
      case 1:
        for(int i = 0; i <= 5; ++i) {
          for(int j = 0; j <= 2; ++j) {
            colorData[i][j] = dataArray[x];
            ++x;
          }
        }
        break;

      default: break;
      case 2:
        for(int i = 0; i <= 2; ++i) {
          for(int j = 0; j <= 2; ++j) {
            colorData[i * 2][j] = dataArray[x];
            colorData[i * 2 + 1][j] = dataArray[x];
            ++x;
          }
        }
        break;
      case 4:
        if(modeChanged()) {
          for(int i = 0; i <= 5; ++i) {
            for(int j = 0; j <= 2; ++j) {
              colorData[i][j] = 0;
            }
          }
          colorData[5][0] = 253;
        }
        for(int i = 4; i >= 0; --i) {
          for(int j = 0; j <= 2; ++j) {
            colorData[i][j] = legacyColor[(4 - i) * 40][j];
          }
        }
        break;
      case 3:
        if(modeChanged()) {
          for(int i = 0; i <= 5; ++i) {
            for(int j = 0; j <= 2; ++j) {
              colorData[i][j] = 0;
            }
          }
          colorData[5][0] = 253;
        }
        for(int i = 4; i >= 0; --i) {
          for(int j = 0; j <= 2; ++j) {
            colorData[i][j] = colorData[5][j];
          }
        }
        break;
    }
    for(int i = 0; i <= 5; ++i) {
      int var;
      if(dataArray[27] == 1) {
        var = digitValue[i + 6];
      }
      else {
        var = digitValue[i];
      }
      int r = colorData[i][0] * dataArray[26] / 100, g = colorData[i][1] * dataArray[26] / 100, b = colorData[i][2] * dataArray[26] / 100;
      pixels.setPixelColor(var * 2 + (i * 20), pixels.Color (r, g, b));
      pixels.setPixelColor(var * 2 + (i * 20) + 1, pixels.Color (r, g, b));
    }
    pixels.show();
  }

  private:
    int modeChanged() {
      if(dataArray[24] != modeTest) {
        modeTest = dataArray[24];
        return true;
      }
      else {
        return false;
      }
    }
};

void packetHandler() {
  digitalWrite(9, HIGH);
  bool received = false;
  //Serial.println("pre packet");
  while(!received) {
    if(Serial.available() > 0) {
      
      digitalWrite(10, HIGH);
      dataBuffer[29] = Serial.read();
      //Serial.println(dataBuffer[29]);
      if(dataBuffer[0] == 254 && dataBuffer[29] == 255) {
        for(int i = 1; i <= 29; ++i) {
          dataArray[i - 1] = dataBuffer[i];
        }
        digitalWrite(9, LOW);
        received = true;
        //Serial.println("test 1");
      }
      for(int i = 0; i <= 28; ++i) {
        dataBuffer[i] = dataBuffer[i + 1];
      }
    }
    else { digitalWrite(10, LOW); }
  }
}

void setup() {
  Serial.begin(115200);
  Clock clock;
  LED led;
  led.initialize();
  pixels.begin();
  pinMode(8, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(9, LOW);
  digitalWrite(7, HIGH);
  while(true) {
    digitalWrite(7, LOW);
    //if(Serial){digitalWrite(7,HIGH);}else{digitalWrite(7,LOW);}
    //Serial.println("start2");
    packetHandler();
    clock.getRawTime();
    led.setDigitValues();
    led.displayNumbers();
  }
}

void loop() {}

Are we being entirely serious here?

Yes its needed for a smooth color transition

So that's (120 x 3) + (240 x 2 X 3) = 1800 bytes of RAM.
Out of 2048.

You would usually write this

like this

  digitalWrite(9, LOW);
  digitalWrite(7, HIGH);
} // end of setup

void loop() {
    digitalWrite(7, LOW);
    //if(Serial){digitalWrite(7,HIGH);}else{digitalWrite(7,LOW);}
    //Serial.println("start2");
    packetHandler();
    clock.getRawTime();
    led.setDigitValues();
    led.displayNumbers();
}

Ok i realized that this code may not be the best thought out but after changing some data types to byte everything worked fine. So thanks to TheMemberFormerlyKnownAsAWOL (funny name lol) for pointing out that array i completly overlooked.

I know but i didnt want to reinitialize the classes every time. But in most c++ applications you have int main(){} which runs only once so its not uncommon.

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