Advertisement
Guest User

Untitled

a guest
Aug 28th, 2013
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. // Raynerd Change Machine ****
  2. /* This code controls both a coin hopper which dispences 10 pence coins and a 6 coin acceptor. The 6 coins acceptor pulses on coinSelectorPin"2" - 1 pulse = £1, 2 pulse = 2p, 3pulses = 5p...
  3. The arduino reads which code has been inserted and dispences the appropriate number of 10 pence coins. e.g. £1 = 10x10p 50p = 5x10p
  4. */
  5.  
  6. //**************Variables ****************
  7. volatile byte coinPulseCount=0; // a counter to see how many times the pin has changed - which coin inserted
  8. volatile byte hopperPulseCount = 0; // a counter to she how many coins have been ejected
  9. volatile unsigned long pulseTime; //this stores the time of the last pulse.
  10. byte newCoinInserted; // a place to put our last coin pulse count
  11. byte coinValue = 0; // number of pulses required to dispence each coin type.
  12. byte twoPence = 0; // count 5x 2 pences to dispence 10p
  13. byte fivePence = 0; // count 2x 5 pences to dispence 10p
  14. //***********************************************************************************************
  15. byte pulseThreshold = 500; //EDIT THIS VALUE TO CHANGE DELAY BETWEEN DETECTING BANK OF PULSES
  16. //***********************************************************************************************
  17.  
  18. //************Pins Used *******************
  19. int hopperPin = 2; // pin2 as optical count input
  20. int coinSelectorPin = 3; // pin3 as optical count input
  21. int relayPin = 7; // pin7 output relay
  22.  
  23.  
  24.  
  25. void setup()
  26. {
  27. Serial.begin(9600);
  28.  
  29. //**** pinModes *************
  30. pinMode(hopperPin, INPUT); //hopper optical count is an input
  31. pinMode(coinSelectorPin, INPUT); //coin selector optical input
  32. pinMode (relayPin, OUTPUT); //relay pin output
  33. digitalWrite(relayPin, HIGH); //turn off relay - active LOW.
  34. digitalWrite(hopperPin, HIGH); //use the internal pullup resistor on the hopper optical input
  35.  
  36. attachInterrupt(1, coinacceptor, RISING); // CoinAcceptor - attach a PinChange Interrupt to our on the rising edge - link to coinacceptor function
  37. attachInterrupt(0, hopper, FALLING); // Hopper - attach a PinChange Interrupt to our pin on the falling edge - link to hopper function
  38.  
  39. }
  40.  
  41. void loop()
  42. {
  43. //CHECK NOW TO SEE WHICH COIN IS INSERTED
  44. if (coinPulseCount >0 && millis()- pulseTime > pulseThreshold) //if there is a coin count & the time between now and the last pulse is greater than 1/4 of a second - THE END OF BANK OF PULSES
  45. {
  46. newCoinInserted = coinPulseCount; //new variable to free up coinPulseCount on the interrupt.
  47. Serial.print("newCoinInserted pulses ");
  48. Serial.println(newCoinInserted); // print the pulses of the new coin inserted.
  49.  
  50. coinPulseCount = 0; // clear pulse count ready for a new pulse on the interrupt.
  51.  
  52. }
  53.  
  54. //Proccess the coin inserted
  55.  
  56. switch (newCoinInserted) {
  57.  
  58. case 1:
  59. Serial.println("£1 inserted"); //1 pulse from validator - £1 INSERTED
  60. newCoinInserted = 0; //reset - ALTHOUGH DO I NEED TO DO THIS?
  61. coinValue = 10; //hopper dispense 10 pulses/coins
  62. dispence();
  63. break;
  64.  
  65. /* IGNORE CASE 2 and 3 - this is harder as multiple coins need inserting to dispence 1x10p! OUCH :-(
  66. case 2: //2 pulses from Validor - 2p INSERTED
  67. Serial.println("2p inserted");
  68. newCoinInserted = 0;
  69. twoPence++; //variable to hold two pence count - twoPence needs to get to 5 to dispence
  70. if (twoPence == 5)
  71. {
  72. coinValue = 1;
  73. dispence();
  74. twoPence = 0; // reset twoPence variable
  75. }
  76. break;
  77.  
  78. case 3:
  79. Serial.println("5p inserted");
  80. newCoinInserted = 0;
  81. fivePence++;
  82. if (fivePence == 2)
  83. {
  84. coinValue = 1;
  85. dispence();
  86. fivePence = 0;
  87. }
  88. break;
  89. */
  90. case 4:
  91. Serial.println("10p inserted");
  92. newCoinInserted = 0;
  93. coinValue =1;
  94. coinPulseCount = 0; // clear pulse count ready for a new pulse on the interrupt.
  95. dispence();
  96.  
  97. break;
  98.  
  99. case 5:
  100. Serial.println("20p inserted");
  101. newCoinInserted = 0;
  102. coinValue = 2;
  103. dispence();
  104. break;
  105.  
  106. case 6:
  107. Serial.println("50p inserted");
  108. newCoinInserted = 0;
  109. coinValue = 5;
  110. dispence();
  111. break;
  112. }
  113. }
  114. //*****INTERUPT detecting pulses from the coin acceptor
  115. void coinacceptor() //Function called when coin enters coin acceptor
  116. {
  117. coinPulseCount++;
  118. pulseTime = millis(); //store current time in pulseTime
  119. }
  120.  
  121. //******INTERUPT detecting pulses from the hopper
  122. void hopper() //function called when a coin is ejected from the hopper
  123. {
  124. hopperPulseCount++ ;
  125. }
  126.  
  127. void dispence()
  128. {
  129. digitalWrite(relayPin, LOW); //turn on relay - active LOW.
  130. delay(50);
  131. hopperPulseCount =0;
  132.  
  133. while (hopperPulseCount < coinValue)
  134. {
  135. //do nothing and wait with the relay on dispencing coins until it hits the "coinValue"
  136. }
  137. delay(50); //wait to ensure the coin has enough momentum to leave hopper but not long enough for another coins to dispence!
  138. digitalWrite(relayPin, HIGH); //turn off relay - active LOW.
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement