Trouble with running RC522 RFID together with bill/coin acceptors

Good day, I’m using an Arduino Micro and I can’t seem to merge the RFID with the acceptors. If I run the RFID and the acceptors independtly, they work great. But when I combine them the RFID is the only one working fine and the acceptors sometimes output to the serial, sometimes they dont or they output the wrong count.

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 9;
constexpr uint8_t SS_PIN = 10;
MFRC522 rfid(SS_PIN, RST_PIN);

const byte COINpin = 2;
const byte BILLpin = 3;

volatile int min_pulse_width; // the minimum pulse width to acccept
volatile int max_pulse_width; // the maximum pulse width to accept
volatile int debounce_speed; // ignore changes in input line state that happen faster than this
volatile int pulse_count; // how many pulses have been received so far in this pulse train

volatile unsigned long pulse_duration; // how long was the last pulse
volatile unsigned long pulse_begin; // when did the last pulse begin
volatile unsigned long pulse_end; // if they pulse was within min and max pulse width, when did it end
volatile unsigned long curtime; // what is the current time
volatile int post_pulse_pause; // how long to wait after last pulse before sending pulse count
volatile int pulse_state; // what is the current input line state (1 for high, 0 for low)
volatile int last_state; // what was the last input line state

void RFIDcall()
{
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial())
  {
    for (byte i = 0; i < rfid.uid.size; i++)
    {
      Serial.print(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
      Serial.print(rfid.uid.uidByte[i], HEX);
    }
    rfid.PICC_HaltA();
    Serial.print("\n");   //to give newline to seril output
  }
}

void coin()
{
  pulse_state = digitalRead(COINpin);
  if ((pulse_state == 1) && (last_state == 0))
  {
    pulse_begin = curtime;
    last_state = 1;
  }
  else if ((pulse_state == 0) && (last_state == 1))
  {
    pulse_duration = curtime - pulse_begin;
    if (pulse_duration > debounce_speed)
    {
      last_state = 0;
    }
    if (pulse_duration > min_pulse_width)
    {
      pulse_end = curtime;
      pulse_count++;
    }
  }
}

void bill()
{
  pulse_state = digitalRead(BILLpin);
  if ((pulse_state == 1) && (last_state == 0))
  {
    pulse_begin = curtime;
    last_state = 1;
  }
  else if ((pulse_state == 0) && (last_state == 1))
  {
    pulse_duration = curtime - pulse_begin;
    if (pulse_duration > debounce_speed)
    {
      last_state = 0;
    }
    if (pulse_duration > min_pulse_width)
    {
      pulse_end = curtime;
      pulse_count++;
    }
  }
}

void count()
{
  if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough that we don't expect any further pulses to be forthcoming
  {
    switch (pulse_count)
    {
      case 1:
        Serial.println(F("1"));
        pulse_count = 0;
        break;
      case 5:
        Serial.println(F("5"));
        pulse_count = 0;
        break;
      case 10:
        Serial.println(F("10"));
        pulse_count = 0;
        break;
      case 20:
        Serial.println(F("20"));
        pulse_count = 0;
        break;
      case 50:
        Serial.println(F("50"));
        pulse_count = 0;
        break;
      case 100:
        Serial.println(F("100"));
        pulse_count = 0;
        break;
      case 200:
        Serial.println(F("200"));
        pulse_count = 0;
        break;
      case 500:
        Serial.println(F("500"));
        pulse_count = 0;
        break;
      case 1000:
        Serial.println(F("1000"));
        pulse_count = 0;
        break;
    }
    pulse_end = 0;
  }
}

void setup()
{
  pinMode(COINpin, INPUT_PULLUP);
  Serial.begin(9600);

  //  Keyboard.begin();
  pulse_begin = 0;
  last_state = 0;
  min_pulse_width = 40;
  max_pulse_width = 60;
  debounce_speed = 1;
  post_pulse_pause = 150;
  pulse_end = 0;
  pulse_count = 0;
  
  SPI.begin();
  rfid.PCD_Init(); // Init MFRC522
}

void loop()
{
  curtime = millis();
  coin();
  bill();
  count();
  RFIDcall();
}

My goal is whatever comes first, a coin, a bill, or a tapped card, it will be processed. Not simultaneously but what comes first.

As for the connections, I followed the RC522 library with the IRQ not connected. And the acceptors using one digital pin each. I used the 5v to the optocoupler to clean the pulse signal from the acceptors. Everything is connected to the GND.
Thanks for the help! Will really appreciate it

I’ve fixed it somehow. It seems that the RFID blocks the pulse counting so I’ve set the RFID to only work when the pulse count is zero.

#include <SPI.h>
#include <MFRC522.h>

constexpr uint8_t RST_PIN = 9;
constexpr uint8_t SS_PIN = 10;
MFRC522 rfid(SS_PIN, RST_PIN);

const byte COINpin = 2;
const byte BILLpin = 3;

