Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch assumes that coin 1, will return 1 pulse, coin 2 will return 2 pulses, and so on.
- Coin values:
- 1 = 0.5
- 2 = 1
- 3 = 2
- 4 = 10
- */
- const byte coinValues[4] = {
- 5, 10, 20, 100}; //Coin values goes into this array
- #define pulseTimeout 200 //How many milliseconds there are from the last impulse to the coin is determined (default 275)
- #define actionTimeout 60000 //How many milliseconds before stored credit is reset (default 60000)
- unsigned long lastAction = 0; //When last action was made
- unsigned long lastPulse = 0; //When last pulse was send
- int pulseCount = 0; //How many pulses we got
- int currentCredit; //The current credit
- void showCredit()
- {
- int credit = currentCredit/10;
- int left = currentCredit % (currentCredit/10);
- Serial.print("Current credit: ");
- Serial.print(credit, DEC);
- Serial.print(".");
- Serial.print(left, DEC);
- Serial.println("0");
- }
- void setup()
- {
- Serial.begin(115200);
- attachInterrupt(0, acceptorCount, RISING); //Digital interrupt pin 1
- attachInterrupt(1, acceptorPulse, RISING); //Digital interrupt pin 2
- Serial.println("Coin Acceptor ready");
- pulseCount = 0;
- pinMode(13, OUTPUT);
- }
- unsigned long tempAction;
- unsigned long tempPulse;
- void loop()
- {
- tempAction = lastAction;
- tempPulse = lastPulse;
- if (millis() - lastPulse >= pulseTimeout && pulseCount > 0 || pulseCount >= 4)
- {
- if (tempAction != lastAction || tempPulse != lastPulse) return; //Check if interrupt has fired since loop started, wait for next cycle if it has
- tone(12, 1800, 150);
- currentCredit += coinValues[pulseCount-1];
- showCredit();
- pulseCount = 0;
- }
- if ((millis() - lastAction >= actionTimeout) && (lastAction != 0) && (currentCredit > 0) && (pulseCount == 0))
- {
- if (tempAction != lastAction || tempPulse != lastPulse) return; //Check if interrupt has fired since loop started, wait for next cycle if it has
- currentCredit = 0;
- lastAction = 0;
- Serial.println("\n*** TIMEOUT! Credit reset. ***");
- showCredit();
- }
- }
- void acceptorPulse()
- {
- lastAction = millis();
- lastPulse = millis();
- pulseCount++;
- }
- void acceptorCount()
- {
- digitalWrite(13, digitalRead(13));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement