Problem with number of pulses with coin selector

Hello guys,

I am using the multi coin acceptor CH-923 connected to Arduino Uno , i programmed it to accept 3 different coins. the first type of coin should give me 1 pulse, the second 5 pulses, the third 10 pulses.

I decided to use the interrupt function to count the number of pulses. But when ever i run the code it gives me a crazy number of pulses each time i insert a coin.

Is it a problem with my code?
Is it a problem with my wiring?
do i need to add some sort of debounce function?

Wiring :
I have used common ground for the Arduino and coin acceptor.
I tried to use a pull down resistor between in the input in and the ground.

code:

const int coinInt = 0;


void setup()
{
 // Debugging output
 Serial.begin(9600);

 Serial.println("Ready...");
 
 
 attachInterrupt(coinInt, coinISR, RISING);  // COIN wire connected to D2;
}


// total amount of money collected;
float money = 0.0;

// gets incremented by the ISR;
// gets reset when coin was recognized (after train of pulses ends);
volatile int pulses = 0;
volatile long timeLastPulse = 0;


// executed for every pulse;
void coinISR()
{
 pulses++;
 timeLastPulse = millis();
}


void loop()
{

 long timeFromLastPulse = millis() - timeLastPulse;
 if (pulses > 0 && timeFromLastPulse > 200)
 {
   // sequence of pulses stopped; determine the coin type;
   if (pulses == 1)
   {
     Serial.println("1 pulses");
     money += .1;
   }
   else if (pulses == 5)
   {
     Serial.println("5 pulses");
     money += .25;
   }
   else if (pulses == 10)
   {
     Serial.println("10 pulses");
     money += 1.0;
   }
   else if (pulses == 15)
   {
     Serial.println("Received tooney (15 pulses)");
     money += 2.0;
   }
   else
   {
     Serial.print("Unknown coin: ");
     Serial.print(pulses);
     Serial.println(" pulses");
   }

   pulses = 0;
 }
}

Hi and welcome to the forum.
Take a few minutes to read through the post linked to at the top of every forum named How to use this forum.

Especially

  1. If you are posting code or error messages, use "code" tags

Can you show us a wiring diagram? A pencil, paper and a camera are good enough if you include proper detail (like pin numbers). Read through this handy image guide.

Why use interrupts?
To start why not just count and print the number of pulses?

Once you know you are getting the correct number of pulses for the correct coin then add more code.

There was another recent and extensive Thread about a coin mechanism. Google should be able to find it for you.

...R

I am using interrupt because all the source codes I am able find online that uses the same device uses interrupt.

To be honest I am not sure how to use a different function to count pulses. When ever I google " pulse counter Arduino" the interrupt function pops up.

I understand there are similar posts that uses the same device, but the posts I was able to find are solving different issues, and instead of hijacking other posts I decided to start my own.

castorino88:
I understand there are similar posts that uses the same device, but the posts I was able to find are solving different issues, and instead of hijacking other posts I decided to start my own.

I was not suggesting you should hijack another Thread - merely learn from it. Even if the overall direction is different it may shed some light on your problems.

It is perfectly fine to use interrupts if you understand what you are doing.

...R

Sadly I am not able to use other posts to solve my problem, and believe me I tried hard.

I believe I m using the interrupt function correctly. I understood how it is used, and tried to implement what i learned. The thing is that it is giving me 400+ pulses when I'm expecting only 1,5,10...Something is really off, I just don't know what.

castorino88:
Something is really off, I just don't know what.

As already requested, post a wiring diagram.

A photo of the actual wiring would also be a good idea. Maybe there is electrical interference on the wire connected to your interrupt pin.

Have you a good GND connection between the coin device and the Arduino?

...R

Here are photos for my wiring and my serial output:

IMG_5643.jpg

IMG_5644.jpg

Did you find this: http://blog.deconinck.info/post/2017/02/25/CH923/CH926/CH928-coin-acceptor-features-and-caveats.

And follow the suggestions? 1 Amp minimum power supply, Coin line may need to be attached to a pin with "INPUT_PULLUP". And other information, as well.

Paul

The link you have sent tackle a problem where if two or more coins are inserted in a short period of time that would cause a false reading on the number of pulses.

But my problem is different. I am inserting one coin and the pulses are incorrect.

The power supply I am using offers 2 Amp, so no problem here.

I am not sure what you mean about the input_Pullup? can you tell me where you got that information from ?

