Coin acceptor counting pulses

I bought a coin acceptor from ebay, KAI-638C and Im trying to make it work. I found a code at Instructables but somehow it doesn't count right. When I insert a coin the arduino registers 1-3coins instead of just the one inserted coin. So I guess it's gotta do something about the signals from the acceptor. I have no oscilloscope to see what kind of signal it outputs. I've tried to experiment with different delays(I know delays are not recommended in interrupts. but hey, Im desperate) but no luck so far.

The acceptor and the arduino share the same ground.

Maybe you guys have any thoughts or suggestions I could try because I'm all out of ideas?

const int coinInt = 0; 
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.

volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;                  
//A Coin has been inserted flag

void setup()
{
  Serial.begin(9600);                 
//Start Serial Communication
  attachInterrupt(coinInt, coinInserted, RISING);   
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
}

void coinInserted()    
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 0.05;  
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;                           
//Flag that there has been a coin inserted
}

void loop()
{
  if(coinsChange == 1)          
//Check if a coin has been Inserted
  {
    coinsChange = 0;              
//unflag that a coin has been inserted
  
    Serial.print("Credit: £");
    Serial.println(coinsValue);    
//Print the Value of coins inserted
  }
}

There are adjustments on this device for sensitivity - have you tried them?

regards

Allan.

allanhurst:
There are adjustments on this device for sensitivity - have you tried them?

regards

Allan.

Yes, but I believe that only adjusts the sensitivity for the acceptor and has nothing to do with the signal output?

What really bothers me is that the arduino interprets the signal different every time. If it always would count one coin as three or two coins it wouldn't be a problem. But now its hard to count the coins accurately.

For something like this, I don't think that an interrupt is necessary. If there are really multiple pulses per coin for whatever reason, by changing to polling you could use a 'millis()'-based delay for debounce, so that after a coin is detected, your code doesn't look for another coin until that delay period has elapsed. It only needs to be slightly less than the time between multiple coins that are inserted very quickly. Something in the neighbourhood of 50ms to 100ms.
You could also use an interrupt and do similar, but I don't see a need, as long as your 'loop()' is reasonably fast, without blocking delays.
Then set the coin mechanism's pulse length to the minimum pulse length, (30mS).

3 triggering TIMER SWITCH of Pulse Duration ,
Long : 100 ms
Default : 50 ms
Short : 30 ms

Edit: And don't use 'delay()' in interrupt service routines. It won't work.
From the 'attachInterrupt()' reference:-

Inside the attached function, delay() won't work and the value returned by millis() will not increment.

Something along these lines:-
(Untested.) Edit: Oops, I made a mistake in the first version but just fixed it.

const byte coinPin = 2;

void setup()
{
    pinMode(coinPin, INPUT);
}

void loop()
{
    static unsigned long prevCoinMillis = 0;
    static float coinValue = 0;     // This would be better as an 'int', in cents instead of dollars.
    bool coinInserted = false;

    unsigned long currentMillis = millis();
    if((currentMillis - prevCoinMillis >= 50) && (digitalRead(coinPin) == HIGH))
    {
        prevCoinMillis = currentMillis;
        coinInserted = true;
        coinValue += 0.05;
    }

    if(coinInserted == true)
    {
        // Do things here:-

    }
}

Thx, OldSteve! Smart! It did the trick.

Samfag:
Thx, OldSteve! Smart! It did the trick.

Excellent. Glad to hear it's sorted out. :slight_smile: