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

Thread: T4.1 wont appear as a serial USB device unless I put analogRead in the code.

  1. #1
    Member JaredReabow's Avatar
    Join Date
    May 2020
    Location
    oxford , UK
    Posts
    74

    T4.1 wont appear as a serial USB device unless I put analogRead in the code.

    In the code below, if I do not put the analogRead function in the code, the Teensy wont show up via USB and I have to press the reset to upload new code.
    However if I put the analogRead in, it functions without issue.
    Any help would be appreciated.


    Code:
    #define randomVar 16
    
    void setup() 
    { 
     Serial.begin(9600); 
     Serial4.begin(57600);  //Baud Rate for AT-command Mode.  
     analogRead(randomVar );
     Serial.println("*** ONLINE ***"); 
    } 
    
    void loop() 
    { 
     if (Serial4.available()){
       Serial.println(Serial4.readString()); 
       
     }
     if (Serial.available()){
       Serial4.write(Serial.read());
       tone(13,1000,40);
      } 
    }

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    520
    I verified your sketch on a T4.1 and with or without the analogRead(randomVar ); line, it shows a serial USB device [COM port].
    Did you set Menu > Tools > USB Type to Serial?

    Using Arduino 1.8.13 & Teensyduino 1.53 on Windows 10.

    Regards,
    Paul

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,006
    Maybe Teensy is transmitting "*** ONLINE ***" before your PC is able to complete USB detection, and then you interpret the lack of that message as the USB device not working? But that doesn't explain needing the press the button to reprogram.

    I can confirm I commented out the analogRead line and I was able to repeatedly upload without pressing the pushbutton.

    To get "*** ONLINE ***" to appear in the serial monitor, use this:

    Code:
    #define randomVar 16
    
    void setup() 
    { 
     Serial.begin(9600);
     while (!Serial) {
       // wait for Arduino serial monitor to open
     }
     Serial4.begin(57600);  //Baud Rate for AT-command Mode.  
     //analogRead(randomVar );
     Serial.println("*** ONLINE ***"); 
    } 
    
    void loop() 
    { 
     if (Serial4.available()){
       Serial.println(Serial4.readString()); 
       
     }
     if (Serial.available()){
       Serial4.write(Serial.read());
       tone(13,1000,40);
      } 
    }

  4. #4
    Member JaredReabow's Avatar
    Join Date
    May 2020
    Location
    oxford , UK
    Posts
    74
    I am in serial mode, I also tried whilst in mtp mode.

    It is not that it doesn't print to serial, it is that the device doesn't even show up on USB or run blink

  5. #5
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,666
    try another PC, could be a windows issue

  6. #6
    Member JaredReabow's Avatar
    Join Date
    May 2020
    Location
    oxford , UK
    Posts
    74
    Quote Originally Posted by tonton81 View Post
    try another PC, could be a windows issue
    Although i appreciate the suggestion, there must be a reason for this problem on my computer that I hope someone may have an idea towards debugging?

  7. #7
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,666
    Just wanted to rule out if it was a PC issue or not

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,230
    Have you tried Pauls sketch?

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,006
    Just to repeat, I did indeed run the code you shared on a Teensy 4.1 here. I was able to upload several times without pressing the pushbutton. I can’t explain the behavior you have described. But I did test with a board here and it definitely did not do what you have said.
    Last edited by PaulStoffregen; 02-25-2021 at 10:25 PM. Reason: Typo

  10. #10
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,893
    Might really help to understand what exactly is your setup?

    Windows? Linux? MAC? ... What version of Arduino are you running? What version of Teensyduino are you running?

    Anything changed on your machine, like using different versions of Core or ...

    Also understand there is a significant difference between running as USB type of Serial and running on USB type of MTP.

    With MTP you are not using Serial, but instead you are using Serial Emulation.

    And for example if you are running on Linux fixed a problem where for example if you built using some USB type that did not use hardware, the Serial monitor would not work nor could you program the board without hitting the program button... This was an issue with udev rules...

    But again not sure if this is your issue or not... I am guessing Not:

    But again if you are trying to use MTP you might try using the MTP with Serial option and see if that solves it for you...

    If this is not the issue, could be simple timing issue as mentioned and your analogRead() really did not do anything except delay a bit of time.

    Most of my sketches will have something in it like what Paul put in, except I put in some timeout such that my code an work when USB is not connected

    // which will wait up to 5 seconds. Note I do this at the start so mills is sufficient otherwise I would put in an elapsedMillis object set to zero before this and check that value...
    Something like: while (!Serial && millis() < 5000) {

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,006
    Even though the while (!Serial) doesn't explain the lockup where pressing the pushbutton was needed, I do believe it's time to reconsider the previously failed attempt to have Serial.begin() wait.

    I'm going to put this Serial.begin() delay we previous tried back into the code.

    https://github.com/PaulStoffregen/co...1cd983cc98403e

    Before it was 2.5 seconds, which was mostly motivated by the slowness of Windows 7's and weird delays USB drivers. It also just wanted 2.5 seconds in every case, even if no USB cable was connected. Looks like about 0.6 seconds is needed for normal USB enumeration to become apparent.

    I want to give this another try for 1.54-beta8 and we'll decide whether it causes too much pain before a 1.54 release. If we can have more programs written for Arduino Uno "just work" and not have to go to the much worse way Uno uses where opening the port hard reboots the main processor, even though it adds startup delay the improvement in usability might be worthwhile.

    Or maybe, like before, the extra startup delay will be too painful for too many people? I am listening and this might get pulled out yet again before 1.54 releases... but I do want to eventually find a way to avoid the confusion people experience when running programs written for Uno & Mega and they believe Teensy isn't working before they don't see the first string their code prints.

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,764
    Ick - that was so nice when you undid that ... of course Serial.begin() is optional.

  13. #13
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    168
    Quote Originally Posted by PaulStoffregen View Post
    Even though the while (!Serial) doesn't explain the lockup where pressing the pushbutton was needed, I do believe it's time to reconsider the previously failed attempt to have Serial.begin() wait.

    I'm going to put this Serial.begin() delay we previous tried back into the code.

    https://github.com/PaulStoffregen/co...1cd983cc98403e

    Before it was 2.5 seconds, which was mostly motivated by the slowness of Windows 7's and weird delays USB drivers. It also just wanted 2.5 seconds in every case, even if no USB cable was connected. Looks like about 0.6 seconds is needed for normal USB enumeration to become apparent.

    I want to give this another try for 1.54-beta8 and we'll decide whether it causes too much pain before a 1.54 release. If we can have more programs written for Arduino Uno "just work" and not have to go to the much worse way Uno uses where opening the port hard reboots the main processor, even though it adds startup delay the improvement in usability might be worthwhile.

    Or maybe, like before, the extra startup delay will be too painful for too many people? I am listening and this might get pulled out yet again before 1.54 releases... but I do want to eventually find a way to avoid the confusion people experience when running programs written for Uno & Mega and they believe Teensy isn't working before they don't see the first string their code prints.
    A possible solution could be to enlarge Serial.begin() to Serial.begin(baudRate, timeOut or delay) where Serial.begin(baudRate) /Serial.begin() defaults to a timeout/delay of 2.5 seconds, or whatever is determined to be the better compromise.

    If Serial.begin(baudRate,0) is used then there is NO delay nor TimeOut.

    The discussion then becomes which is better to use "timeout until usb is up" or simply delay.
    I would suggest the latter as it would allow slow hardware to become alive and would be the better and simpler for the less knowledgeable migrating from UNO etc.

    If a timeout scenario is required it can still be implemented with
    Code:
    while (!Serial.begin(9600,0) && (millis()  <= 5000)){}  ;
    Last edited by BriComp; 02-28-2021 at 11:43 AM.

  14. #14
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,764
    As noted in post#3 by Paul - it works here - using T_4.1 and a known good cable.

    Not ever seen such a thing here, other than using the while(!Serial) in some fashion needed before the fist output will show - and there were times when too much output to Serial before it was online would act funny (but not like this) - that was in Beta IIRC.

    @JaredReabow - it isn't noted which version of TeensyDuino is in use? And what OS?

    Does the code for Serial4 and Serial making the tone work as expected - when USB works? Just fails to do the next Auto Upload?

    This really should work and normally does without odd 'Random' code being needed - so anyone trying to help to get to a solution will be puzzled as it seems to work on their system - so there is something missing that may be the cause of the trouble - so various questions to find that missing piece ...

    Windows only here. TD 1.53 had good SerMon - something odd ( at least with full speed output ) with TD 1.54 Beta 7 noted on that thread - may not apply here.


    @BriComp: the Serial.begin( ## ) is a void placeholder just for compatibility. When the sketch builds with Serial - it comes online by default when the computer connects without calling the .begin(). In which case the note timeout with while (!Serial) in some fashion will detect when the computer connects.

  15. #15
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,006
    The purpose here is to improve compatibility with code designed using Arduino Uno & Mega, even if we can't get to the bottom of what really went wrong with JaredReabow's system.

    I do not believe adding a Teensy-specific extension to the inputs of Serial.begin() helps serve that purpose. Since Serial.begin() doesn't actually do anything on Teensy, anyone who would use a Teensy-specific combination of inputs could just as well delete or comment out Serial.begin() from their program.

  16. #16
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    168
    Quote Originally Posted by PaulStoffregen View Post
    The purpose here is to improve compatibility with code designed using Arduino Uno & Mega, even if we can't get to the bottom of what really went wrong with JaredReabow's system.

    I do not believe adding a Teensy-specific extension to the inputs of Serial.begin() helps serve that purpose. Since Serial.begin() doesn't actually do anything on Teensy, anyone who would use a Teensy-specific combination of inputs could just as well delete or comment out Serial.begin() from their program.
    Your data on using Serial.begin on this page https://www.pjrc.com/teensy/td_serial.html states that:-

    "Serial.begin()
    Initialize the Serial object. The baud rate is ignored and communication always occurs at full USB speed."

    I can't find anywhere where it is documented as doing nothing on Teensy.

  17. #17
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,764
    Quote Originally Posted by BriComp View Post
    ...

    I can't find anywhere where it is documented as doing nothing on Teensy.
    It is in the installed sources - just an empty function ...

  18. #18
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,006
    Quote Originally Posted by BriComp View Post
    I can't find anywhere where it is documented as doing nothing on Teensy.
    Good point. The documentation on the USB serial page says: "Initialize the Serial object. The baud rate is ignored and communication always occurs at full USB speed."

    I've updated that page with better info.

Posting Permissions

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