Serial monitor not displaying

i used this code

int ir_pin = 2;
int counter = 0;
int detected = false;





void setup() {
  Serial.begin(9600);
  pinMode(ir_pin,INPUT);
}

void loop() {

      int val = digitalRead(ir_pin);

      if( (val == 0) && (detected = false )  ){
          counter++;
          detected = true;
         
          
          Serial.print("count =");
          Serial.println(counter);
      }
      else if( (val == 1) && (detected = true ) ){
        detected = false;
           
      }

}

and does not display anything. but when the first time I upload it, it works fine, but when the 2nd time I upload it( I unplugged the arduino to take a break) serial monitor do not display anything.

but when i try this code:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print("helloo");
}

it works fine, what seems to be the problem? please help me

If you disconnect the Arduino, the connection in the PC is lost. When you connect again, a new connection is created. But serial monitor does not know about that new connection.

Close serial monitor and open it again.

(deleted)