Teensy 4.1 RAM issu

Status
Not open for further replies.

yoc1983

New member
Hi,
I got my first teensy 4.1 for a project that required it and it does weird things.
so i wrote a simple sketch to check the RAM

The sketch is simply writing numbers to an array
then it goes over the array and prints them to the serial

When i am using an array of a size around more then 4000 things get weird

example1: array of 3000 (graph_size=3000)
prints ok to serial every time last value is 2999

example2: array of 10,000 (graph_size=10,000)
doesn't print all the array every time
serial prints for each loop stops randomly at certain numbers:
4621, 4963, 5304, 7011, 7352, 9999

also sometimes it prints some gibberish on the way

would be happy to get some idea what i am missing..


*****this is the sketch:****

const int graph_size=10000;
int graph[graph_size];

void setup() {
}

void loop() {

for(int i = 0; i < graph_size ; i++)
graph=i;

for(int i = 0; i < graph_size ; i++)
Serial.println(graph);

delay(2000);
}
 
Just noticed you have not braced your loops {} so you are overwriting data

for(int i = 0; i < graph_size ; i++)
{graph=i;}

for(int i = 0; i < graph_size ; i++)
{Serial.println(graph);}
 
It is running and complete before the computer has USB Serial connected to display the prints.
Also the printing is going faster than the PC can make sense of.

Code:
  const int graph_size=10000;
  int graph[graph_size];

void setup() {
  while( !Serial && millis() <4000 ); // wait up to 4 seconds for Serial to connect to PC
}

void loop() {

  for(int i = 0; i < graph_size ; i++)
    graph[i]=i;

  for(int i = 0; i < graph_size ; i++) {
    Serial.println(graph[i]);
    if ( !(i%40) ) // selective delay to assure PC can keep up
      delay( 1 );
  }

  delay(2000);
}

CODE block '#' blocking used to show formatting

Code not tested ... but should give proper result

@MikeA > if , for and while execute a single statement - in this case those are single statements, braces are needed to make a single statement when multiple 'lines' / statements are present.
 
Just noticed you have not braced your loops {} so you are overwriting data

for(int i = 0; i < graph_size ; i++)
{graph=i;}

for(int i = 0; i < graph_size ; i++)
{Serial.println(graph);}


wasn't aware of that
you mean it might write data in RAM that in still need?
 
It is running and complete before the computer has USB Serial connected to display the prints.
Also the printing is going faster than the PC can make sense of.

Code:
  const int graph_size=10000;
  int graph[graph_size];

void setup() {
  while( !Serial && millis() <4000 ); // wait up to 4 seconds for Serial to connect to PC
}

void loop() {

  for(int i = 0; i < graph_size ; i++)
    graph[i]=i;

  for(int i = 0; i < graph_size ; i++) {
    Serial.println(graph[i]);
    if ( !(i%40) ) // selective delay to assure PC can keep up
      delay( 1 );
  }

  delay(2000);
}

CODE block '#' blocking used to show formatting

Code not tested ... but should give proper result

@MikeA > if , for and while execute a single statement - in this case those are single statements, braces are needed to make a single statement when multiple 'lines' / statements are present.

OK,thanks yes seems like a nice solution

the 4 sec waiting for serial is a must?
i never used it and didn't got problems but maybe in some scenario it can help?
 
Four second wait allows for a couple things - but that code will leave the while() and continue as soon as Serial is online whether under 1 second or up to 4 seconds - at 4 seconds it leaves even if no Serial has connected.
> PC usually connects to the PC when millis() is about 400 - maybe up to 700 depending on machine when the SerMon is active and waiting.
> But if SerMon wasn't started when the upload completes then 4 seconds allows it to wait for user to click SerMon and for it to connect

Note the Delay(1) is a long time - there is a finer granularity where perhaps delayMicroseconds(100); would be a better starting point and allow it to complete faster.

Teensy is easy to play with printing anything - the only issues are:
> Teensy can connect before the PC connects USB
> Teensy - especially the 4.x - can overwhelm the PC with output faster than it can be processed and displayed on a PC
 
Status
Not open for further replies.
Back
Top