I'm testing out a Nano 33 BLE Sense for possible use in a computer organization and assembly language class that I teach (transitioning from an NXP LPC1768 on MBED), and after looking through the many posts about issues with the Serial monitor, I thought I would post what I have come up with. I'm running Arduino 1.8.15 on MacOS 11.4 (Big Sur) and testing on both Intel and M1 machines. As has been noted, the native USB in the Nano Sense causes some inconsistent behavior for communicating with the serial monitor, and various posts have already mentioned how upload can cause the port to change on Windows, with a variety of workarounds. This is for MacOS, and may also be relevant to Linux.
If I run the following sketch, and open the serial monitor while compiling:
void setup() {
Serial.begin(9600);
Serial.println("This is a test.");
}
void loop() { }
It frequently, but not always, will print the message. But pressing the reset button on the board usually doesn't cause the message to be redisplayed (it has done it a few times, but never after a second reset). However, with the following adjustment:
void setup() {
delay(1000);
Serial.begin(9600);
delay(1000);
Serial.println("This is a test.");
}
void loop() { }
It consistently shows the message after uploading, and on each press of the reset button (if it is held for a second or so). I was particularly interested in being able to get consistent behavior from setup() because several of my assembly assignments involve C++ calling an assembly routine (with parameter passing), displaying some memory before and after the call, to show that it has worked correctly, and I don't want that to run in the loop() section.
But I also ran some experiments with loop(). The following sketch runs reliably on the Intel machine, displaying both output lines repeatedly after upload and pressing reset:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("This is a first test.");
Serial.println("This is a second test.");
delay(1000);
}
But on the M1 machine, I had a situation where it would skip the first line on the first iteration. Inserting a delay after the call to begin fixed that, but also unplugging the USB cable and reconnecting seemed to solve it.
I also tried the approach of following the code in the loop() section with a while(1); to get the singe-execution effect. But that caused no output on upload or after resets, which I'm guessing is because the code that repeatedly calls loop is doing some additional management or buffer flushing that gets prevented when loop doesn't return.
At this point, I'm satisfied that I have a fix I can share with students. I also have a sketch successfully calling a routine in a .S file (after sorting out differences between the Arm and gcc assembler directives, to be able to access the full M4 instruction set). Next on my list is working out the locations and values for the GPIO ports, so I can show the students how to write a low level driver for the RGB LED. I have the Nordic documentation and the schematics for the BLE Sense, so hopefully it won't be too much work to get that going.