Exit(0) breaks my Micro board and turns it into a brick

Exit(0) breaks my Micro board and turns it into a brick. After I upload the code to the Micro code using IDE, Window pops a message box "the last USB device you connected to this computer malfunctionaled and window does not recongized it ...". I tried to update the driver, but it does not help.
image

I would like to use Micro SPI to talk to an antenna. I wrote some code, and put exit(0) at the end of the loop().
Ex:
void loop() {

digitalWrite(ucs_pin, HIGH);
digitalWrite(pcs_pin, HIGH);
digitalWrite(csd_pin, HIGH);

.....
//wait 10us btween
delayMicroseconds(10);
exit(0);

I broke three Micro boards before I figured the issue is the "exit(0)". However, the Due board has no issue at all.

Any idea?

what is the alternative way to stop the infinite loop() other than using exit(0).

Thanks!

Welcome to the forum

Why ?

What do you mean by "broke" ?

The Micro becomes unusable after I uploaded the code to the board. Window complains the device is malfunctionaled .

I think double pressing the reset button puts it into bootloader mode. Do you still get the same error if you try this?

OK, but why the exit(0) ?

Where did you think that the code would exit to ?

Yes I am still getting the same error message from window after I double pressing the reset button.

I am not sure why, but after remove the exit(0) from my code, it works fine.

Do everything in setup()

So they aren't bricked or you uploaded to a different one?

Yes, Due board works fine with the "exit(0)" code.

No program on any arduino board needs exit(0) instruction.

Do you have any peripherals, devices, or other wiring attached to the board?

You can ALWAYS exit from an infinite loop by having the code modify the variable to cause the exit. OR! change to a compound loop that includes a variable or Boolean in the test. When time to exit the loop, change the tested value of the variable or the Boolean and you are exited.

On AVR based boards, exit() ends the program, possibly in an endless loop and interrupts disabled. I do not know for the Due.

The problem is that part of the code that you have uploaded to a board with native USB handles the USB (board detection and reaction on software reset). And by using exit() that is no longer working.

See avr-libc exit()

This should not effect a double tap reset; it should still invoke the bootloader; you can check it in Windows device manager.

Note that the below two seem to contradict each other. You get an error after double tap reset but you can upload?

If you still have the problem,

  1. If your windows has a COM1, select that as the port.
  2. If your Windows does not have a COM1, hook up and other board that does not support native ISB (e.g. Nano, Uno, Mega or a dedicated TTL-to USB adapter) and select its port.
  3. Leave the board as Micro !!
  4. Upload blink or an empty sketch
  5. When the IDE reports ther memory usage, double tap the reset button.
  6. Upload should succeed.

@sunsun1437 - Glad to see the community was able to help resolve your problem.

Something that helped me while I was starting out was to get a “Ruggeduino”. It’s the same as an Arduino Uno, only it’s made by a different company.
It costs more, but the difference is you can accidentally fry an Arduino/your computers USB port if you’re not careful. “Ruggeduino” has resettable fuses for everything (even reverse polarity protection) It’s brick-proof - I’ve tried :wink: It really helps cutting down the learning curve.

exit() is a function that a program would use, on a desktop, to return to the operating system.

An Arduino does not have an operating system, so exit() doesn't do what you expect.
On an AVR like the Arduino Micro, it essentially completely stops the CPU, including the USB connection. The double-tap reset should work to get back to the bootloader, but you have to upload something else that DOESN'T exit() to restore normal behavior.

You might have wanted the C/C++ break statement, which immediately exits a loop, or perhaps return, which would stop the current function and continue from there.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.