Just reading down the blog page. Writer says may or may not be needed. Only examining line with a DVM will tell. It may be either ground or floating, according to the blog. If floating, you need a resistor to hold it positive and easiest way is to use the internal pull up resistor for that Arduino pin.

Paul

I added an internal PULLUP resister that doesn't seem to solve my problem.

Does my code looks fine?

const int coinInt = 0;


void setup()
{
  // Debugging output
  Serial.begin(9600);

  Serial.println("Ready...");
  
  pinMode (2,INPUT_PULLUP);
  
  attachInterrupt(coinInt, coinISR, RISING);  // COIN wire connected to D2;
}


// total amount of money collected;
float money = 0.0;

// gets incremented by the ISR;
// gets reset when coin was recognized (after train of pulses ends);
volatile int pulses = 0;
volatile long timeLastPulse = 0;


// executed for every pulse;
void coinISR()
{
  pulses++;
  timeLastPulse = millis();
}


void loop()
{

  long timeFromLastPulse = millis() - timeLastPulse;
  if (pulses > 0 && timeFromLastPulse > 200)
  {
    // sequence of pulses stopped; determine the coin type;
    if (pulses == 1)
    {
      Serial.println("2 pulses");
      money += .1;
    }
    else if (pulses == 5)
    {
      Serial.println("5 pulses");
      money += .25;
    }
    else if (pulses == 10)
    {
      Serial.println("10 pulses");
      money += 1.0;
    }
    else if (pulses == 15)
    {
      Serial.println("Received tooney (15 pulses)");
      money += 2.0;
    }
    else
    {
      Serial.print("Unknown coin: ");
      Serial.print(pulses);
      Serial.println(" pulses");
    }

    pulses = 0;
  }
}

Not quite. Here is code from my anemometer program:

void loop()
{
// make a working copy of the counter while disabling interrupts
cli();
cnt = icounter; // Two interrupts per revolution.
sei();

You are using your counter in "loop" while it is being incremented in the ISR function. Make a copy to work with like I did.

Paul

I have used the cli() and sei() like you mentioned, still didn't fix the problem, here is my code Im i using it correctly?

const int coinInt = 0;


void setup()
{
  // Debugging output
  Serial.begin(9600);

  Serial.println("Ready...");
  
  pinMode (2,INPUT_PULLUP);
  
  attachInterrupt(coinInt, coinISR, RISING);  // COIN wire connected to D2;
}


// total amount of money collected;
float money = 0.0;

// gets incremented by the ISR;
// gets reset when coin was recognized (after train of pulses ends);
volatile int pulses = 0;
volatile long timeLastPulse = 0;


// executed for every pulse;
void coinISR()
{
  pulses++;
  timeLastPulse = millis();
}


void loop()
{

  long timeFromLastPulse = millis() - timeLastPulse;
  if (pulses > 0 && timeFromLastPulse > 200)
  {
    cli();
    // sequence of pulses stopped; determine the coin type;
    if (pulses == 1)
    {
      Serial.println("2 pulses");
      money += .1;
    }
    else if (pulses == 5)
    {
      Serial.println("5 pulses");
      money += .25;
    }
    else if (pulses == 10)
    {
      Serial.println("10 pulses");
      money += 1.0;
    }
    else if (pulses == 15)
    {
      Serial.println("Received tooney (15 pulses)");
      money += 2.0;
    }
    else
    {
      Serial.print("Unknown coin: ");
      Serial.print(pulses);
      Serial.println(" pulses");
    }

    pulses = 0;
    sei();
  }
}

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you disconnect the coin counter completely and just pulse the input manually to see if your counter is working.

Thanks... Tom... :slight_smile:

A little bit of googling showed that this device has an open collector output.

These pulse count numbers point to a floating pin.

A look at OPs code showed he didn't bother to set the pinMode for the pin he reads the input from (bad practice, even though the default mode of pins is INPUT).

So I think adding this line to setup() will solve it:

  pinMode(2, INPUT_PULLUP);

Images from Reply #8 so we don't have to download them. See this Simple Image Guide

04fc22c12b4fa33f25f306184e32f92508f58481.jpg

fa47d0e266bdb53b54d9b480a808edb3f87552c1.jpg

...R

You need to post a circuit diagram - the photos are not a substitute for that.

Also you need to explain what is shown in the photos.

And the screenshot is unreadable. Don't post pictures of text. Just copy and paste the text.

...R

GitHub - netpipe/coin_acceptor: Sintron multi programmable coin selector code ch-9xx i would still like to get the coin counter wire working to avoid my 12 pulse problem