Go Down

Topic: Problem with number of pulses with coin selector  (Read 551 times) previous topic - next topic

castorino88

Feb 09, 2018, 05:01 pm Last Edit: Feb 09, 2018, 06:03 pm by castorino88
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:

Code: [Select]
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;
 }
}

vinceherman

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

7. 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.

ardly

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.
"Facts do not cease to exist because they are ignored" - Aldous Huxley

Robin2

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

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

castorino88

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.



Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

castorino88

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

castorino88

Here are photos for my wiring and my serial output:

Paul_KD7HB

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

castorino88

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 ?

Paul_KD7HB

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

castorino88

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


Does my code looks fine?


Code: [Select]
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;
  }
}

Paul_KD7HB

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

castorino88


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?



Code: [Select]
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();
  }
}

Go Up