#define COIN_PIN 2
void setup()
{
// Debugging output
Serial.begin(9600);
// set up the LCD's number of rows and columns:
Serial.println("Ready...");
pinMode(COIN_PIN, INPUT);
attachInterrupt(0, 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 == 2)
{
Serial.println("Received dime (2 pulses)");
money += .1;
}
else if (pulses == 5)
{
Serial.println("Received quarter (5 pulses)");
money += .25;
}
else if (pulses == 10)
{
Serial.println("Received looney (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;
}
}