volatile int min_pulse_width; // the minimum pulse width to acccept
volatile int max_pulse_width; // the maximum pulse width to accept
volatile int debounce_speed; // ignore changes in input line state that happen faster than this
volatile int pulse_count; // how many pulses have been received so far in this pulse train

volatile unsigned long pulse_duration; // how long was the last pulse
volatile unsigned long pulse_begin; // when did the last pulse begin
volatile unsigned long pulse_end; // if they pulse was within min and max pulse width, when did it end
volatile unsigned long curtime; // what is the current time
volatile int post_pulse_pause; // how long to wait after last pulse before sending pulse count
volatile int pulse_state; // what is the current input line state (1 for high, 0 for low)
volatile int last_state; // what was the last input line state

void RFIDcall()
{
  if (pulse_count == 0)
  {
    if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial())
    {
      for (byte i = 0; i < rfid.uid.size; i++)
      {
        Serial.print(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
        Serial.print(rfid.uid.uidByte[i], HEX);
      }
      rfid.PICC_HaltA();
      Serial.print("\n");   //to give newline to seril output
    }
  }
  else
    return;
}

void coin()
{
  pulse_state = digitalRead(COINpin);
  if ((pulse_state == 1) && (last_state == 0))
  {
    pulse_begin = curtime;
    last_state = 1;
  }
  else if ((pulse_state == 0) && (last_state == 1))
  {
    pulse_duration = curtime - pulse_begin;
    if (pulse_duration > debounce_speed)
    {
      last_state = 0;
    }
    if (pulse_duration > min_pulse_width)
    {
      pulse_end = curtime;
      pulse_count++;
    }
  }
  if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough that we don't expect any further pulses to be forthcoming
  {
    if (pulse_count == 1)
    {
      Serial.println(F("1"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 1) && (pulse_count <= 6))
    {
      Serial.println(F("5"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 5) && (pulse_count <= 11))
    {
      Serial.println(F("10"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 16) && (pulse_count <= 21))
    {
      Serial.println(F("20"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 20) && (pulse_count <= 51))
    {
      Serial.println(F("50"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 50) && (pulse_count <= 101))
    {
      Serial.println(F("100"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 100) && (pulse_count <= 201))
    {
      Serial.println(F("200"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 200) && (pulse_count <= 401))
    {
      Serial.println(F("500"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 500) && (pulse_count <= 1001))
    {
      Serial.println(F("1000"));
      pulse_count = 0;
      curtime = 0;
    }
    else
    {
      Serial.println(F("F U"));
      pulse_count = 0;
      curtime = 0;
    }
    pulse_end = 0;
  }
}


void bill()
{
  pulse_state = digitalRead(BILLpin);
  if ((pulse_state == 1) && (last_state == 0))
  {
    pulse_begin = curtime;
    last_state = 1;
  }
  else if ((pulse_state == 0) && (last_state == 1))
  {
    pulse_duration = curtime - pulse_begin;
    if (pulse_duration > debounce_speed)
    {
      last_state = 0;
    }
    if (pulse_duration > min_pulse_width)
    {
      pulse_end = curtime;
      pulse_count++;
    }
  }
  if ((pulse_end > 0) && (curtime - pulse_end > post_pulse_pause)) // check if we've waited long enough that we don't expect any further pulses to be forthcoming
  {
    if (pulse_count == 1)
    {
      Serial.println(F("1"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 1) && (pulse_count <= 6))
    {
      Serial.println(F("5"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 5) && (pulse_count <= 11))
    {
      Serial.println(F("10"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 10) && (pulse_count <= 21))
    {
      Serial.println(F("20"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 20) && (pulse_count <= 51))
    {
      Serial.println(F("50"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 50) && (pulse_count <= 101))
    {
      Serial.println(F("100"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 100) && (pulse_count <= 201))
    {
      Serial.println(F("200"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 200) && (pulse_count <= 401))
    {
      Serial.println(F("500"));
      pulse_count = 0;
      curtime = 0;
    }
    else if ((pulse_count > 500) && (pulse_count <= 1001))
    {
      Serial.println(F("1000"));
      pulse_count = 0;
      curtime = 0;
    }
    else
    {
      Serial.println(F("F U"));
      pulse_count = 0;
      curtime = 0;
    }
    pulse_end = 0;
  }
}

void setup()
{
  pinMode(COINpin, INPUT_PULLUP);
  Serial.begin(9600);

  //  Keyboard.begin();
  pulse_begin = 0;
  last_state = 0;
  min_pulse_width = 40;
  max_pulse_width = 60;
  debounce_speed = 1;
  post_pulse_pause = 150;
  pulse_end = 0;
  pulse_count = 0;

  SPI.begin();
  rfid.PCD_Init(); // Init MFRC522
}

void loop()
{
  curtime = millis();
  coin();
  bill();
  RFIDcall();
}