First of all you said
So when I insert coin 1 then the output should be 10, for coin 2 the
output should be 20 and for coin 3 the output should be 50.
Yes coin 3 would be 50 but it does not means that coin 3 has 3 impulses(he will have 5 impulse), I recommend to you always use this formula:
Coin_Value = Pulse_Count * Denomination.
I don't recommend to you use 3 Impulses from coin acceptor Because you don't have 15 cent and 30 cent coins. In most cases Coin acceptors have:
1/4 1/2 1 2 4 5 8 10 impulses
Generally I prefer to save all data in variables
So lets get to variables for code
const int coinIntPin = 2; //interruptPin 0 is digital pin 2
volatile boolean newCoin = false;
volatile int pulseCount; //counts pulse for
volatile int timeOut = 0; //counts timeout after last coin inserted
float denomination = 0.50; // in my case 1 Pulse = 0.50 $
int max_impulse = 4; // this is the maximum impulses I use
volatile long timeFPulse = 0; // to determine when First impulse was interrupted
volatile long interval; // to determine interval from first and last impulse
And always use attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); It is recommended by Arduino as below:
attachInterrupt(digitalPinToInterrupt(coinIntPin), coinInserted, FALLING);
And I will give you rest of code:
newCoin = true;
timeFPulse = millis();
while (newCoin == true)
interval = millis() - timeFPulse; //interval from first and last impulse
if(pulseCount == max_impulse || (interval > 500 && pulseCount > 0)) //Max_impulse will avoid any extra impulses for any reason.
// This is the reason why I recommended to you use 1 2 4 5 10 20 Impulses instead of 3 7 9 etc...
coinType = pulseCount * denomination;
newCoin = false;
Serial.print("Do what ever you want. Inserted Money = ");
coinType = 0; // remove if you want to save Coins and print sum of inserted coins
pulseCount = 0; // remove if you want to save impulses determined by coin acceptor
And Finally look at ( interval > 500 ) If you have different max impulses you must change 500 to (impulse length * Max impulses + 100); so if you have max 10 impulses you must enter 1100 and not 500 (in my case);