Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Teensy 4.0 is freezing while sending/receiving serial2 commands

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Location
    Tijuana, Mexico
    Posts
    8

    Teensy 4.0 is freezing while sending/receiving serial2 commands

    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
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,804
    Ouch, Strings....... Did you try to comment out all Serial2* calls and see if it freezes? that will rule out serial....

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,370
    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 by Frank B; 04-07-2021 at 07:44 PM.

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,370
    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.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    252
    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 by BriComp; 04-07-2021 at 08:16 PM.

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,370
    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?

  7. #7
    Junior Member
    Join Date
    Apr 2021
    Location
    Tijuana, Mexico
    Posts
    8
    Quote Originally Posted by tonton81 View Post
    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()
    Attached Files Attached Files

  8. #8
    Junior Member
    Join Date
    Apr 2021
    Location
    Tijuana, Mexico
    Posts
    8
    Regarding Serial Ports, I connected to 7 & 8
    Is getting stuck before

    if (checkingflag==0)
    {
    if (s0<=55)
    {
    checkingflag=1;
    }
    }

  9. #9

  10. #10
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,370
    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.

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,206
    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,st5;
    
    //...
    st0=strings[0];
      st1=strings[1];
      st2=strings[2];
      st3=strings[3];
      st4=strings[4];
      st5=strings[5];

  12. #12
    Junior Member
    Join Date
    Apr 2021
    Location
    Tijuana, Mexico
    Posts
    8
    I have already changed this, but it stills freezing, I just noticed that when I remove the function runningleds the program works properly

  13. #13
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,637
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •