#include <LiquidCrystal_I2C.h> // includes the LiquidCrystal Library
#include <Servo.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
Servo servo1, servo2, servo3, servo4; // DS04-NFC motors
#define coinDetector 12
#define button1 2
#define button2 3
#define button3 4
#define button4 5
int con = 0;
int buttonPressed;
void setup() {
servo1.attach(6);
servo2.attach(9);
servo3.attach(10);
servo4.attach(11);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
}
void loop() {
// Print "Insert a coin!" on the LCD
lcd.init();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insert a coin!");
lcd.setCursor(0, 1);
lcd.print("Insert slowly!");
// Wait until a coin is detected
while (true) {
if (digitalRead(coinDetector) == LOW) { // If a coin is detected, exit the from the while loop
if (con == 1) {
con = 0;
break;
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Coin inserted!");
con = 1;
delay(500);
}
}
}
delay(10);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
lcd.setCursor(0, 1);
lcd.print(" 1, 2, 3 or 4?");
// Wait until a button is pressed
while (true) {
if (digitalRead(button1) ==LOW) {
buttonPressed = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You selected 1!");
delay(2000);
break;
}
if (digitalRead(button2) == LOW) {
buttonPressed = 2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You selected 2!");
delay(2000);
break;
}
if (digitalRead(button3) == LOW) {
buttonPressed = 3;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You selected 3!");
delay(2000);
break;
}
if (digitalRead(button4) == LOW) {
buttonPressed = 4;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You selected 4!");
delay(2000);
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
switch (buttonPressed) {
case 1:
// Rotate the helical coil, discharge the selected item
servo1.writeMicroseconds(3600); // rotate
delay(1290);
servo1.writeMicroseconds(1500); // stop
delay(300);
break;
case 2:
// Rotate the helix, push the selected item
servo2.writeMicroseconds(3600); // rotate
delay(950);
servo2.writeMicroseconds(1500); // stop
delay(300);
break;
case 3:
// Rotate the helix, push the selected item
servo3.writeMicroseconds(3600); // rotate
delay(1250);
servo3.writeMicroseconds(1500); // stop
delay(300);
break;
case 4:
delay(200);
// Rotate the helix, push the selected item
servo4.writeMicroseconds(3600); // rotate
delay(830);
servo4.writeMicroseconds(1500); // stop
delay(300);
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(2000);
}