#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servos[4]; //servo identities
#define button1 2 //----------------
#define button2 3 //| BUTTON |
#define button3 4 //| PIN NUMBERS |
#define button4 5 //----------------
#define coinSlot 6 // coin slot counter wire to pin 6
#define LED 7 // LED Anode to pin 7 for processing indicator
#define vending 8 // vendin button to pin 8
int itemvalue1 = 10; // Replace 10 with the actual value for item #1
int itemvalue2 = 20; // Replace 20 with the actual value for item #2
int itemvalue3 = 30; // Replace 30 with the actual value for item #3
int itemvalue4 = 40; // Replace 40 with the actual value for item #4
const int numItems = 4;
int itemValues[numItems] = {itemvalue1, itemvalue2, itemvalue3, itemvalue4};
int buttonPins[numItems] = {button1, button2, button3, button4};
int buttonStatus;
int coinBalance;
int pulse;
int coinSlotStatus;
int buttonPressed;
int pulseValue = pulse;
int selectedMenuItem; // Track the selected item
int vendingStatus;
boolean userBalance = false;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" MARVIN");
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" Ma. VICTORIA");
delay(50);
digitalWrite(LED, HIGH);
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" PLEASE PRESS");
lcd.setCursor(0, 1);
lcd.print(" ITEM BUTTON");
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(coinSlot, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(vending, INPUT_PULLUP);
servos[0].attach(30); // Attach servo1 to pin 30
servos[1].attach(31); // Attach servo2 to pin 31
servos[2].attach(32); // Attach servo3 to pin 32
servos[3].attach(33); // Attach servo4 to pin 33
}
void loop() {
// Loop through the items
//Wait until one of the item buttons is pressed.
for (int i = 0; i < numItems; i++) {
int buttonStatus = digitalRead(buttonPins[i]);
if (buttonStatus == 0) {
buttonPressed = 1;
selectedMenuItem = i; // Track the selected item
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" ITEM #" + String(i + 1, DEC));
lcd.setCursor(0, 1);
lcd.print(" IS SELECTED");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" INSERT COINS");
lcd.setCursor(0, 1);
lcd.print(" PHP ");
lcd.setCursor(8, 1);
lcd.print(itemValues[i]);
}
}
//After one of the buttons is pressed, execute the LCD display.
//INSERT COIN
if (buttonPressed == 1) { // If no button is pressed, coinslot is disabled.
coinSlotStatus = digitalRead(coinSlot); // wait to insert coins.
delay(30);
if (coinSlotStatus == LOW) {
digitalWrite(LED, LOW);
userBalance = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" COINS INSERTED");
pulse += 1;
char lcdBuffer[16];
sprintf(lcdBuffer, "Bal. PHP %d.00", pulse);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
delay(30);
}
} // INSERT COINS ends here.
// VENDING the item selected
vendingStatus = digitalRead(vending);
if (vendingStatus == 0 && buttonPressed == 1 && pulse >= itemValues[selectedMenuItem]) {
coinBalance = pulse - itemValues[selectedMenuItem];
buttonPressed = 0;
userBalance = false;
servos[selectedMenuItem].write(180); // Rotate the selected servo
delay(2000);
servos[selectedMenuItem].write(0); // Stop rotating the selected servo
vendingDisplay();
pulse = coinBalance;
}
}
void vendingDisplay() {
lcd.clear();
delay(500);
char lcdBuffer[16];
sprintf(lcdBuffer, " Bal. PHP%d.00", coinBalance);
lcd.setCursor(0, 0);
lcd.print(" ITEM VENDED");
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" THANK YOU");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" PLEASE PRESS");
lcd.setCursor(0, 1);
lcd.print(" ITEM BUTTON");
}