Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <Wire.h>
- #include <LCD.h>
- #include <LiquidCrystal_I2C.h>
- // Arduino coin acceptor code by Kuma McCraw
- // Originally created for the Live Oak School Vending Machine
- // Read your coin acceptor's specs and instructions first for hookup specifics
- // Modifications to this code may be required for operation
- // Coin acceptor model used in this example is CH-926
- //
- // xoxox
- /* Constants
- cost of 1 corresponds to the cost of purchasing the invetory in slot 1
- cost of 2 is the cost of the inventory in slot 2
- cost of 3 is the cost of inventory in slot 3
- cost of 4 is the cost of inventory in slot 4
- */
- /*The circuit
- LCD Enable pin to digital pin 11
- LCD D4 pin to digital pin 5
- LCD D5 pin to digital pin 4
- LCD D6 pin to digital pin 3
- LCD D7 pin to digital pin 2
- LCD R/W pin to ground
- 10K resistor:
- ends to +5V and ground
- wiper to LCD VO pin (pin 3)
- */
- // Servo's for each slot
- LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- Servo slot1;
- Servo slot2;
- Servo slot3;
- // Variables
- int servopo3 = 0;
- int servopo2 = 0;
- int servopo1 = 0;
- const int coinpin = 2;
- const int cost1 = 100;
- const int button1 = 3;
- const int button2 = 4;
- const int button3 = 5;
- volatile int cents = 0;
- volatile int pulses;
- int credits = 0;
- void setup() {
- slot1.attach(8);
- slot2.attach(7);
- slot3.attach(6);
- lcd.begin(16,2);
- lcd.clear();
- lcd.noAutoscroll();
- lcd.backlight();
- pinMode(button1, INPUT);
- pinMode(button2, INPUT);
- pinMode(button3, INPUT);
- attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
- }
- void coinInterrupt()
- {
- pulses += 1;
- mydelay(1);
- }
- void translate(int pulses)
- {
- int rc = pulses * 5;
- cents += rc;
- }
- void mydelay(float ss)
- {
- int ms = 1000 * ss;
- return delay(ms);
- }
- // Main loop
- void loop()
- {
- mydelay(5);
- translate(pulses);
- pulses = 0;
- int button1state = digitalRead(button1);
- int button2state = digitalRead(button2);
- int button3state = digitalRead(button3);
- if (credits >= 2)
- {
- lcd.clear();
- lcd.print("to get item 1 ");
- lcd.setCursor(0,1);
- lcd.print("press button 1");
- if (button1state == HIGH)
- {
- // if button which correponds to that slot is pushed activate dispensing motor.
- slot1.writeMicroseconds(servopo1);
- credits -= 2;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("bought item 1");
- mydelay(0.75);
- slot1.writeMicroseconds(1500);
- }
- }
- if (credits >= 2)
- {
- lcd.clear();
- lcd.print("to get item 2 ");
- lcd.setCursor(0,1);
- lcd.print("press button 2");
- if (button2state == HIGH)
- {
- slot2.writeMicroseconds(servopo2);
- credits -= 2;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("you bought item 2");
- mydelay(0.75);
- slot2.writeMicroseconds(1500);
- }
- //if button which corresponds to that slot is pushed, activate dispensing motor for that slot.
- }
- if (credits >= 3)
- {
- lcd.clear();
- lcd.print("to get item 3 ");
- lcd.setCursor(0,1);
- lcd.print("press button 3");
- if (button3state == HIGH)
- {
- //move correspoding button specific amount of rotations
- slot3.writeMicroseconds(servopo3);
- credits -= 3;
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("you bought item 3");
- delay(1000);
- slot3.writeMicroseconds(1500);
- }
- // if button which correponds to that slot is pushed activate dispensing motor.
- }
- //If we've hit our target amount of coins, increment our credits and reset the cents counter
- if (cents >= cost1)
- {
- credits = credits + 1;
- cents = cents - cost1;
- }
- // If we haven't reached our target, keep waiting and display, "not enough money."
- else
- {
- if (credits == 0) {
- lcd.clear();
- lcd.print("not enough money");
- }
- }
- // Debugging zone
- lcd.clear();
- lcd.print(cents);
- lcd.print(" cents || ");
- lcd.setCursor(0,1);
- lcd.print(credits);
- lcd.print(" credit(s) ||");
- mydelay(1);
- // if the amount of credits needed for
- }
Add Comment
Please, Sign In to add comment