Interrupt problem

Hello!!

i have problem with interrupt in my code because the interrupt works only if i use the serial.println(life). If i delete serial.println(life) in my code the interrupt seems not to work

Here is the code that i use:

#include <Servo.h> 


int servoDxPin = 9;
int servoSxPin = 10;
Servo servoDx;
Servo servoSx;
int sensorTrigger = 11;
int sensorInput = 12;
int led = 13;

long duration = 0;
long distance = 0;
long rnd = 0;

volatile int life = 10;

void setup(){
  pinMode(sensorInput,INPUT);
  pinMode(sensorTrigger,OUTPUT);
  servoDx.attach(servoDxPin); 
  servoSx.attach(servoSxPin); 
  randomSeed(analogRead(0)); 
  attachInterrupt(0, lifeReduction, RISING);
  
  pinMode(led,OUTPUT);
 // Serial.begin(9600);
}

void loop(){  
  digitalWrite(sensorTrigger, HIGH); // set HIGH for 15us to trigger ranging
  delayMicroseconds(15);
  digitalWrite(sensorTrigger, LOW);  // set pin LOW
  duration = pulseIn(sensorInput, HIGH);  // read in pulse length
  distance = microsecondsToCentimeters(duration);
  if (distance < 12){
    servoDx.write(90);
    servoSx.write(90); 
    rnd = random(10);
    if (rnd>=5){
      servoSx.write(180);
      servoDx.write(180);
      delay(1000);
      servoSx.write(90);   
      servoDx.write(90);
    }else{
      servoDx.write(0);
      servoSx.write(0);
      delay(1000);
      servoDx.write(90); 
      servoSx.write(90);  
    }
  }else{
     servoSx.write(180);
     servoDx.write(0);
  }
 Serial.println(life);         //<------------------------------------------------------------------------------
  if(life <= 0){
    digitalWrite(led,HIGH);
    life = 10;
  }
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

void lifeReduction(){
 life--;
}

How is it wired, can you provide a circuit diagram or schematic? Are you using pull down resistors?
Also if you are unaware, the zero in attachInterrupt(0, lifeReduction, RISING); does not mean pin 0, it is pin 2. And this will need a pull down resistor.

I have attached the photo of circuit diagram and arduino

So, your switch is attached with no pullup or pulldown resistor. Welcome to the frustration of floating pins. Time to wire up a resistor!

Just to expand on PaulS' comment, the issue is that while the switch is in an intermediate position the common pin is not connected to anything so it will be in an undefined state. Rather than connecting it directly to 5V and 0V, it would be better to use a pull-up resister to pull the input up to 5V by default, and then use the switch to pull it down to 0V as required. This means the input is always pulled high or low and never left floating.

The Arduino provides internal pull-up resisters for each digital input which can be enabled in software.

With a clunky mechanical switch like that it's quite likely that you will get contact bounce and may need to debounce the input. It also means that the switch is unlikely to change state at high frequency and so interrupts are neither required nor sensible to handle this type of input - it would be better to poll it.