Port not found after upload

Hello,

I'm working with an Arduino Nano 33 BLE Sense on a Linux machine, running ElementaryOS (an Ubuntu derivative). It's a Dell Latitude E5470 in case that matters. What's happening is I upload the following sketch with a library I wrote, and after finishing uploading it crashes and I need to reset it with the button the board to get the port to show up again. I know the problem is with this sketch because I can upload others. It does compile when I press verify, though.

Here's the header file for the library, TinyTrainable.h:

#ifndef TinyTrainable_h
#define TinyTrainable_h

#include "Arduino.h"

// include Arduino KNN library
#include <Arduino_KNN.h>

// TODO fix the input things and classes (3)

enum OutputMode {usb, midi, pin};

class TinyTrainable
{
  public:
    TinyTrainable(bool serialDebugging);
    TinyTrainable(bool serialDebugging, byte midiChannelHex, byte midiVelocity, int midiNote1, int midiNote2, int midiNote3);
    TinyTrainable(bool serialDebugging, int outputPin, long noteDuration, int noteFreq1, int noteFreq2, int noteFreq3);

    template <typename T> void debugPrint(T message) {
      if (_serialDebugging) {
        Serial.println(message);
      }
    };
    void setLabels(String object1, String object2, String object3);
    void trainKNN(int k, int examplesPerClass, float colorThreshold);
    void identify();
  private:
    void setupInstrument();
    void setupSerial1();
    void readColor(float color[]);
    void setColorBuiltInLED(int color);
    void midiCommand(byte cmd, byte data1, byte data2);

    byte _midiChannelHex;
    byte _midiVelocity;
    int _outputPin;
    long _noteDuration;
    int _notes[3];

    bool _serialDebugging;
    OutputMode _outputMode;
    KNNClassifier _myKNN;
    float _colorReading[3];
    int _k;
    String _label[3];
    int _previousClassification;
    float _colorThreshold;
};

#endif

And the cpp file for the library, TinyTrainable.cpp:

#include "Arduino.h"
#include "TinyTrainable.h"

// include Arduino KNN library
#include <Arduino_KNN.h>

// include Arduino library for proximity sensor
// more information at
// https://www.arduino.cc/en/Reference/ArduinoAPDS9960
#include <Arduino_APDS9960.h>

TinyTrainable::TinyTrainable(bool serialDebugging) : _myKNN(3)
{
  _serialDebugging = serialDebugging;
  _outputMode = usb;
  setupInstrument();
}

TinyTrainable::TinyTrainable(bool serialDebugging, byte midiChannelHex, byte midiVelocity, int midiNote1, int midiNote2, int midiNote3)  : _myKNN(3)
{
  _serialDebugging = serialDebugging;
  _outputMode = midi;
  _midiChannelHex = midiChannelHex;
  _midiVelocity = midiVelocity;
  _notes[0] = midiNote1;
  _notes[1] = midiNote2;
  _notes[2] = midiNote3;
  setupInstrument();
  setupSerial1();
}

TinyTrainable::TinyTrainable(bool serialDebugging, int outputPin, long noteDuration, int noteFreq1, int noteFreq2, int noteFreq3)  : _myKNN(3)
{
  _serialDebugging = serialDebugging;
  _outputMode = pin;
  _outputPin = outputPin;
  _noteDuration = noteDuration;
  _notes[0] = noteFreq1;
  _notes[1] = noteFreq2;
  _notes[2] = noteFreq3;
  setupInstrument();
}

void TinyTrainable::setupInstrument() {
  if (_serialDebugging || _outputMode == usb) {
    Serial.begin(9600);
    while (!Serial);
  }

  Serial.println("got to setupInstrument");

  if (!APDS.begin()) {
    while (1);
  }

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);

  // start with everything off
  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);

  _previousClassification = -1;
}

void TinyTrainable::setupSerial1() {
  Serial1.begin(9600);

  // desired baudrate
  uint32_t baudrate = 0x800000;

  // pointer to the memory address that stores the baudrate
  uint32_t *pointerBaudrate = ( uint32_t * )0x40002524;

  // replace the value at the pointer with the desired baudrate
  *pointerBaudrate = baudrate;
}

void TinyTrainable::setLabels(String object1, String object2, String object3) {
  _label[0] = object1;
  _label[1] = object2;
  _label[2] = object3;
}

void TinyTrainable::trainKNN(int k, int examplesPerClass, float colorThreshold) {
  _k = k;
  _colorThreshold = colorThreshold;

  for (int currentClass = 0; currentClass < 3; currentClass++) {

    setColorBuiltInLED(currentClass);

    // Ask user to show examples of each object
    for (int currentExample = 0; currentExample < examplesPerClass; currentExample++) {

      debugPrint("Show me an example of:");
      debugPrint(_label[currentClass]);

      // Wait for an object then read its color
      readColor(_colorReading);

      // Add example color to the k-NN model
      _myKNN.addExample(_colorReading, currentClass);
    }
    // Wait for the object to move away again
    while (!APDS.proximityAvailable() || APDS.readProximity() == 0) {}
  }
}

