Can't assign value to global array

Hello,
I'm writing code to measure the serialization speed of nanopb, a protocol buffers implementation in c for embedded systems.
The code takes values from a sensor and assigns them to a protocol buffers message.
Then this message is serialized by nanopb. The serialization time is measured by Arduino Chrono library which gives me the time as an unsinged long, which works fine so far.

Since im repeating the measuring several times i want to store the time of each serialization procedure in an array, so i can compute the average time it takes.
To do this i created a global int array like this:

int serializationTime[100];

But whenever i try to assign a value to this array from any of my functions the programm stops working.
It compiles without errors, but the serial monitor only outputs some characters of the first println like this:

NQNEN⸮N⸮N⸮NENENEN⸮N⸮NENENEN⸮NENEN⸮N⸮N⸮NENENEN⸮NENENENENENEN⸮N⸮NEN⸮⸮⸮N⸮N⸮NENENENE⸮⸮NEW LOOP

This is my code:

#include <Chrono.h>

//Proto generated file
#include "./src/serializationResults.pb.h"
#include "./src/testmessages.pb.h"

//Nanopb
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"

#include "./src/generateTestmessage.h"

//Define Pin for analog Light sensor input
#define LIGHT_INPUT A5

#define SENSOR_ARRAY_SIZE 24

//Treshold for number of readings
#define TRESHOLD 100

//Chrono setup
Chrono myChronoMicros(Chrono::MICROS);

//Proto message setup

//Create testmessage for all data types
AllTypesMessage message = AllTypesMessage_init_zero;

//Create array for sensor values
//Limit array size to number of fields in message
int sensorValues[SENSOR_ARRAY_SIZE];

//Create global array to store results
int serializationTime[TRESHOLD];

bool done = false;


int getLightSensorData() {
  return analogRead(LIGHT_INPUT);
}


void printResultData() {
  for (int i = 0; i < TRESHOLD; i++) {
    Serial.println(serializationTime[i]);
  }
}

//Serialize the message
void serializeData() {
  unsigned long timePassed;
  int counter = 0;
  while (counter < TRESHOLD) {
    //uint8_t : unsigned integer of length 8 bits
    uint8_t buffer[128];


    //Construct output stream for writing into a memory buffer
    //1st input: buffer 2nd input: maximum number of bytes to write -> size of buffer
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    //Start timer to measure serialization time
    myChronoMicros.restart();

    //Call pb_encode function for encoding
    //1st argument: address of pb_ostream_t variable, 2nd argument: autogenerated fields description array, 3rd argument: address of TestMessage struct
    //Output: boolean true=encoding successful false= encoding failed
    bool status = pb_encode(&stream, AllTypesMessage_fields, &message);


    //Stop timer and get result
    myChronoMicros.stop();
    timePassed = myChronoMicros.elapsed();
    if (!status)
    {
      Serial.println("Failed to encode");
      return;
    }


    //Add results to result array
    serializationTime[counter] = timePassed;
    Serial.println(serializationTime[counter]);

    counter++;
  }
}

void setup() {

  Serial.begin(9600);

  for (int i = 0; i < SENSOR_ARRAY_SIZE; i++) {
    sensorValues[i] = getLightSensorData();
  }

  message = generateTestMessage(sensorValues);
}

void loop() {
  Serial.println("NEW LOOP");


  if (done == false) {
    serializeData();
    printResultData();
    done = true;
  }

}

Im trying to assign the value of timePassed to the serializationTime array at the bottom of the serializeData() function.
Can anyone please tell me what I'm doing wrong, since im pretty new to arduino and the c language.

Sidenote: I get the same problem if i create a proto message for Result with a repeated serializationTime field with max_count:100 (int32_t serializationTime[100]; ) and try to assign values to this array, so I guess I'm doing something wrong with the assignment of a value to an array?

Thanks in advance for any help.

You sure it is not your serial monitor baud rate? Are any other serial messages getting displayed correctly?

Also, if you look in the Chrono library the elapsed() method returns type chrono_t. I am certain this value will not fit into a type int. You should declare timePassed and serializationTime as type chrono_t. In this case, chrono_t is an unsigned long

instead of assigning it a value returned from myChronoMicros.elapsed(), why not initialize timePassed and try incrementing each iteration

ToddL1962:
You sure it is not your serial monitor baud rate? Are any other serial messages getting displayed correctly?

Also, if you look in the Chrono library the elapsed() method returns type chrono_t. I am certain this value will not fit into a type int. You should declare timePassed and serializationTime as type chrono_t. In this case, chrono_t is an unsigned long

I added some more serial messages and they all get displayed correctly at 9600 baud (NL+CR) if i comment out the value assignment to the array.
I tried declaring the array and variable as chrono_t like this

chrono_t serializationTime[TRESHOLD];
chrono_t timePassed;

but when compiling I get an error saying:
'chrono_t' does not name a type; did you mean 'Chrono'?

Also if I add too many serial.println() the serial monitor starts acting weird again and stops outputting after a few println's.

//Serialize the message
void serializeData() {
  unsigned long timePassed;
  unsigned long timeIncrement = 0;
  int counter = 0;
  while (counter < TRESHOLD) {
    //uint8_t : unsigned integer of length 8 bits
    uint8_t buffer[128];

    Serial.println("Creating stream");
    //Construct output stream for writing into a memory buffer
    //1st input: buffer 2nd input: maximum number of bytes to write -> size of buffer
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    //Start timer to measure serialization time
    myChronoMicros.restart();

    Serial.println("Encoding");
    //Call pb_encode function for encoding
    //1st argument: address of pb_ostream_t variable, 2nd argument: autogenerated fields description array, 3rd argument: address of TestMessage struct
    //Output: boolean true=encoding successful false= encoding failed
    bool status = pb_encode(&stream, AllTypesMessage_fields, &message);


    //Stop timer and get result
    myChronoMicros.stop();
    timePassed = myChronoMicros.elapsed();
    timeIncrement = timeIncrement + timePassed;
    if (!status)
    {
      Serial.println("Failed to encode");
      return;
    }
    Serial.println("Encoding finished");
    //Serial.println("Serialization time:");
    Serial.println(timePassed);
    Serial.println(timeIncrement);

    counter++;
  }
  //Serial.println("Average serialization time: ");
  Serial.println(timeIncrement / TRESHOLD);
}

This works fine. Output:

Creating stream
Encoding
Encoding finished
13784
17248

However if i uncomment one println at the bottom of the function the output looks like this:

Creating stream
Encoding
Encoding finished
Serialization time:
3781920511
3972513527⸮⸮

If i uncomment both, all i get is "NEW L".
The only other baudrate that is not outputting gibberish is 1000000, but the behaviour is the same. If i uncomment the println I get garbage characters and the values of timePassed and timeIncrement also change.

gcjr:
instead of assigning it a value returned from myChronoMicros.elapsed(), why not initialize timePassed and try incrementing each iteration

Thank you, I followed your advice and use an additional varibale for timeIncrement, which seems to work fine for now.
I guess this is a workaround for my problem, but I still don't know why the assignment of a variable to an array doesnt work.

You will have to call it Chrono::chrono_t because it is defined within the Chrono class.

I'm beginning to wonder if you are running out of RAM which is causing the weirdness.