void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo motor;
int paymentButton = 2;
int productButton = 3;
int balance = 0;
int price = 100;
void setup() {
lcd.init();
lcd.backlight();
motor.attach(9);
motor.write(0);
pinMode(paymentButton, INPUT_PULLUP);
pinMode(productButton, INPUT_PULLUP);
lcd.setCursor(0, 0);
lcd.print("VENDING MACHINE");
delay(2000);
lcd.clear();
lcd.print("Insert token");
}
void loop() {
if (digitalRead(paymentButton) == LOW) {
balance += 100;
lcd.clear();
lcd.print("Balance: ");
lcd.print(balance);
delay(500);
while (digitalRead(paymentButton) == LOW) {}
}
if (digitalRead(productButton) == LOW) {
lcd.clear();
if (balance >= price) {
lcd.print("Dispensing...");
motor.write(90);
delay(1000);
motor.write(0);
balance -= price;
lcd.clear();
lcd.print("Take your item");
delay(2000);
} else {
lcd.print("Insufficient");
lcd.setCursor(0, 1);
lcd.print("Insert token");
delay(2000);
}
lcd.clear();
lcd.print("Balance: ");
lcd.print(balance);
delay(500);
while (digitalRead(productButton) == LOW) {}
}
}
}