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

Thread: Loosing my com port connection with Teensy 4.0

  1. #1

    Loosing my com port connection with Teensy 4.0

    I am puzzled why uploading the code below onto my Teensy 4.0 makes it lose its connection (com port) with my computer. It is a modified SamplePlayer from the PJRC Audio Library.

    I am trying to make a simple drum project with 30 triggers (without MIDI), but unable to get past 7 triggers with a number of code rewrites.

    I am using Windows 8.1 and a Teensy Audio Shield. Everything is hooked up property because a similar code (with fewer inputs) runs fine and I get output from my headphones connected to the Audio Shield.

    I have added a flash chip to the Audio Board for extra memory, because my sounds (cpp files) are large.

    I do not get any compilation errors with the code.

    Before I run the code, I have erased all flash memory (erase everything) and verified that the Teensy is indeed connected to the computer and running the blink program. The port says “hid#vid_16c0&pid_0486 RawHID (Teensy 4.0)”

    When I upload the code, I get a message that the “Sketch uses 512128 bytes (25%) of program storage space. Maximum is 2031616 bytes.
    Global variables use 522176 bytes (49%) of dynamic memory, leaving 526400 bytes for local variables. Maximum is 1048576 bytes.”

    Perhaps the Teensy (even with extra flash memory on the Audio Board) is not enough to run my code?

    Any ideas?

    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    // WAV files converted to code by wav2sketch
    #include "AudioSampleC_drum.h"  //  AudioSampleC1_drum (C4)
    #include "AudioSampleD_drum.h"  //  AudioSampleD_drum (D4)
    #include "AudioSampleE_drum.h"  //  AudioSampleE_drum (E4)
    #include "AudioSampleF_drum.h"  //  AudioSampleF_drum (F4)
    #include "AudioSampleG_drum.h"  //  AudioSampleG_drum (G4)
    #include "AudioSampleA_drum.h"  //  AudioSampleA_drum (A4)
    #include "AudioSampleB_drum.h"  //  AudioSampleB_drum (B4)
    #include "AudioSampleC1_drum.h"  //  AudioSampleC2_drum (C5)
    //#include "AudioSampleD1_drum.h"  //  AudioSampleD1_drum (D5)
    // GUItool: begin automatically generated code
    AudioPlayMemory          playMem2;       //xy=62,73
    AudioPlayMemory          playMem1;       //xy=63,34
    AudioPlayMemory          playMem4;       //xy=71,160
    AudioPlayMemory          playMem3;       //xy=72,117
    AudioPlayMemory          playMem5;       //xy=74,215
    AudioPlayMemory          playMem6;       //xy=77,257
    //AudioPlayMemory          playMem9;       //xy=97,400
    AudioPlayMemory          playMem7;       //xy=103,302
    AudioPlayMemory          playMem8;       //xy=108,342
    AudioMixer4              mixer1;         //xy=249,47
    AudioMixer4              mixer3;         //xy=295,259
    AudioMixer4              mixer4;         //xy=301,353
    AudioMixer4              mixer2;         //xy=462,162
    AudioOutputI2S     headphones;
    AudioOutputAnalog  dac;     // play to both I2S audio board and on-chip DAC
    AudioConnection          patchCord1(playMem2, 0, mixer1, 1);
    AudioConnection          patchCord2(playMem1, 0, mixer1, 0);
    AudioConnection          patchCord3(playMem4, 0, mixer1, 3);
    AudioConnection          patchCord4(playMem3, 0, mixer1, 2);
    AudioConnection          patchCord5(playMem5, 0, mixer3, 0);
    AudioConnection          patchCord6(playMem6, 0, mixer3, 1);
    //AudioConnection          patchCord7(playMem9, 0, mixer4, 0);
    AudioConnection          patchCord8(playMem7, 0, mixer3, 2);
    AudioConnection          patchCord9(playMem8, 0, mixer3, 3);
    AudioConnection          patchCord10(mixer1, 0, mixer2, 0);
    AudioConnection          patchCord11(mixer3, 0, mixer2, 1);
    AudioConnection          patchCord12(mixer4, 0, mixer2, 2);
    AudioConnection          patchCord13(mixer2, 0, headphones, 0);
    AudioConnection          patchCord14(mixer2, 0, headphones, 1);
    AudioConnection          patchCord15(mixer2, 0, dac, 0);
    AudioControlSGTL5000 audioShield;
    // GUItool: end automatically generated code
    // Bounce objects to read six pushbuttons (pins 0-5)
    Bounce button0 = Bounce(0, 5);
    Bounce button1 = Bounce(1, 5);  // 5 ms debounce time
    Bounce button2 = Bounce(2, 5);
    Bounce button3 = Bounce(3, 5);
    Bounce button4 = Bounce(4, 5);
    Bounce button5 = Bounce(5, 5);
    Bounce button9 = Bounce (9, 5); // added this button 080919
    Bounce button14 = Bounce (14, 5); // added this button 080919
    //Bounce button16 = Bounce (16, 5); // added this button 080919
    void setup() {
      // Configure the pushbutton pins for pullups.
      // Each button should connect from the pin to GND.
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);
      pinMode(9, INPUT_PULLUP); // added this pin 080919
      pinMode(14, INPUT_PULLUP); // added this pin 080919
      //pinMode(16, INPUT_PULLUP); // added this pin 080919
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      // turn on the output
      // by default the Teensy 3.1 DAC uses 3.3Vp-p output
      // if your 3.3V power has noise, switching to the
      // internal 1.2V reference can give you a clean signal
      // reduce the gain on mixer channels, so more than 1
      // sound can play simultaneously without clipping
      mixer1.gain(0, 0.4);
      mixer1.gain(1, 0.4);
      mixer1.gain(2, 0.4);
      mixer1.gain(3, 0.4);
      mixer2.gain(1, 0.4);
      mixer2.gain(2, 0.4);
      mixer3.gain(2, 0.4); //
      mixer3.gain(2, 0.4); // 
    void loop() {
      // Update all the button objects
      button9.update();  // 
      button14.update();  // 
     // button16.update();  //
      // When the buttons are pressed, just start a sound playing.
      // The audio library will play each sound through the mixers
      // so any combination can play simultaneously.
      if (button0.fallingEdge()) {;
      if (button1.fallingEdge()) {;
      if (button2.fallingEdge()) {;
      if (button3.fallingEdge()) {;
      if (button4.fallingEdge()) {;
      if (button5.fallingEdge()) {;
      if (button9.fallingEdge()) {;
     if (button14.fallingEdge()) {;
     //if (button16.fallingEdge()) {
        //I have commented to take all of the gong stuff out of the sketch
        // comment this line to work with Teensy 3.0.
        // the Gong sound is very long, too much for 3.0's memory
        // }
        // if (button5.fallingEdge()) {

  2. #2
    Senior Member
    Join Date
    Nov 2015
    Cold hollow VT
    I may have had a similar issue with a previous project where the serial monitor unexpectedly closed and Arduino programmer complained about not being able to set the serial port parameters in the error output window.. Forgive me for being vague, I've been doing yard sale all day and it was a work project. Anyway I was using several large arrays which were too small for the number of items that I was trying to store. Once I dimmensioned them to a proper size, the problem went away.

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Guessing - looks like the 'samples' are going to RAM?

    The T4 RAM is 1MB total but in two banks of 512KB. One bank is used for CODE and STACK when the code isn't marked as PROGMEM.

    This:: Global variables use 522176 bytes (49%)

    Is really close to the 512KB space and on startup stack and other use may be pushing into the 512KB limit.

    Please post the alloc lines for :: #include "AudioSampleC_drum.h"

    They need to be declared PROGMEM to reside on FLASH and not be pulled to RAM AFAIK, there was another user post about this 'recently'. ... your thread ::

    To use the second 512KB at runtime malloc() will alloc from that area, or it can be defined as DMAMEM.

  4. #4
    Defragster, Thanks again for stepping in to help. I am working to reprogram with PROGMEM. It compiles without errors, but I don't have any sound, so my programming needs more work.

    I think you are asking for me to post the "alloc lines for :: #include "AudioSampleC_drum.h"

    I assume you want to see the size of the file, but not sure. Here is what I think you are asking for. Sorry if I'm wrong.

    const unsigned int AudioSampleC_drum[13825] = {

    The largest file is const unsigned int AudioSampleC1_drum[26625] = {

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    @ofishal - based on those lines in post #4 above … the answer to that sits in post #2 on your original thread :

    With that change those samples should reside on FLASH and RAM would not have been exhausted and it would not have Crashed.

    So this is very related it seems - and would have been more convenient keeping it on the same thread to preserve context.

    Please give that thread's p#2 a try and see if RAM usage drops and this problem goes away

  6. #6
    Excellent analysis Defragster, thank you. I will continue to work on PROGMEM as a solution.

Posting Permissions

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