I am using Arduino Micro and every time (tried at least 10 times) I upload a sketch the bootloader gets corrupted. The uploaded program appears to work but when I try to upload something again I get butterfly_recv() programmer is not responding error (pressing the restart button on Arduino does not help).
To eliminate the possibility of a hardware error I tried another Arduino Micro but got the same problem.
I also tried to insert an infinite loop at the end of the setup function and the bootloader didn't get corrupted. Therefore I think the bootloader gets corrupted while running the code.
Removing these 2 lines fixes the issue (but if I try to add more code the issue occurs again)
When compiling the code I get this warning:
Sketch uses 17938 bytes (62%) of program storage space. Maximum is 28672 bytes.
Global variables use 1935 bytes (75%) of dynamic memory, leaving 625 bytes for local variables. Maximum is 2560 bytes.
Low memory available, stability problems may occur.
So my question is: given the circumstance what can cause the corruption of the bootloader and how to avoid it?
The bootloader is stored in flash memory, I don't see how the code lines you pointed out could corrupt it.
On the other hand, I don't understand in detail how Pro Micro auto-resets when sketch upload is initiated, it's different to other non-native USB types of Arduino. Maybe you are running out of ram memory and this is somehow preventing the Pro Micro from auto-resetting.
You need to find ways to reduce RAM memory use, such as storing some of that data in flash memory using the F() macro and PROGMEM and so on.
That is a clear indication of to much RAM usage,
But there is something about the timer you spend inside an ISR, (or timer callback, is actually an ISR)
which disable interrupts.
Timer1.initialize(1000);
Sets timer 1 to fire every 1ms, that might be a bit much , and what you do in that timer ISR,
is seriously going to take to long. Displays are slow, and you will not just miss encoder ticks because of it.
You should make a short ISR, that sets flags, which control the execution of the conditional function calls
from the menuController class, from within the main flow of the program.
And the icons should be in PROGMEM. u8g2 library is known to suck up a lot of memory.
It is extremely unlikely that you corrupt the bootloader.
The code that you upload to 32U4 based boards (and probably all boards with native USB support) contains a part that handles the USB communication; and in there is the detection of a reset condition that is caused by opening and closing the serial port with a baudrate of 1200 baud which the IDE uses to reset your board before doing the upload.
It's that USB communication code that does not work properly because of programming mistakes in the sketch; very often it's caused by writing outside the bounds of an array in the sketch which causes corruption of variables used by the USB communication code.
If you can upload a simple sketch ( blink ?) and then repeat it then your issue is with your sketch .
I assume you are uploading by clicking the upload arrow and not selecting upload using programmer ( which over writes the bootloader ).