cyborg8274:
how about this code
IMHO the code in your Original Post was better - going in the right direction.
What gave you the idea to use pulseIn()
Let's think about this logically ...
When you put in a coin that produces the maximum number of pulses what is the time interval (in microsecs) between the first pulse and the last pulse?
When you ISR first detects a pulse it should save the value of micros() and your maain code should allow it to keep counting pulses until enough time has elapsed that you can be certain that no more pulses will come from that coin. I have no idea what that time interval might be but let's pretend it is 20 millisecs or 20,000 microsecs. Then the code in your ISR (extended from your Original Post) might be like this
void coinInserted()
{
if (waitingForCoin == true) { // make sure it only saves the time for the first pulse
isrSavedMicros = micros();
waitingForCoin = false;
}
coins = coins + 1;
}
and in your code in loop() you could have something like this
void loop() {
noInterrupts();
if (waitingForCoin == false) {
coinStartMicros = isrSavedMicros;
tempCoinPulses = coins;
}
interrupts();
if (micros - coinStartMicros >= maxPulseTimeMicros) { // we know there will be no more pulses
numCoinPulses = tempCoinPulses;
waitingForCoin = true;
}
// other code
}
...R