Problem with interrupt to count pulses

Hello Arduineros.
I am making a dispenser for a product and the coin selector sends out pulses to identify the coins.
1 pulses = 100 COP
2 pulses = 200 COP
5 pulses = 500 COP
The wave is square and is normally high, so I used an interruption to detect the falling edge.
The problem comes when I insert enough coins to complete the cost of the product that is 1000 COP, I deliver the product and despite resetting the coin counting variables is on the welcome screen even if you insert more coins, The cycle once in spite of resetting the variables.

 #include <Wire.h>
 #include <LCD.h>
 #include <LiquidCrystal_I2C.h>
 #define I2C_ADDR    0x3F
 #define motor 7
 #define SIR 6
 volatile long pulseTime = 0;
 volatile int pulse = 0;
 int coste = 1000;
 int ssir = 0;
 int coin = 0;
 int count = 0;
 LiquidCrystal_I2C             lcd(I2C_ADDR,2, 1, 0, 4, 5, 6, 7);
void setup(){
  Serial.begin(9600);
  lcd.begin (16,2);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(SIR,INPUT);
  pinMode(motor,OUTPUT);
  attachInterrupt(0, funtionInterruption, FALLING);
}
void loop() {
  ssir = digitalRead(SIR);
  digitalWrite(motor,LOW);
  if(pulse > 0 && millis()- pulseTime > 200){ 
      switch(pulse){
        case 1:
          coin = 100;
          pulse = 0;
          pulseTime=0;
        break;
        case 2:
          coin = 200;
          pulse = 0;
          pulseTime=0;
        break;
        case 5:
          coin = 500;
          pulse = 0;
          pulseTime=0;
        break;
      }
      count = count+coin;
  }
  /*--------------------------*/
  if(count==0 && coin==0){
  lcd.setCursor ( 0, 0 );
  lcd.print("Welcome");
  lcd.setCursor ( 0, 1 );
  lcd.print(" Insert a coin");
  }
  if(count>0 && coin >0){
    lcd.setCursor ( 0, 0 );
    lcd.print("Inserted: $");
    lcd.setCursor ( 13, 0 );
    lcd.print(coin);//COUNT o COIN
    lcd.setCursor ( 0, 1 );
    lcd.print("Lacks: $");
    lcd.setCursor ( 12, 1 );
    lcd.print(coste-count);
  }
  if(count>=1000 && coin >0){
      lcd.setCursor ( 0, 0 );
      lcd.print("Your product is");
      lcd.setCursor ( 2, 1 );
      lcd.print("coming out");
      while(ssir == 1){
      digitalWrite(motor,HIGH);
      ssir = digitalRead(SIR);
    }                         
    coin = 0;
    count = 0;                             
  }
  /*--------------------------*/
}
void funtionInterruption() {
    pulse++;
    pulseTime = millis();
  }

I would greatly appreciate your collaboration. :slight_smile:

pulseTime should be an

volatile unsigned long

Note that millis() is not maintained in your interrupt so you'll only read the time it was when you entered the ISR but that should be fine for your needs, if you want precision, consider using micros()

the section after this  if(pulse > 0 && millis()- pulseTime > 200){ should be considered as critical an not to be interrupted when you enter it because you are resetting the variables in there, so you don't want the ISR to happen and mess around while you execute it --> suspend ISR when you enter that if

In you case you might want to debug and add something telling you something else than 1,2 or 5 has been detected.

Please check posts in the Spanish section on this topic before replying.

J-M-L:
pulseTime should be an

volatile unsigned long

Note that millis() is not maintained in your interrupt so you'll only read the time it was when you entered the ISR but that should be fine for your needs, if you want precision, consider using micros()

the section after this

  if(pulse > 0 && millis()- pulseTime > 200){

should be considered as critical an not to be interrupted when you enter it because you are resetting the variables in there, so you don't want the ISR to happen and mess around while you execute it --> suspend ISR when you enter that if

In you case you might want to debug and add something telling you something else than 1,2 or 5 has been detected.

The code works fine but when the engine turns on, it stays on the welcome screen and even though you insert more coins it stays there

The code works fine...

Well clearly it doesn't...