Interrupt freezing the system instead of continuing the loop?

Hi, so i'm trying to create a coin dispensing system. I'm attaching a sensor to interrupt to increment coin_count by 1. But everytime the interrupt triggers, the whole system freezes even though serial.print is in the loop() instead of the ISR. (Later on coin_requested will be edited to receive variable from python, but right now it's just a number to test the system)

#define sensor_pin 2
#define relay_pin 12
#define motor_R_enable 9
#define motor_L_enable 8
#define R_PWM 7
#define L_PWM 6
volatile byte motor_status = LOW;
volatile int coin_count = 0;
volatile int coin_requested = 2;
volatile byte web_trigger = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(sensor_pin, INPUT);
  pinMode(relay_pin, OUTPUT);
  pinMode(motor_R_enable, OUTPUT);
  pinMode(motor_L_enable, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  pinMode(R_PWM, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(sensor_pin), countup, FALLING);
}

void loop() {
  while (motor_status == LOW) {

    if (coin_requested > 0) {
      motor_status = HIGH;
    }
  }

  while (motor_status == HIGH) {
    motor_RPWM();
    Serial.print(coin_count);
    Serial.println(coin_requested);
    if (coin_count >= coin_requested) {
      motor_off();
      reset_var();
      web_trigger = 1;
      Serial.println(web_trigger);
    }
  }
}
void reset_var() {
  coin_count = 0;
  coin_requested = 0;
  web_trigger = 0;
}

void countup() {
  coin_count ++;
}

void motor_RPWM() {
  digitalWrite(relay_pin, HIGH);
  digitalWrite(motor_R_enable, HIGH);
  digitalWrite(motor_L_enable, HIGH);
  analogWrite(R_PWM, 150);
  analogWrite(L_PWM, 0);
}

void motor_off() {
  motor_status = LOW;
  digitalWrite(motor_R_enable, LOW);
  digitalWrite(motor_L_enable, LOW);
  digitalWrite(relay_pin, LOW);
  analogWrite(R_PWM, 0);
  analogWrite(L_PWM, 0);
}

That code does not compile.

Ah, sorry, made error on the edits when posting, should be

attachInterrupt(digitalPinToInterrupt(sensor_pin), countup, FALLING);

fixed it on the original post

technically speaking only this variable needs to be volatilevolatile int coin_count = 0;So how is the sensor wired up ?pinMode(sensor_pin, INPUT);does it have a pullup resistor ? Actually how are you powering the whole thing ? within the code i see no real reason for the interrupt to mess things up.
From my point of view it makes more sense to decrement the 'coin_requested' within the ISR, but your way should work as well.

The motor should turn on until coin_count reaches 2 (coin_requested). Then coin_requested is set to 0 and this loop gets stuck:

 while (motor_status == LOW)
  {
    if (coin_requested > 0)
    {
      motor_status = HIGH;
    }
  }

Maybe 'while' should be 'if'. That would allow the not-yet-written code that changes 'coin_requested' to something other than zero.

It's a simple IR optocoupler sensor with VCC, GND, and DO. I didn't put any resistor as the output is around 4,8v with very low amp. After the increment and serial.print(coin_count) the system somehow freezes, only outputing 1 coin instead of 2

I didn't put any resistor as the output is around 4,8v with very low amp.

Please post a hand drawn wiring diagram and a link to the optical interruptor.

Most require a resistor.

jremington:
Please post a hand drawn wiring diagram and a link to the optical interruptor.

Most require a resistor.

5v and ground to arduino, digital out to sensor_pin.
It's a generic chinese speed sensor module, i've never had problem using it without resistor

You have a problem now, but if that is all the information you can provide, you will have to live with it.

jremington:
You have a problem now, but if that is all the information you can provide, you will have to live with it.

I've made with the exact same hardware and woring (literally didn't touch it), another version with firmata and it works fine, just that the timing is a bit off since firmata is too slow, now i'm trying to make one with a more reliable timing. So pretty sure it's not hardware problem

Ok so turns out as john said, it is stuck on the while because the hardware was bouncing. The solution was to add a capacitor, but since my deadline is drawing close and in the current world condition, i'm implementing debouncing algorithm and it works. Thank you for all of you who tried to help.