I am currently dealing with some type of USB issue on my Nano 33 IoT . My PC does not recognize my Arduino when I plug it in, It was working earlier today and I don't understand why it would suddenly stop. It started when I was uploading some test code and I got this error.
"Couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."
I was confused as I had not changed the com port or unplugged anything. When I tried to change/fix the selected port it appeared grayed out. I was able to get the Arduino to recognize on my pc by putting it into bootloader mode ( double-clicking the reset button) ,when I do this the port selection becomes usable and code can be uploaded but when the serial monitior is used the arduino disconnects and windows dosent identify the arduino(all it does is make window able to identify it).
to summarize The arduino is not recognized by windows unless I double tap the reset button as discribed by this artical although I am using a nano not an Arduino Edge https://support.arduino.cc/hc/en-us/articles/360021532700-How-to-set-the-Edge-Control-to-bootloader-mode, as using the bootloader mode does not allow normal operations I need to be able to select a port when not in bootloader mode.(atleast I think thats the root of the problem)
The tricky thing about the boards with native USB capability like your Nano 33 IoT is that the USB code that creates the CDC serial port is running on the same microcontroller as your sketch. This means your sketch code can break the USB code, or stop it from running. When that happens, it no longer presents a port.
This can be unexpected to those who previously mainly worked with the boards like Uno and Mega with a dedicated USB chip that can never be affected by the sketch code.
As you discovered, the board can easily be recovered from this state by putting it in bootloader mode, but if you then upload the problematic sketch once again, you will get the same result.
So the next step is to study your code to find the cause. In some cases, that might even be the expected result. For example, it is normal and expected for the board to not have a port if your code puts the microcontroller to sleep to save power. In other cases, it might be a bug in the code that causes the problem.
The first thing I would do is to start to remove parts of the sketch, uploading each time to check whether the problem still occurs. When you reach the state where the problem no longer occurs, you will know that the last code you removed was the cause. That should allow you to produce a minimal demonstration sketch that will make the problem clear.
You can post the code here on the forum and it is likely the helpers will be able to explain the reason why it breaks the USB.
Please tell us where you got the libraries for these files:
MPU6050_tockn.h
DFRobot_BMP280.h
PID_v2.h
(I'm assuming Servo.h and SD.h are from the official Arduino libraries).
If you installed it using the Arduino Library Manager (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.
If you downloaded it from a website, then please post a link to that website.
You should not make these calls from outside of a function. I see that you are not even using these variables at all (you have local variables shadowing them). So you can simply delete the lines I quoted above to fix the bug.