Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <Wire.h>
  3. #include <LCD.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6.  
  7.  
  8.  
  9. // Arduino coin acceptor code by Kuma McCraw
  10. // Originally created for the Live Oak School Vending Machine
  11. // Read your coin acceptor's specs and instructions first for hookup specifics
  12. // Modifications to this code may be required for operation
  13. // Coin acceptor model used in this example is CH-926
  14. //
  15. // xoxox
  16. /* Constants
  17. cost of 1 corresponds to the cost of purchasing the invetory in slot 1
  18. cost of 2 is the cost of the inventory in slot 2
  19. cost of 3 is the cost of inventory in slot 3
  20. cost of 4 is the cost of inventory in slot 4
  21. */
  22. /*The circuit
  23. LCD Enable pin to digital pin 11
  24. LCD D4 pin to digital pin 5
  25. LCD D5 pin to digital pin 4
  26. LCD D6 pin to digital pin 3
  27. LCD D7 pin to digital pin 2
  28. LCD R/W pin to ground
  29. 10K resistor:
  30. ends to +5V and ground
  31. wiper to LCD VO pin (pin 3)
  32.  
  33. */
  34. // Servo's for each slot
  35. LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  36. Servo slot1;
  37. Servo slot2;
  38. Servo slot3;
  39. // Variables
  40.  
  41. int servopo3 = 0;
  42. int servopo2 = 0;
  43. int servopo1 = 0;
  44.  
  45. const int coinpin = 2;
  46. const int cost1 = 100;
  47.  
  48. const int button1 = 3;
  49. const int button2 = 4;
  50. const int button3 = 5;
  51.  
  52. volatile int cents = 0;
  53.  
  54. volatile int pulses;
  55.  
  56. int credits = 0;
  57.  
  58. void setup() {
  59. slot1.attach(8);
  60. slot2.attach(7);
  61. slot3.attach(6);
  62. lcd.begin(16,2);
  63. lcd.clear();
  64. lcd.noAutoscroll();
  65. lcd.backlight();
  66. pinMode(button1, INPUT);
  67. pinMode(button2, INPUT);
  68. pinMode(button3, INPUT);
  69. attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
  70.  
  71. }
  72. void coinInterrupt()
  73. {
  74. pulses += 1;
  75. mydelay(1);
  76. }
  77.  
  78. void translate(int pulses)
  79. {
  80. int rc = pulses * 5;
  81. cents += rc;
  82. }
  83.  
  84. void mydelay(float ss)
  85. {
  86. int ms = 1000 * ss;
  87. return delay(ms);
  88. }
  89.  
  90. // Main loop
  91. void loop()
  92. {
  93. mydelay(5);
  94. translate(pulses);
  95. pulses = 0;
  96.  
  97. int button1state = digitalRead(button1);
  98. int button2state = digitalRead(button2);
  99. int button3state = digitalRead(button3);
  100.  
  101. if (credits >= 2)
  102. {
  103. lcd.clear();
  104. lcd.print("to get item 1 ");
  105. lcd.setCursor(0,1);
  106. lcd.print("press button 1");
  107.  
  108. if (button1state == HIGH)
  109. {
  110. // if button which correponds to that slot is pushed activate dispensing motor.
  111. slot1.writeMicroseconds(servopo1);
  112. credits -= 2;
  113. lcd.clear();
  114. lcd.setCursor(0,0);
  115. lcd.print("bought item 1");
  116. mydelay(0.75);
  117. slot1.writeMicroseconds(1500);
  118.  
  119. }
  120.  
  121. }
  122. if (credits >= 2)
  123. {
  124. lcd.clear();
  125. lcd.print("to get item 2 ");
  126. lcd.setCursor(0,1);
  127. lcd.print("press button 2");
  128.  
  129. if (button2state == HIGH)
  130. {
  131. slot2.writeMicroseconds(servopo2);
  132. credits -= 2;
  133. lcd.clear();
  134. lcd.setCursor(0,0);
  135. lcd.print("you bought item 2");
  136. mydelay(0.75);
  137. slot2.writeMicroseconds(1500);
  138.  
  139. }
  140. //if button which corresponds to that slot is pushed, activate dispensing motor for that slot.
  141. }
  142. if (credits >= 3)
  143. {
  144. lcd.clear();
  145. lcd.print("to get item 3 ");
  146. lcd.setCursor(0,1);
  147. lcd.print("press button 3");
  148.  
  149. if (button3state == HIGH)
  150. {
  151. //move correspoding button specific amount of rotations
  152. slot3.writeMicroseconds(servopo3);
  153. credits -= 3;
  154. lcd.clear();
  155. lcd.setCursor(0,0);
  156. lcd.print("you bought item 3");
  157. delay(1000);
  158. slot3.writeMicroseconds(1500);
  159.  
  160. }
  161. // if button which correponds to that slot is pushed activate dispensing motor.
  162. }
  163.  
  164. //If we've hit our target amount of coins, increment our credits and reset the cents counter
  165.  
  166. if (cents >= cost1)
  167. {
  168. credits = credits + 1;
  169. cents = cents - cost1;
  170. }
  171. // If we haven't reached our target, keep waiting and display, "not enough money."
  172. else
  173. {
  174. if (credits == 0) {
  175. lcd.clear();
  176. lcd.print("not enough money");
  177.  
  178. }
  179. }
  180.  
  181. // Debugging zone
  182. lcd.clear();
  183. lcd.print(cents);
  184. lcd.print(" cents || ");
  185. lcd.setCursor(0,1);
  186. lcd.print(credits);
  187. lcd.print(" credit(s) ||");
  188. mydelay(1);
  189.  
  190. // if the amount of credits needed for
  191.  
  192. }
Add Comment
Please, Sign In to add comment