#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Crear el objeto lcd dirección 0x27 y 16 columnas x 2 filas
/* Servo motor (DS04-NFC motors) */
Servo servo1, servo2, servo3, servo4, servo5;
Servo servo6, servo7, servo8, servo9, servo10;
/* Servo motor (DS04-NFC motors) */
#define coinDetector 21
/* Botones */
#define button1 19
#define button2 18
#define button3 17
#define button4 16
#define button5 15
#define button6 14
#define button7 0
#define button8 1
#define button9 2
#define button10 3
/* Botones */
int buttonPressed;
void setup() {
Wire.begin(); // Inicializa la comunicación I2C
lcd.init(); // Inicializar el LCD
lcd.backlight(); //Encender la luz de fondo.
servo1.attach(13);
servo2.attach(12);
servo3.attach(11);
servo4.attach(10);
servo5.attach(9);
servo6.attach(8);
servo7.attach(7);
servo8.attach(6);
servo9.attach(5);
servo10.attach(4);
pinMode(coinDetector, INPUT);
// Activación de las resistencias "pull up" de los pines digitales
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
pinMode(button6, INPUT_PULLUP);
pinMode(button7, INPUT_PULLUP);
pinMode(button8, INPUT_PULLUP);
pinMode(button9, INPUT_PULLUP);
pinMode(button10, INPUT_PULLUP);
}
void loop() {
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insert a coin!");
// Wait until a coin is detected
while (true) {
if (digitalRead(coinDetector) == LOW) { // If a coin is detected, exit the from the while loop
break;
}
}
delay(10);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selecciona del:");
lcd.setCursor(0, 1);
lcd.print("1 - 10");
// Wait until a button is pressed
while (true) {
if (digitalRead(button1) == LOW) {
buttonPressed = 1;
break;
}
if (digitalRead(button2) == LOW) {
buttonPressed = 2;
break;
}
if (digitalRead(button3) == LOW) {
buttonPressed = 3;
break;
}
if (digitalRead(button4) == LOW) {
buttonPressed = 4;
break;
}
if (digitalRead(button5) == LOW) {
buttonPressed = 5;
break;
}
if (digitalRead(button6) == LOW) {
buttonPressed = 6;
break;
}
if (digitalRead(button7) == LOW) {
buttonPressed = 7;
break;
}
if (digitalRead(button8) == LOW) {
buttonPressed = 8;
break;
}
if (digitalRead(button9) == LOW) {
buttonPressed = 9;
break;
}
if (digitalRead(button10) == LOW) {
buttonPressed = 10;
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
// Depending on the pressed button, move the carrier to that position and discharge the selected item
switch (buttonPressed) {
case 1:
// Rotate the helical coil, discharge the selected item
servo1.writeMicroseconds(2000); // rotate
delay(950);
servo1.writeMicroseconds(1500); // stop
break;
case 2:
// Rotate the helix, push the selected item
servo2.writeMicroseconds(2000); // rotate
delay(950);
servo2.writeMicroseconds(1500); // stop
break;
case 3:
// Rotate the helix, push the selected item
servo3.writeMicroseconds(2000); // rotate
delay(950);
servo3.writeMicroseconds(1500); // stop
break;
case 4:
// Rotate the helix, push the selected item
servo4.writeMicroseconds(2000); // rotate
delay(950);
servo4.writeMicroseconds(1500); // stop
break;
case 5:
// Rotate the helix, push the selected item
servo5.writeMicroseconds(2000); // rotate
delay(950);
servo5.writeMicroseconds(1500); // stop
break;
case 6:
// Rotate the helix, push the selected item
servo6.writeMicroseconds(2000); // rotate
delay(950);
servo6.writeMicroseconds(1500); // stop
break;
case 7:
// Rotate the helix, push the selected item
servo7.writeMicroseconds(2000); // rotate
delay(950);
servo7.writeMicroseconds(1500); // stop
break;
case 8:
// Rotate the helix, push the selected item
servo8.writeMicroseconds(2000); // rotate
delay(950);
servo8.writeMicroseconds(1500); // stop
break;
case 9:
// Rotate the helix, push the selected item
servo9.writeMicroseconds(2000); // rotate
delay(950);
servo9.writeMicroseconds(1500); // stop
break;
case 10:
// Rotate the helix, push the selected item
servo10.writeMicroseconds(2000); // rotate
delay(950);
servo10.writeMicroseconds(1500); // stop
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(2000);
}