//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).
Ahh, yeah, the worst kind of comments, wrong comments :
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
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.