FastLED matrix resetting

I was wondering if someone could look over this sketch for me, and let me know if and where I have made any mistakes. It is a hash of a few example sketches. I’m a complete noob to coding and I have no idea where to go from here.
The sketch is currently “running” on a Nano clone, and I am experiencing resetting, freezing when the ScreenSavers exit, and the DisplayClock portion of the code runs. The sketch will usually reset the Nano then display the clock, (PIR sensor is active (HIGH) for 10 seconds). On the odd occasion, it will completely lock up forcing a reset via on-board button.
Serial.print in the sketch is there for debugging porpoises and does not change how the sketch behaves.
I have also ran the three function consecutively with the same behaviour.

Sorry if this is not the correct section.
Much thanks
Code below.

#include <FastLED.h>
#include <Adafruit_GFX.h>
#include <FastLED_NeoMatrix.h>
#include <gamma.h>
#include <Wire.h>
#include "DS3231.h"
DS3231 Clock;
RTClib RTC;
#define PIN_PIX 6 //LED Matrix
#define PIN_PIR 3 //PIR Sensor
#define PIN_BUT_ADJ 10 // Push Button for clock adjustment
#define PIN_BUT_HP 11 // Push button to add hour to clock
#define PIN_BUT_MP 12 // Push button to add minute to clock
#define matrixWidth 32
#define matrixHeight  8
#define NUM_LEDS    matrixWidth * matrixHeight
#define FASTLED_ALLOW_INTERRUPTS 0
#define UPDATES_PER_SECOND 100

uint8_t Switcher = 0;

byte bcurHour = 11;
byte bcurMin = 22;
String curTime = "01:02";

//Noise Playgroud default values
uint32_t x, y, v_time, hue_time, hxy;
uint8_t octaves = 1;
uint8_t hue_octaves = 1;
int xscale = 7771;
int yscale = 7771;
uint8_t hue_scale = 4;
uint8_t time_speed = 111;
uint8_t hue_speed = 1;
int x_speed = 331;
int y_speed = 1111;

CRGB leds[NUM_LEDS]; //this value randomly multiplies itself causing arduino to lock up


//FastLED Matrix Configuration
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, matrixWidth, matrixHeight, matrixWidth / matrixWidth, 1,
    NEO_MATRIX_BOTTOM     + NEO_MATRIX_RIGHT +
    NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );
const uint16_t colors[] = {matrix->Color(240, 255, 255)};
CRGBPalette16 currentPalette( CRGB::Black);
CRGBPalette16 targetPalette( PartyColors_p );


void setup() {
  Serial.begin(57600);
  Serial.println(F("resetting!"));
  delay(4000);
  Wire.begin();
  pinMode(PIN_BUT_ADJ, INPUT);
  pinMode(PIN_BUT_HP, INPUT);
  pinMode(PIN_BUT_MP, INPUT);
  pinMode(PIN_PIR, INPUT);

  FastLED.addLeds<WS2812, PIN_PIX, GRB>(leds, NUM_LEDS);
  //Serial.println(NUM_LEDS);
  FastLED.setBrightness(50);
  FastLED.clear();
  matrix->begin();
  matrix->setTextWrap(false);
  matrix->setBrightness(1);
  matrix->setTextColor(colors[0]);
  matrix->clear();

  //Noise Playgroud more default values
  random16_set_seed(3000);
  //random16_add_entropy(analogRead(3));
  random16_add_entropy(analogRead(1));
  hxy = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  x = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  y = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  v_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  hue_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();

}

