Teensy 4.0 is freezing while sending/receiving serial2 commands

Status
Not open for further replies.
Hello
I'm working with a Teensy 4.0 to run a 3 minutes countdown timer, showing it on a Nextion(NX4832T035_011) display, I used to run it on Arduino Uno and Mega and everything worked pretty well, but I decided to change the project to Teensy, the issue is that when I run the timer, the teensy gets frozen and I need to remove the power source or press the programming button, sometimes it freezes when the time is up, some other times when the time is running (usually at minute 1:38), I've removed everything else on my code to only run Nextion and countdown timer and it stills freezing, also I've tried adding Serial2.flush(), waiting for a while to refresh my display, removing debug serial, run it with an external power source and no computer, changing the teensy many times, reinstalling everything regarding Arduino and Teensy, but nothing seems to work. :(:(:(


Software: Arduino 1.8.13 & Teensyduino 1.53
Hardware: Teensy 4.0

Regards,
Miguel
 

Attachments

  • newMainNoThreads.ino
    59 KB · Views: 63
Ouch, Strings....... Did you try to comment out all Serial2* calls and see if it freezes? that will rule out serial....
 
Normally, a freeze is a hardfault.
Unfortunatley, Teensyduino jumps to a endless loop, then, instead of showing them ;)
I'd look for array / buffer overflows, nullpointers, etc.
 
Last edited:
all the code in this style
Code:
void thirteen()
{
 digitalWrite(demux0,LOW);
 digitalWrite(demux1,HIGH);
 digitalWrite(demux2,HIGH);
 digitalWrite(demux3,LOW);
 digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite(20,LOW);
  digitalWrite(21,LOW);
  digitalWrite(22,LOW);
  digitalWrite(23,LOW);
  digitalWrite(30,LOW);
}
may need a delay between setting the mux and the other pins. Teensy is much fast faster than the other boards, and it may be to fast for a "demux".
We had this issue often, in this forum.

But you gave no information re: the hardware, so take it with a grain of salt.

Them when porting from AVR, you should remember that teensy s 32Bit, so an int is 32 Bit and most other types change their widths too.
As above, often an issue.
 
You may have got confused with naming the Serial Ports.
The Rx/Tx pair on the top left, pins 0 and 1, are Serial1. On the Uno/Mega I suspect that the first Hardware Serial port is Serial2 whilst on Teensy it is Serial1.

I would also say that you need to put in some Serial debug statements to find out where in your program its getting stuck.
 
Last edited:
Code:
String strings[5];//
An array with 5 entries.

Pretty fascinating that this works on AVR..
Code:
 for (int i = 0; i <=5 ; i++)
   {  
    strings[i]= myString.substring(0,myString.indexOf("|"));

[...]
A loop over 6 entries.

It's more a bug that AVR does not crash...

Doesn't it show a warning?
 
Ouch, Strings....... Did you try to comment out all Serial2* calls and see if it freezes? that will rule out serial....

Yes it stills freezing, I just upload the code attached and it gets frozen when the timer ends after turn lampy low and before reach green()
 

Attachments

  • noStrings.ino
    18.7 KB · Views: 44
Regarding Serial Ports, I connected to 7 & 8
Is getting stuck before

if (checkingflag==0)
{
if (s0<=55)
{
checkingflag=1;
}
}
 
Most likely the problem are all the out of bound accesses to the array (Post #6) - there may be more than the String strings[5] array.
ist has 5 elements. the first is [0] - the last element is [4]
You have several places where you use strings[5] or the loop I mentioned.
 
Not seeing any Serial or Seria2 used in p#7 code to Freeze????

As noted Serial would be USB and Serial1 would be UART1 (on RX1 and TX1 pins ) and Serial2 is UART2 (on RX2 and TX2 pins )

As far as the code hanging - first this would be to declare 6 STRINGS using if needed as :: String strings[6];

Or edit the code to only refer to 5 at most - As noted the <=5 case and also snippet below shows out of bounds usage:
Code:
String st0,st1,st2,st3,st4,[B][COLOR="#FF0000"]st5[/COLOR][/B];

//...
st0=strings[0];
  st1=strings[1];
  st2=strings[2];
  st3=strings[3];
  st4=strings[4];
  [B][COLOR="#FF0000"]st5=strings[5];[/COLOR][/B]
 
I have already changed this, but it stills freezing, I just noticed that when I remove the function runningleds the program works properly
 
I would guess that runningLeds is calculating an invalid value for the index 'cled' into the ledfunctions array. This calculation:
Code:
  int tled=st-ct;
  float pled=tled/float(st);
boils down to pled = (st-ct)/st
which is the same as pled = 1 - ct/st
If ct > st then pled will be negative which will cause an out of bounds array reference.
If ct <= st then pled will be 0 <= pled <=1 (because st and ct are presumed positive)
However, this means that calculating cled = pled*20 results in cled having the range 0 <= cled <= 20 which will cause an out of bounds array reference if cled = 20.
Add a debugging statement in runningLeds to test the value of cled before it is used:
Code:
  if(cled < 0 || cled > 19) Serial.printf("ERROR: invalid value of cled = %d\n",cled);

Pete
 
Status
Not open for further replies.
Back
Top