For significant projects I always add code to print at least its filename for future identification. For example, if I've left a loaded Uno aside ages ago and now want to work on it again, I'm almost certain I could start by finding its name. But darned if I can remember how I did it!
The steps I've just taken:
Plugged a PC USB cable into the board containing the unknown sketch.
Ran IDE 1.8.19.
(Or run the IDE first; makes no difference.)
The last sketch I was working on was loaded, GardenLamp-3.ino. I obviously ignore it.
Checked Board and Port were set correctly for the unknown sketch. The port needed changing.
This where I'm hazy, but..
Opened the Serial Monitor, usually with Ctrl + Shift + M.
That's when I expected the old sketch to run, printing the name during setup(). Nor after pressing the Reset button.
There's something else going on: uploading is not proceeding correctly. I loaded a trivial (1488 byte)s sketch. It verified. But 'Uploading' remained for a minute or two, and then the error report appeared, dominated by avrdude: stk500_recv(): programmer is not responding
Same with Blink and other sketches.
Re-starting IDE didn't help. No clues in Device Manager.
Fixed the uploading issue after a PC reboot. That was clearly the cause. My filename procedure now works as it should.
EDIT 5 mins later: Premature - it's not solved!
It worked on first test. But then tried another old board, with the blink sketch as the unavoidable 'base' sketch also open. Same 'programmer is not responding' error with that, and with all subsequent sketches. Including the one that ran correctly on the first test. It seems that the procedure itself is somehow introducing a port problem which persists until a PC reboot.
Opened the serial monitor. Message 'Board at COM5 not available'.
With Tools > Port chose COM9. (Question: does port suggested always depend on the sketch and board, not the physical port actually in use?)
Opened COM9. Nothing printed. No Tx light flashing.
Closed TestSketch. Unplugged the 'unknown' Uno.
Renamed TestSketch as TestSketch2. Identical otherwise, and I will use this as the 'unknown', confident that it should print its name if the procedure works
Loaded Blink. Closed TestSketch2, so only Blink now loaded.
9.. Plugged in TestSketch2 again. Shown as COM9.
Opened monitor, which correctly showed its name.
Unplugged it and plugged in the previous reluctant sketch. Tools > Port listed COM5 and COM9, with the latter marked as the correct one.
But as you see from the screenshot COM9 is now unavailable.
So not surprisingly the opened monitor now does not report anything.
I'm wondering whether the arbitrary 'base' sketch must always be associated with the same port as the unknown 'tested' sketch? If so, a Catch 22 sitiuation?
Many thanks, that made a big improvement in my success rate. However there seems to be one other important factor that had contributed. Best explained by summarising another test example.
In the last hour or two I did several tests on different boards, some with regular sockets and some with mini-USB, so two different cables are currently connected to the PC. Steps for a fresh test were then as follows:
Closed & restarted IDE 1.8.19
The Blink example was loaded automatically as expected.
With nothing plugged in Tools>Port shows just COM1 as expected.
Plugged in Uno #1, holding a sketch whose filename I do know and which does contain code to print it,, but which we can regard as the 'unknown' that I'm trying to identify. Tools>Port now also shows 'COM5 (Arduino Uno)' as expected,
duly selected.
Opened serial monitor. Message shows correct name TestSketch2. So far so good.
Closed monitor, unplugged Uno #1, plugged in Uno #2. Tools>Port shows COM1 & 'COM9 (Aruino Uno).' Nothing marked on COM9.
Opened serial monitor. Message "Board at COM5 is not available", despite that not being offered.
Opened serial monitor anyway which gave same predictable message "Board at COM5 is not available".
Unplugged Uno #2, closed monitor. No point repeating, so took the action that I believe is crucial: closed & restarted IDE. Tools>Port has now purged the irrelevant stuff and just shows COM1.
Plugged in Uno #2. Tools>Port reassuringly shows COM1 & 'COM9 (Arduino Uno)', unmarked so I selected it.
Opened monitor. No error messages. In fact no message at all, so I clearly did not code the filename into that particular sketch; one of the possibilities mentioned by @Delta_G in post #2.
I'll wait a while for any feedback but this time I do feel reasonably confident of this solution.
AFAIK, the serial monitor and/or “upload” will not automatically switch ports just because you have plugged in a different arduino, even if the old arduino is “gone.”
It’s really ambiguous what it “ought” to do, especially in the presence of “native usb” boards whose serial ports disappear and reappear with every reboot.