void loop() {

  if (digitalRead(PIN_PIR) == LOW) {
    if (Switcher == 0) {


      //Noise Playgroud Randomised values
      xscale = random(-8000, 8000);
      yscale = xscale;
      hue_scale = random(2, 4.1);
      octaves = random(1, 3.1);
      hue_octaves = random(1, 3.1);
      hue_speed = random(1, 3.1);
      //Serial.println(F("Start Screensaver!"));*/

      ScreenSaver01();
      Switcher = 1;

    } else {

      ScreenSaver02();
      Switcher = 0;

    }

  } else {

    //Serial.println(F("Start Clock!"));
    ClockDisplay();

  }

}
void ClockDisplay() {

  Serial.println(F("Clock Start"));
  FastLED.clear();
  //FastLED.show();

  matrix->clear();
  matrix->show();
  delay(20);
  bool h12;
  bool PM;

  for (int i = 0; i <= 10; i++) {

    DateTime now = RTC.now();
    if (Clock.getHour(h12, PM) < 10)
    {
      bcurHour = '0', Clock.getHour(h12, PM);
    } else {
      bcurHour = Clock.getHour(h12, PM);
    }
    if (Clock.getMinute() < 10)
    {
      bcurMin = '0', Clock.getMinute();
    } else {
      bcurMin = Clock.getMinute();
    }
    Serial.println(F("Set Time"));
    delay(100);
    //String cDiv = ":";
    String cDiv = "|";
    String cSpa = " ";
    curTime = bcurHour + cDiv + bcurMin;
    Serial.println(F("Print Time"));
    delay(100);
    matrix->setBrightness(((500 - (analogRead(1) / 2)) / 20));
    Serial.println(F("setBrightness"));
    delay(100);
    //matrix->fillScreen(0);
    matrix->clear();
    Serial.println(F("fillScreen"));
    delay(100);
    matrix->show();
    Serial.println(F("Show"));
    delay(100);
    matrix->setCursor(1, 0);
    Serial.println(F("setCursor")); //usually the last line printed in serial interface before reset
    delay(100);
    matrix->print(curTime);
    Serial.println(F("Print"));
    delay(100);
    matrix->show();
    Serial.println(F("Show"));
    delay(100);
    Serial.println(F("Half Time"));
    delay(500);

    //matrix->fillScreen(0);
    matrix->clear();
    curTime = bcurHour + cSpa + bcurMin;
    matrix->setCursor(1, 0);
    matrix->print(curTime);
    matrix->show();
    Serial.println(F("Displayed Time"));
    delay(500);

  }
  matrix->clear();
  matrix->show();
  Serial.println(F("ClockMain!EXIT"));
  delay(2000);
}
void ScreenSaver01() {

  Serial.println(F("Screen Saver 01!"));
  delay(10);
  for (int i = 0; i <= 200; i++) {
    if (digitalRead(PIN_PIR) == LOW) {

      fill_2dnoise16(FastLED.leds(), 32, 8, false,
                     octaves, x, xscale, y, yscale, v_time,
                     hue_octaves, hxy, hue_scale, hxy, hue_scale, hue_time, false);
      FastLED.setBrightness(220 - (analogRead(1) / 4));
      FastLED.show();
      Serial.print(F("SS01-"));
      Serial.println(i);
      // adjust the intra-frame time values
      x += x_speed;
      y += y_speed;
      v_time += time_speed;
      hue_time += hue_speed;

    } else {
      //FastLED.clear();
      //FastLED.show();
      Serial.println(F("Screen Saver01! BREAK"));
      break;
      //delay(1000);
      //ClockDisplay(); // loop change here seems to reset arduino or crash sketch
    }

  }
  FastLED.clear();
  FastLED.show();
  Serial.println(F("Main Loop SS01! EXIT"));
  delay(1000);
}
void ScreenSaver02() {

  Serial.println(F("Screen Saver 02!"));
  for (int i = 0; i <= 200; i++) {
    if (digitalRead(PIN_PIR) == LOW) {
      FastLED.setBrightness(220 - (analogRead(1) / 4));
      ChangePalettePeriodically();
      uint8_t maxChanges = 24;
      nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);


      static uint8_t startIndex = 0;
      startIndex = startIndex + 1; /* motion speed */
      FillLEDsFromPaletteColors( startIndex);
      Serial.print(F("SS02-"));
      Serial.println(i);
      FastLED.show();
      FastLED.delay(1000 / UPDATES_PER_SECOND);

    } else {
      //FastLED.clear();
      //FastLED.show();
      Serial.println(F("Main Loop SS02! BREAK"));
      //delay(2000);
      break;

    }

  }
  FastLED.clear();
  FastLED.show();
  Serial.println(F("Screen Saver 02!EXIT"));
  delay(2000);
}


void FillLEDsFromPaletteColors( uint8_t colorIndex) {
  uint8_t brightness = (220 - (analogRead(1) / 4));

  for ( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i * 16), brightness);
    colorIndex += 3;
  }
}


void ChangePalettePeriodically() {
  uint8_t secondHand = (millis() / 1000) % 60;
  static uint8_t lastSecond = 99;

  if ( lastSecond != secondHand) {
    lastSecond = secondHand;
    CRGB p = CHSV( HUE_PURPLE, 255, 255);
    CRGB g = CHSV( HUE_GREEN, 255, 255);
    CRGB b = CRGB::Black;
    CRGB w = CRGB::White;
    if ( secondHand ==  0)  {
      targetPalette = RainbowColors_p;
    }
    if ( secondHand == 10)  {
      targetPalette = CRGBPalette16( g, g, b, b, p, p, b, b, g, g, b, b, p, p, b, b);
    }
    if ( secondHand == 20)  {
      targetPalette = CRGBPalette16( b, b, b, w, b, b, b, w, b, b, b, w, b, b, b, w);
    }
    if ( secondHand == 30)  {
      targetPalette = LavaColors_p;
    }
    if ( secondHand == 40)  {
      targetPalette = CloudColors_p;
    }
    if ( secondHand == 50)  {
      targetPalette = PartyColors_p;
    }
  }
}
#define matrixWidth 32
#define matrixHeight  8
#define NUM_LEDS    matrixWidth * matrixHeight

