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