Coin Accepter for coin Recognition

//initialization

int pulse=0;

//read coin value
void coin_value()
{
pulse=pulse+1;

}

void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
Serial.println("Ready to be collected");
}

void loop()
{
if (digitalRead(2) == HIGH)
{
//0 as Interrupt digital pin 2
//call coin_value function during the pulse falling from high to low
attachInterrupt(0, coin_value, RISING);
delay(300);
if(pulse==3)
{
Serial.println("50");
delay(1);
}
if(pulse==2)
{
Serial.println("20");
delay(1);
}
if(pulse==1)
{
Serial.println("10");
delay(1);
}
}

pulse=0;
}

This is my code. Whenever i connect my Coin Accepter CH-926 to Arduino Uno pin no 2, it shows random output. The connections are properly made.
The output of the code varies after each coin.
Some times it gives 10 while the other time it gives 20 even when i put the same coin.

Please edit your post to have code tags. See How to use the forum please.

Next, apparently the coin output is open collector. Aka, you need a pull up resistor. Easy way, enable the internal pull up resistor by calling pinMode(pin, INPUT_PULLUP).

Some reading for you: http://blog.deconinck.info/post/2017/02/25/CH923/CH926/CH928-coin-acceptor-features-and-caveats

  //call coin_value function during the pulse falling from high to low
    attachInterrupt(0, coin_value, RISING);

?

Ahh, yeah, the worst kind of comments, wrong comments ::slight_smile:

But yeah, more errors in the code. 'pulse' isn't volatile. 'pulse' is read without disabling interrupts. And for what? Even on fast a pulse is still 20ms. But okay, it's not human slow so interrupt is okay. But you have to use it correctly :slight_smile:

const int coinPin = 2;
volatile int pulse = 0;

int counter=0;

void setup() {
  
  Serial.begin(9600);
}

void loop() {

 attachInterrupt(digitalPinToInterrupt(coinPin), coin, RISING);


  if(pulse==1)
  {
    if(counter==2){
    Serial.println("2");
    counter=0;
  }
    else if(counter==5){
    Serial.println("5");
    counter=0;
  }
  pulse=0;
  }
}

void coin(){
  pulse++;
  counter++;
  
}

I,m using a coin accepter CH-926 which gives pulses as an output. I,m using it to recognize coin types (say 3 coins)

coin 1-- Pulse 1
coin 2--Pulse 2
coin 3--pulse 5
this is the configuration of the coin accepter.

My system should do a job for coin 1 and some other job for coin 2 and then another for coin 3.
I'm unable to do this.

the control never reaches the lowest IF loop. Any suggestions ?

Why is pulse volatile, but not counter?
Why do you keep attaching the interrupt in loop()?
Why are you even using interrupts?
What does your timing diagram look like?

I've copied the code from instructables and trying to make the logic of my own. But unable to do the same.
I don't really know what the volatile keyword does.

ssumitk14:
I’ve copied the code from instructables

That is generally a very bad idea, most of the stuff at instructables is crap.

ssumitk14:
I don’t really know what the volatile keyword does.

And Google is broken again?