void TinyTrainable::setColorBuiltInLED(int color) {
  // turn everything off
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);

  if (color == 0) {
    digitalWrite(LEDR, LOW);
  } else if (color == 1) {
    digitalWrite(LEDG, LOW);
  } else if (color == 2) {
    digitalWrite(LEDB, LOW);
  }
}

void TinyTrainable::identify() {
  // Wait for the object to move away again
  while (!APDS.proximityAvailable() || APDS.readProximity() == 0) {}

  debugPrint("Let me guess your object");

  // Wait for an object then read its color
  readColor(_colorReading);

  // Classify the object
  int classification = _myKNN.classify(_colorReading, _k);

  debugPrint("You showed me:");
  debugPrint(_label[classification]);

  setColorBuiltInLED(classification);

  if (classification != _previousClassification) {
    switch (_outputMode) {
      case usb:
        debugPrint(classification);
        break;
      case midi:
        midiCommand(_midiChannelHex, _notes[classification], _midiVelocity);
        break;
      case pin:
        tone(_outputPin, _notes[classification], _noteDuration);
        break;
    }

    _previousClassification = classification;
  }
}

void TinyTrainable::readColor(float colorReading[]) {
  int red, green, blue, proximity, colorTotal = 0;

  // Wait for the object to move close
  while (!APDS.proximityAvailable() || APDS.readProximity() > 0) {}

  // Wait until we have a color bright enough
  while (colorTotal < _colorThreshold) {
    // Sample if color is available and object is close
    if (APDS.colorAvailable()) {

      // Read color and proximity
      APDS.readColor(red, green, blue);
      colorTotal = (red + green + blue);

      _colorReading[0] = red;
      _colorReading[1] = green;
      _colorReading[2] = blue;

      debugPrint(colorTotal);
      debugPrint(_colorReading[0]);
      debugPrint(", ");
      debugPrint(_colorReading[1]);
      debugPrint(", ");
      debugPrint(_colorReading[2]);
      debugPrint("\n");
    }
  }
}

// send 3 byte midi message
void TinyTrainable::midiCommand(byte cmd, byte data1, byte data2) {
  Serial1.write(cmd);
  Serial1.write(data1);
  Serial1.write(data2);
}

The ino file, TinyTrainable.ino:

#include "Arduino.h"
#include "TinyTrainable.h"

TinyTrainable myInst(true);
// TinyTrainable myInst(true, 0x99, 127, 38, 39, 42);
// TinyTrainable myInst(true, 9, 100, 250, 450);

void setup() {
  Serial.println("got to setup");
  myInst.debugPrint("Arduino k-NN color classifier");
  myInst.setLabels("Object 1", "Object 2", "Object 3");
  myInst.trainKNN(5, 10, 0.5);  // TODO make these constants at the top
}

void loop() {
  myInst.identify();
}

I'll reply to this with the error - I'm hitting the word limit.

Here's the error, after checking "Show verbose output during upload":

Sketch uses 87724 bytes (8%) of program storage space. Maximum is 983040 bytes.
Global variables use 43792 bytes (16%) of dynamic memory, leaving 218352 bytes for local variables. Maximum is 262144 bytes.
Forcing reset using 1200bps open/close on port /dev/ttyACM0
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
PORTS {/dev/ttyACM0, } / {/dev/ttyACM0, } => {}
Uploading using selected port: /dev/ttyACM0
/home/ptone/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino1/bossac -d --port=ttyACM0 -U -i -e -w /tmp/arduino_build_611618/TinyTrainable.ino.bin -R 
Set binary mode
version()=Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Connected at 921600 baud
identifyChip()=nRF52840-QIAA
write(addr=0,size=0x34)
writeWord(addr=0x30,value=0x400)
writeWord(addr=0x20,value=0)
version()=Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Device       : nRF52840-QIAA
Version      : Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Address      : 0x0
Pages        : 256
Page Size    : 4096 bytes
Total Size   : 1024KB
Planes       : 1
Lock Regions : 0
Locked       : none
Security     : false
Erase flash
chipErase(addr=0)