CRGB leds[NUM_LEDS]; //this value randomly multiplies itself causing arduino to lock up

That comment is sheer stupidity. The amount of memory needed by that array does NOT randomly multiply itself. If is EXTREMELY unlikely that you HAVE enough memory to handle 256 LEDs on a Nano.

yup.

Ran the code on a 2560 and it seems ok for now. Having said that the 2 ScreenSaver loops run fine for hours without the clock portion enabled.

However that value serial.printed as part of the FastLED_NeoMatrix.h does randomly change from the 256 that it should be set at.

Thanks for your enlightening input.

      hue_scale = random(2, 4.1);
      octaves = random(1, 3.1);
      hue_octaves = random(1, 3.1);
      hue_speed = random(1, 3.1);

The random function does NOT take float arguments.

However that value serial.printed as part of the FastLED_NeoMatrix.h does randomly change from the 256 that it should be set at.

I have no idea what you are talking about, since you did not post a link to the FastLED_NoeMatrix library that you are using.

Sorry! didn't realize that i forgot link the Libs in the sketch

random() in a uint8 or uint16 should drop any numbers after the decimal?
or thats how i have read the referance material.

random() in a uint8 or uint16 should drop any numbers after the decimal?
or thats how i have read the referance material.

That’s true, but it is silly to use literal values that you KNOW are the wrong type.

The FastLED_NeoMatrix header file does not have any Serial.print() statements in it, so I still don’t know what you are talking about.

Personally, I would not use that piece of crap library. It looks like the kind of crap Adafruit publishes, where they can’t be bothered to repeat the function return type for each function.

i’m not going to argue with that. My sketch is BOTCHED to heck, held together with hopes and dreams, and if i was able to find an simple example of printing text on the standard FastLED lib i don’t think i would be in this situation.

excerpt from FastLED_NeoMatrix.cpp for referance.

// Constructor for single matrix:
FastLED_NeoMatrix::FastLED_NeoMatrix(CRGB *leds, uint8_t w, uint8_t h, 
    uint8_t matrixType): 
  Adafruit_GFX(w, h),
  type(matrixType), matrixWidth(w), matrixHeight(h), tilesX(0), tilesY(0), 
  remapFn(NULL) { 
    _leds = leds;
    // WARNING: Serial.print seems to crash in the constructor, 
    // but works in begin()
    numpix = matrixWidth * matrixHeight;
  }

// Constructor for tiled matrices:
FastLED_NeoMatrix::FastLED_NeoMatrix(CRGB *leds, uint8_t mW, uint8_t mH, 
    uint8_t tX, uint8_t tY, uint8_t matrixType) :
  Adafruit_GFX(mW * tX, mH * tY),
  type(matrixType), matrixWidth(mW), matrixHeight(mH), tilesX(tX), tilesY(tY), 
  remapFn(NULL) { 
    _leds = leds;
    numpix = matrixWidth * matrixHeight * tilesX * tilesY;
 }

void FastLED_NeoMatrix::begin() {
 // Serial.print("Num Pixels: ");
 // Serial.println(numpix);
}

// Expand 16-bit input color (Adafruit_GFX colorspace) to 24-bit (NeoPixel)
// (w/gamma adjustment)
static uint32_t expandColor(uint16_t color) {
  return ((uint32_t)pgm_read_byte(&gamma5[ color >> 11       ]) << 16) |
         ((uint32_t)pgm_read_byte(&gamma6[(color >> 5) & 0x3F]) <<  8) |
                    pgm_read_byte(&gamma5[ color       & 0x1F]);
}

any idea how i can ditch this lib and achieve the same result???

We need to go back to my original question. How much memory does ONE instance of the CRGB class take? How much memory does 256 instances take? How much memory does the Nano have?

The answer to the first question is easy to determine.

Serial.print("One instance of the CRGB class uses ");
Serial.print(sizeof(CRGB));
Serial.println(" bytes.");

The answer to the second one is easy. Just multiply the first answer by 256.

The answer to the third one is easy, too.

SRAM 2 KB

If the answer to the second question is not significantly less than the answer to the third question (and by significantly I mean 25% or more), then you can NOT use the Nano.