uploading codes killed boot loader of arduino micro

after uploading following codes to arduino micro. arduino stop responding.
but after re burning boot loader it starts working normally.
I cant find any problem in my coding.
Ii tried to comment libraries one by one but problem persist
I also tried by removing all connections of external components

codes are too lengthy to fit here so I attached file

usman.ino (7.21 KB)

Please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded the libraries from. Or if you installed them using Library Manger (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.

The sketch:

#include <elapsedMillis.h>
elapsedMillis timeElapsed;
#include <ShiftRegister74HC595.h>
//------------------------------------------------------------------------------------------------------------------------------
int FPSensor = 12;    //filling bottle pick sensor
int CPSensor = 11;    //caping bottle pick sensor
int CapSensor = 12;    //Caps pick sensor
const int  Pump1Sensor = 10;    // Pump1 1 sensor
const int  Pump2Sensor = 9;
const int byepass1 = 4;
const int byepass2 = 5;
const int PinA = 0; // CLK encoder
const int PinB = 1; // DT encoder
const int up = 6;
const int down = 7;
const int start = 13;
const int Pump2 = A0;
const int conyaor = A1;
const int buzzer = A2;
// shift register
const int Fslide1 = 0; //on shift register
const int Fslide2 = 1; //on shift register
const int Cslide1 = 2; //on shift register
const int Cslide2 = 3; //on shift register
const int Vslide = 4; //on shift register
const int Cmotor = 5; //on shift register
const int Gslide = 6; //on shift register
const int Pump1 = 7; //on shift register
// Variables will change:
int counter = 0;   // counter for the number of button presses
int buttonState = 0;
int lastButtonState = 0;
float  volume = 0;
int flag = 0;
//0int timeElapsed = 0;
//int wait = 0;
int lastCount = 0;
int virtualPosition = 200;
//LCD
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified backpack
//shift register
// create shift register object (number of shift registers, data pin(SER), clock pin(SR), latch pin (R))
ShiftRegister74HC595 sr (1, A3, A5, A4);
void isr ()
{
  static unsigned long lastInterruptTime = 0;
  unsigned long interruptTime = millis();
  // If interrupts come faster than 5ms, assume it's a bounce and ignore
  if (interruptTime - lastInterruptTime > 5)
  {
    if (digitalRead(PinB) == LOW)
    {
      virtualPosition-- ; // Could be -5 or -10
    }
    else
    {
      virtualPosition++ ; // Could be +5 or +10
    }
    // Restrict value from 0 to +100
    // virtualPosition = min(1000, max(0, virtualPosition));
  }
  // Keep track of when we were here last (no more than every 5ms)
  lastInterruptTime = interruptTime;
}

void setup()
{
  lcd.begin (16, 2); // for 16 x 2 LCD module
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(FPSensor, INPUT_PULLUP);
  pinMode(CPSensor, INPUT_PULLUP);
  pinMode(Pump1Sensor, INPUT_PULLUP);
  pinMode(Pump2Sensor, INPUT_PULLUP);
  pinMode(CapSensor, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);
  pinMode(up, INPUT_PULLUP);
  pinMode(byepass2, INPUT_PULLUP);
  pinMode(byepass1, INPUT_PULLUP);
  pinMode(PinA, INPUT);  // Rotary pulses are INPUTs
  pinMode(PinB, INPUT);  // Rotary pulses are INPUTs
  pinMode(start, INPUT_PULLUP);
  pinMode(Pump2, OUTPUT);
  pinMode(conyaor, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(Fslide1, OUTPUT);
  pinMode(Fslide2, OUTPUT);
  pinMode(Cslide1, OUTPUT);
  pinMode(Cslide2, OUTPUT);
  pinMode(Vslide, OUTPUT);
  pinMode(Cmotor, OUTPUT);
  pinMode(Gslide, OUTPUT);
  pinMode(Pump1, OUTPUT);

  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);
  // Ready to go!
  Serial.println("Start");
}
void loop()
{
  sr.set(Pump1, HIGH);
  if (digitalRead(byepass1) == LOW)
  {
    sr.set(Pump1, LOW);
    volume = 0.0;
    counter = 0;
  }
  if (digitalRead(byepass2) == LOW)
  {
    digitalWrite(Pump2, LOW);
    volume = 0.0;
    counter = 0;
  }
  if (digitalRead(up) == LOW)
  {
    virtualPosition = virtualPosition + 50;
    while (digitalRead(up) == HIGH)
      //while (!digitalRead(up))
      delay(10);
  }

  if (digitalRead(down) == LOW)
  {
    virtualPosition = virtualPosition - 50;
    while (digitalRead(down) == HIGH)
      //while (!digitalRead(up))
      delay(10);
    if ((virtualPosition >= 950) &&  (virtualPosition <= 1000))
    {
      lcd.clear();
    }
  }
  virtualPosition = constrain(virtualPosition, 100, 2000);

  // If the current rotary switch position has changed then update everything
  if (virtualPosition != lastCount)
  {
    if ((virtualPosition >= 999) &&  (virtualPosition <= 1000))
    {
      lcd.clear();
    }
    // Write out to serial monitor the value and direction
    Serial.print(virtualPosition > lastCount ? "Up  :" : "Down:");
    Serial.println(virtualPosition);
    //-------------------------------------------------------------
    //LCD
    lcd.home (); // set cursor to 0,0
    lcd.print("Volume:");
    lcd.print(virtualPosition);
    lcd.setCursor (0, 1);       // go to start of 2nd line
    lastCount = virtualPosition ;
  }
  buttonState = digitalRead(Pump1Sensor );

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH)
    {
      // if the current state is HIGH then the button went from off to on:
      counter++;
      //      Serial.println("on");
      //      Serial.print("number of button pushes: ");
      //      Serial.println(counter);
    }
    else
    {
      // if the current state is LOW then the button went from on to off:
      //      Serial.println("off");
    }
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;
  }
  volume = (counter) * 1.5;

  if ((flag == 0) && (timeElapsed >= 1000))
  {
    lcd.setCursor (0, 1);       // go to start of 2nd line
    lcd.print("Start?");
    sr.set(Pump1, HIGH);
    digitalWrite(conyaor, HIGH);
    sr.set(Fslide1, LOW);
    sr.set(Fslide2, HIGH);
    flag = 1;
    timeElapsed = 0;
  }
  else if ((flag == 1) && (timeElapsed >= 100) && (digitalRead(start) == LOW))
  {
    lcd.clear();
    flag = 2;
    timeElapsed = 0;
  }
  else if (flag == 2)
  {
    digitalWrite(conyaor, LOW);
    sr.set(Fslide2, HIGH);
    sr.set(Fslide1, LOW);
    flag = 3;
    timeElapsed = 0;
  }
  else if ((digitalRead(FPSensor) == LOW) && (flag == 3) && (timeElapsed >= 10000))
  {
    digitalWrite(buzzer, HIGH);
    lcd.setCursor (0, 1);       // go to start of 2nd line
    lcd.print("Please Load Bottel");
    if (timeElapsed >= 1000)
    {
      digitalWrite(buzzer, LOW);
    }
  }
  else if ((digitalRead(FPSensor) == HIGH) && (flag == 3) && (timeElapsed >= 100))
  {
    sr.set(Fslide1, HIGH);
    sr.set(Pump1, HIGH);
    Serial.println ("Bottle Loaded");
    flag = 4;
    timeElapsed = 0;
  }
  else if ((volume  >= virtualPosition)  && (flag == 4))
  {
    sr.set(Pump1, HIGH);
    Serial.println ("filling completed");
    flag = 5;
    timeElapsed = 0;
  }
  else if ((volume  <= virtualPosition)  && (flag == 4) && (digitalRead(FPSensor) == LOW))
  {
    sr.set(Pump1, LOW);
  }
  else if ((flag == 5) && ( timeElapsed >= 10))
  {
    sr.set(Fslide2, LOW);
    flag = 6;
    timeElapsed = 0;
  }
  if ((flag == 6) && ( timeElapsed >= 100) && (digitalRead(FPSensor) == HIGH))
  {
    volume = 0.0;
    counter = 0;
    flag = 2;
    timeElapsed = 0;
    Serial.println ("volume zero");
  }
}

