Advertisement
bld

Coin Acceptor

bld
Apr 21st, 2012
6,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. /*
  2.   This sketch assumes that coin 1, will return 1 pulse, coin 2 will return 2 pulses, and so on.
  3.  
  4.  Coin values:
  5.  1 = 0.5
  6.  2 = 1
  7.  3 = 2
  8.  4 = 10
  9.  */
  10.  
  11. const byte coinValues[4] = {
  12.   5, 10, 20, 100}; //Coin values goes into this array
  13. #define pulseTimeout 200        //How many milliseconds there are from the last impulse to the coin is determined (default 275)
  14. #define actionTimeout 60000     //How many milliseconds before stored credit is reset (default 60000)
  15.  
  16.  
  17. unsigned long lastAction = 0;   //When last action was made
  18. unsigned long lastPulse = 0;    //When last pulse was send
  19. int pulseCount = 0;             //How many pulses we got
  20. int currentCredit;              //The current credit
  21.  
  22. void showCredit()
  23. {
  24.   int credit = currentCredit/10;
  25.   int left = currentCredit % (currentCredit/10);
  26.  
  27.   Serial.print("Current credit: ");
  28.   Serial.print(credit, DEC);
  29.   Serial.print(".");
  30.   Serial.print(left, DEC);
  31.   Serial.println("0");
  32. }
  33.  
  34. void setup()
  35. {
  36.   Serial.begin(115200);
  37.  
  38.   attachInterrupt(0, acceptorCount, RISING); //Digital interrupt pin 1
  39.   attachInterrupt(1, acceptorPulse, RISING); //Digital interrupt pin 2
  40.  
  41.   Serial.println("Coin Acceptor ready");
  42.   pulseCount = 0;
  43.  
  44.   pinMode(13, OUTPUT);
  45. }
  46.  
  47. unsigned long tempAction;
  48. unsigned long tempPulse;
  49.  
  50. void loop()
  51. {
  52.   tempAction = lastAction;
  53.   tempPulse = lastPulse;
  54.  
  55.   if (millis() - lastPulse >= pulseTimeout && pulseCount > 0 || pulseCount >= 4)
  56.   {
  57.     if (tempAction != lastAction || tempPulse != lastPulse) return; //Check if interrupt has fired since loop started, wait for next cycle if it has
  58.    
  59.     tone(12, 1800, 150);
  60.     currentCredit += coinValues[pulseCount-1];
  61.     showCredit();
  62.     pulseCount = 0;
  63.   }
  64.  
  65.  
  66.   if ((millis() - lastAction >= actionTimeout) && (lastAction != 0) && (currentCredit > 0) && (pulseCount == 0))
  67.   {
  68.     if (tempAction != lastAction || tempPulse != lastPulse) return; //Check if interrupt has fired since loop started, wait for next cycle if it has
  69.    
  70.     currentCredit = 0;
  71.     lastAction = 0;
  72.     Serial.println("\n*** TIMEOUT! Credit reset. ***");
  73.     showCredit();
  74.   }
  75. }
  76.  
  77. void acceptorPulse()
  78. {
  79.   lastAction = millis();
  80.   lastPulse = millis();
  81.   pulseCount++;
  82. }
  83.  
  84. void acceptorCount()
  85. {
  86.   digitalWrite(13, digitalRead(13));
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement