Coin Slot to Arduino... Need Help. What to do?
This is the code i used, (modified)(original comments still there)
This code doesn't seem to do what i want as an output.
const int coinInt = 0;
//Attach coinInt to Interrupt Pin 0 (Digital Pin 2). Pin 3 = Interrpt Pin 1.
volatile float coinsValue = 0.00;
//Set the coinsValue to a Volatile float
//Volatile as this variable changes any time the Interrupt is triggered
int coinsChange = 0;
int OneLed = 8;
int FiveLed = 9;
int TenLed = 10;
//A Coin has been inserted flag
//Start Serial Communication
attachInterrupt(coinInt, coinInserted, RISING);
//If coinInt goes HIGH (a Pulse), call the coinInserted function
//An attachInterrupt will always trigger, even if your using delays
//The function that is called every time it recieves a pulse
coinsValue = coinsValue + 1;
//As we set the Pulse to represent 5p or 5c we add this to the coinsValue
coinsChange = 1;
//Flag that there has been a coin inserted
if(coinsChange == 1)
//Check if a coin has been Inserted
if (coinsValue == 1)
else if (coinsValue == 5)
else if (coinsValue == 10)
//Print the Value of coins inserted
coinsChange = 0;
What i want to do is, after receiving pulses from the COIN SLOT the arduino will count the number of pulses (1,5, or 10 pulses) then lights up LED corresponding to the number of pulses (if there is 1 pulse, LED1 will be HIGH; if there are 5 pulses, LED2 will be HIGH; if there are 10 pulses, LED3 will be HIGH)
Remember the ISR will run once for each pulse so you will be seeing the oneled all the time.
I assume you want to count a stream of pulses, 1, 5, or 10 and then flash the appropriate LED
As it stands now you would see the first of 10 and set coinsChange
Then if nothing else was going on and you happened to be at the top of the loop you would immediately flash the One led.
While that was happening the value of coinsValue would still be altering.
Also you don't appear to be resting coinsValue to o but I assume that is because you are using it somewhere else.
I think you are confusing logical and real events, consider ....
Your digital pin going high is a real event, the coin mechanism id sending data so deal with it!
But it inst a coin a inserted event ... That occurs after the mechanism go's active.
The pulses from the mechanisme will have a set period right?
So after you start counting you can deduce that a coin has been inserted when you don't see a pulse for longer than the set period.
At that point you count the pulses that you have previously received and you establish what coin it was.
If you are counting cion's You will also probably want a FIFO buffer (First In First Out)
When you see a 'coin' not a pulse put it in the buffer and clear the count.
If you are using the pulse count as a value, so not clearing it, then your definition of 'coin' will depend on what is currently in the count.
like this ...
5 pulses and a gap > 5c coin has been processed flash the 5 led.
10 more pulses and a gap > Your count is now 15 to to deduce that it was a 10c coin you need to subtract the previous count 5 from the current count 15 to get 10
now update the previous count ready for the next coin.
In fact you would always subtract previous from current but previous would start at 0
You are still better with a buffer though ... no chance of missing anything that way.