Did you upload your sketch using USB or using an ISP device (like you do with burning a bootloader). Uploading with an ISP programmer disables the bootloader and you have to burn a new bootloader if you want to upload with USB.

I found problem with assigning name to digital pins.

const int PinA = 0; // CLK encoder
const int PinB = 1; // DT encoder

const int Fslide1 = 0; //on shift register
const int Fslide2 = 1; //on shift register

these four lines confuse micro controller(I thinks so).
I am using Shift register because I run out of outputs on arduino micro and this library to handle shift register. GitHub - Simsso/ShiftRegister74HC595: Arduino library that simplifies the usage of shift registers

question how I could define pins on Shift register to prevent this problem

Those assignments shouldn’t confuse anything. If those are pin assignments, then 0/1 are not good choices as Serial also uses 0/1 for talking to your PC for code downloading and if using Serial.print() for debugging.

Better coding would be to use byte instead of int for values <=255.

You seem to use pins 0, 1 in several places to talk to the hardware:
const int PinA = 0; // CLK encoder
const int PinB = 1; // DT encoder
USB to the PC with Serial.begin(9600);
Perhaps to the LCD with LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
altho I2C on a Nano will come from A4, A5, so I don’t what you have going on there
To the shift register with ShiftRegister74HC595 sr (1, A3, A5, A4);