Done in 0.001 seconds
Write 87732 bytes to flash (22 pages)
[                              ] 0% (0/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0, size=0x1000)
[=                             ] 4% (1/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x1000, size=0x1000)
[==                            ] 9% (2/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x2000, size=0x1000)
[====                          ] 13% (3/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x3000, size=0x1000)
[=====                         ] 18% (4/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x4000, size=0x1000)
[======                        ] 22% (5/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x5000, size=0x1000)
[========                      ] 27% (6/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x6000, size=0x1000)
[=========                     ] 31% (7/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x7000, size=0x1000)
[==========                    ] 36% (8/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x8000, size=0x1000)
[============                  ] 40% (9/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x9000, size=0x1000)
[=============                 ] 45% (10/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xa000, size=0x1000)
[===============               ] 50% (11/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xb000, size=0x1000)
[================              ] 54% (12/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xc000, size=0x1000)
[=================             ] 59% (13/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xd000, size=0x1000)
[===================           ] 63% (14/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xe000, size=0x1000)
[====================          ] 68% (15/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0xf000, size=0x1000)
[=====================         ] 72% (16/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x10000, size=0x1000)
[=======================       ] 77% (17/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x11000, size=0x1000)
[========================      ] 81% (18/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x12000, size=0x1000)
[=========================     ] 86% (19/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x13000, size=0x1000)
[===========================   ] 90% (20/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x14000, size=0x1000)
[============================  ] 95% (21/22 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x15000, size=0x1000)
[==============================] 100% (22/22 pages)
Done in 3.428 seconds
reset()
processing.app.SerialException: Error opening serial port '/dev/ttyACM0'.
	at processing.app.Serial.<init>(Serial.java:152)
	at processing.app.Serial.<init>(Serial.java:82)
	at processing.app.SerialMonitor$2.<init>(SerialMonitor.java:132)
	at processing.app.SerialMonitor.open(SerialMonitor.java:132)
	at processing.app.AbstractMonitor.resume(AbstractMonitor.java:132)
	at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2120)
	at processing.app.Editor.access$1300(Editor.java:117)
	at processing.app.Editor$UploadHandler.run(Editor.java:2089)
	at java.lang.Thread.run(Thread.java:748)
Caused by: jssc.SerialPortException: Port name - /dev/ttyACM0; Method name - openPort(); Exception type - Port not found.
	at jssc.SerialPort.openPort(SerialPort.java:167)
	at processing.app.Serial.<init>(Serial.java:141)
	... 8 more
Error opening serial port '/dev/ttyACM0'.

Any help is appreciated, thank you in advance!

Look at Tools->Port make sure the serial port is checked

Open a terminal and type $ dmesg |grep "usb"|tail -30 That's the PIPE symbol "|", not letter L.
Look for errors.

noweare:
Look at Tools->Port make sure the serial port is checked

It is checked when I press Upload, but as soon as it hits "reset()" after saying "Done in 3.428 seconds" (this is in the error message above) the port disappears.

JCA34F:
Open a terminal and type $ dmesg |grep "usb"|tail -30 That's the PIPE symbol "|", not letter L.
Look for errors.

No errors appear, it just shows up as "USB disconnect" when the port disappears. Here's the output:

$ dmesg | grep "usb" | tail -30
[55545.882748] usb 1-3: SerialNumber: A92024AD020C4931
[55590.268587] usb 1-3: USB disconnect, device number 26
[55590.581215] usb 1-3: new full-speed USB device number 27 using xhci_hcd
[55590.732021] usb 1-3: New USB device found, idVendor=2341, idProduct=005a, bcdDevice= 0.11
[55590.732026] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[55590.732030] usb 1-3: Product: Arduino Nano 33 BLE
[55590.732032] usb 1-3: Manufacturer: Arduino
[55590.732035] usb 1-3: SerialNumber: 0000000000000000A92024AD020C4931
[55595.127446] usb 1-3: USB disconnect, device number 27
[55595.437216] usb 1-3: new full-speed USB device number 28 using xhci_hcd
[55595.588178] usb 1-3: New USB device found, idVendor=2341, idProduct=005a, bcdDevice= 0.11
[55595.588180] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[55595.588181] usb 1-3: Product: Arduino Nano 33 BLE
[55595.588182] usb 1-3: Manufacturer: Arduino
[55595.588183] usb 1-3: SerialNumber: 0000000000000000A92024AD020C4931
[55595.637616] usb 1-3: USB disconnect, device number 28
[55775.847731] usb 1-3: new full-speed USB device number 29 using xhci_hcd
[55775.998486] usb 1-3: New USB device found, idVendor=2341, idProduct=005a, bcdDevice= 0.11
[55775.998488] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[55775.998489] usb 1-3: Product: Arduino Nano 33 BLE
[55775.998490] usb 1-3: Manufacturer: Arduino
[55775.998491] usb 1-3: SerialNumber: 0000000000000000A92024AD020C4931
[55780.332914] usb 1-3: USB disconnect, device number 29
[55780.639808] usb 1-3: new full-speed USB device number 30 using xhci_hcd
[55780.790579] usb 1-3: New USB device found, idVendor=2341, idProduct=005a, bcdDevice= 0.11
[55780.790581] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[55780.790582] usb 1-3: Product: Arduino Nano 33 BLE
[55780.790583] usb 1-3: Manufacturer: Arduino
[55780.790584] usb 1-3: SerialNumber: 0000000000000000A92024AD020C4931
[55780.843087] usb 1-3: USB disconnect, device number 30

It looks like, with this particular sketch, it's resetting but not connecting back to the computer.