Coin Interrupt Issue

I have a coin acceptor connect to an Arduino uno. I am using a coin interrupt code. I counts accurately every time I drop a coin.

The trouble that I am having is that every time I unplug and re-plug the external power source of the coin acceptor, the pulse counts randomly. I am planning to use an optocoupler to fix this issue.

Can anyone give any suggestion. Thank you in advance :slight_smile:

Which pins are you using on the Arduino?
Pin 13, for example, would be a bad choice.

6v6gt:
Which pins are you using on the Arduino?
Pin 13, for example, would be a bad choice.

I used pin 2 of the arduino

Ok. Are you using external interrupts?
Maybe in setup() wait a few seconds before reading anything.
Post the code.

6v6gt:
Ok. Are you using external interrupts?
Maybe in setup() wait a few seconds before reading anything.
Post the code.

Yes, External interrupt. I am using a coin acceptor.

void loop(){
  
  displayCredit();
     attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
 // pinMode(ledpin, OUTPUT);
  unsigned long  lastTime = millis() - timelastPulse;
  buttonState = digitalRead(7);
  if( (pulse > 0 ) && ( lastTime > 200)  ){ // for show LED ON when Inserted
     
     bInserted = false;
      
     credits +=  pulse;
     pulse = 0;

I can't see much of your code but maybe you can move attachInterrupt() to setup() but wait a few seconds before issui6the command.

6v6gt:
I can't see much of your code but maybe you can move attachInterrupt() to setup() but wait a few seconds before issui6the command.

Sorry about that. Are you looking for the setup() part?

 void setup()
 {
Serial.begin(9600);
// lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
lcd.begin(16, 2);
pinMode(coinpin,INPUT);
pinMode(ledpin,OUTPUT);
pinMode(StartButt,INPUT); 
pinMode(ledpin2,OUTPUT); 
   
  } 
 
void loop(){
  
  displayCredit();
     attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
 // pinMode(ledpin, OUTPUT);
  unsigned long  lastTime = millis() - timelastPulse;
  buttonState = digitalRead(7);
  if( (pulse > 0 ) && ( lastTime > 200)  ){ // for show LED ON when Inserted
     
     bInserted = false;
      
     credits +=  pulse;
     pulse = 0;

The whole sketch is always best.
My suggestion may work if there is NOT a detachInterrupt() somewhere in the loop()