So it looks to me that you need to clean up your IO assignments.

CrossRoads:
If those are pin assignments, then 0/1 are not good choices as Serial also uses 0/1 for talking to your PC for code downloading and if using Serial.print() for debugging.

Pins 0 and 1 are Serial1 on the ATmega32U4-based Arduino Micro. Serial is from the native CDC.

changing Pins 0 ans 1 did not solve problem. it happen when i use same number of pins of arduino and shift register together

virtualPosition should be declared as volatile because it is used in an ISR. Also, except for inside the ISR itself, whenever you are accessing virtualPosition in your code you need to temporarily disable interrupts to prevent an interrupt from occurring during the access.

You declared constants for the pin mapping on the shift register. DO NOT use pinMode to set these to outputs, pinMode is only for arduino pins.

const int Fslide1 = 0; //on shift register
const int Fslide2 = 1; //on shift register
const int Cslide1 = 2; //on shift register
const int Cslide2 = 3; //on shift register
const int Vslide = 4; //on shift register
const int Cmotor = 5; //on shift register
const int Gslide = 6; //on shift register
const int Pump1 = 7; //on shift register

//in void setup()
  pinMode(Fslide1, OUTPUT);
  pinMode(Fslide2, OUTPUT);
  pinMode(Cslide1, OUTPUT);
  pinMode(Cslide2, OUTPUT);
  pinMode(Vslide, OUTPUT);
  pinMode(Cmotor, OUTPUT);
  pinMode(Gslide, OUTPUT);
  pinMode(Pump1, OUTPUT);

In an arduino with a native serial port, such as the Leonardo or Micro, normally you want to wait for the serial port to become available before using it, so the following will be in setup before printing anything to the serial port:

while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }

From testing on a Leonardo, it looks like your problem is caused by setting the interrupt pin as an output. You initially set it as an input, but then set it as output when you issued the erroneous pinMode commands for the pins of the shift register.

Instead of burning the bootloader again, you should be able to double tap the reset button immediately after the IDE shows the sketch size and is looking for the board on the USB port.

You declared constants for the pin mapping on the shift register. DO NOT use pinMode to set these to outputs, pinMode is only for arduino pins.

How to declare pins on shift register as outputs

Declaring pins on a shift register is unnecessary.

jalilurrehmaan9:
How to declare pins on shift register as outputs

You buy a serial-in/parallel-out shift register like a 74HC595. All of the pins are outputs.
If you want to 'declare' your shift register pins as inputs, buy a parallel-in/serial-out shift register like a CD4021 or 74HC165/166/597. All of the pins are inputs.
If you want a device where some of the pins can be inputs and some can be outputs, get an I2C Port Expander like a MCP23017 or PCF8575. To set the behavior of the pins you send commands to the I2C device to set bits in its internal registers. You can probably find a library to help.

thanks.
after removing these lines now every thing works fine.

pinMode(Fslide1, OUTPUT); // on shift register
pinMode(Fslide2, OUTPUT);// on shift register
pinMode(Cslide1, OUTPUT);// on shift register
pinMode(Cslide2, OUTPUT);// on shift register
pinMode(Vslide, OUTPUT);// on shift register
pinMode(Cmotor, OUTPUT);// on shift register
pinMode(Gslide, OUTPUT);// on shift register
pinMode(Pump1, OUTPUT);// on shift register

I am using elapsedMillis.h library to keep eye on time . using elapsedMillis function helped me in multitasking.
Problem is that I need another variable like elapsedMillis, which should be independent of elapsedMillis, may be like another timer.