A little problem with opto sensor please help!

Hi all. Im sergio from italy. I just try to write a post in italian section but no luck until now.
Then I have a 24 volt motor take from a coin hopper , a relay module ( from arduino starter kit ) and a opto sensor module.
Then I have a software in my computer called mamehooker that "sniff" outputs from mame and send via serial to arduino.
I'm able to start relay so turn on my motor when a signal come from serial , but i can't manage this kind of input to do a "countdown" with coins that pass close the sensor. In actual situation , when i send some input, the relay go on but when the first coin pass on the sensor , the relay go off.
I just try some debug codes, for count the input , and it work , another for count the sensor input , and it work too . Please if someone know how i can solve this issue , just say thanks.
I share here the two debug codes i used , and the actual code that work in part.

This is the code i actual use

#define TIME_OUT 500UL
 
int value = 0;
volatile int credito = 0;
unsigned long myMillis = 0;
bool riscossione = false;
volatile bool ritardo = false;
volatile bool ledONOFF = HIGH;
volatile bool attivaISR = false;
 
void setup() {
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(2, INPUT);
  Serial.begin(9600);
}
 
void loop() {
  while (Serial.available() > 0) {
    value = Serial.parseInt();
    if (Serial.read() == 'x') {
      riscossione = true;
      myMillis = millis();
      if (value > 0) {
        credito ++;
      }
    }
  }
 
  if (riscossione && (millis() - myMillis > TIME_OUT)) {
    riscossione = false;
    attivaISR = true;
    attachInterrupt(digitalPinToInterrupt(2), erogazione, FALLING);
    digitalWrite(3, HIGH);
  }
  if (ritardo) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(20);
    digitalWrite(3, LOW);
    ritardo = false;
    detachInterrupt (digitalPinToInterrupt(2));
  }
}
 
void erogazione() {
  if (attivaISR) {
    credito--;
    digitalWrite(LED_BUILTIN, ledONOFF);
    ledONOFF = !ledONOFF;
    if (credito <= 0) {
      ritardo = true;
      attivaISR = false;
      credito = 0;
    }
  }
}

this is the debug for check if the opto sensor work

int LED = 13; // Use the onboard Uno LED
int isObstaclePin = 2;  // This is our input pin
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(isObstaclePin, INPUT);
  Serial.begin(9600);  
}

void loop() {
  isObstacle = digitalRead(isObstaclePin);
  if (isObstacle == LOW)
  {
    Serial.println("OBSTACLE!!, OBSTACLE!!");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.println("clear");
    digitalWrite(LED, LOW);
  }
  delay(200);
}

and this is the debug for serial inputs

#define TIME_OUT 500UL
int pin = 1;
int value = 0;
volatile int credito = 0;
unsigned long myMillis = 0;
unsigned long myMillis2 = 0;
bool riscossione = false;
volatile bool ledONOFF = LOW;
bool cambioStato = false;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, ledONOFF);
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    //pin = Serial.parseInt();
    value = Serial.parseInt();
    if (Serial.read() == 'x') {
      riscossione = true;
      myMillis = millis();
      if (value > 0) {
        credito ++;
      }
    }
    //Serial.println(pin);
    //Serial.print(value);
    //Serial.print(" ");
    //Serial.println(credito);
  }
  if (riscossione && (millis() - myMillis > TIME_OUT)) {
    if (!cambioStato) {
      //Serial.print("erogo ");
      //Serial.print(pin);
      //Serial.print(" restano ");
      //Serial.println(credito-1);
      pin++;
      cambioStato = true;
      myMillis2 = millis();
    }
    if (millis() - myMillis2 <= TIME_OUT && credito > 0 && cambioStato) {
      digitalWrite(LED_BUILTIN, !ledONOFF);
    }
    else if (millis() - myMillis2 > TIME_OUT && credito > 0 && cambioStato) {
      digitalWrite(LED_BUILTIN, ledONOFF);
      myMillis = millis();
      cambioStato = false;
      credito--;
    }
    if (credito == 0) {
      riscossione = false;
      //Serial.println("sono qua");
      pin = 1;
    }
  }
}