// C++ code
// Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include<ESP32Servo360.h> //error in library
// Create servo objects
Servo servo1;
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x32 for a 16 chars and 2 line display
// Define servo pins
#define SERVO_PIN_1 9
#define SERVO_PIN_2 10
#define SERVO_PIN_3 11
// Create servo objects
//Servo servo1;
//Servo servo2;
//Servo servo3;
// Define other pins
#define coinSlot 6
#define coinSlotRGBFrame 2
#define buzzer 13
#define button1 4
#define button2 7
#define button3 8
#define Enter 3
#define back 12
char lcdBuffer[16];
char prefix[] = "Bal: Php";
int pulse = 0;
char ZZ[] = ".00";
int phase = 1;
// Define variant prices
const int prices[] = {5, 8, 10}; // Replace with the actual prices for variants
const int buttons[] = {button1, button2, button3};
//Servo* servos[] = {&servo1, &servo2, &servo3};
// Function prototypes
void setup();
void loop();
void setup();
void loop();
void insertcoinpage();
void startlcd();
void checkCredits();
void printCredits();
void dispenseVariant(int index);
bool nextPhase();
bool Cancel();
void printSelectVariant();
// void activateServo(Servo* servo);
void activateBuzzer();
// Setup, initialize everything here
void setup()
{
startlcd(); //done
pinMode(coinSlot, INPUT_PULLUP); //done
pinMode(Enter, INPUT); //done
pinMode(back, INPUT); //done
for (int i = 0; i < 3; i++) { //done
pinMode(buttons[i], INPUT); // Initialize button pins //done
}
// Attach and set initial position of servos to 0 degrees
//Servo* servoArray[] = {&servo1, &servo2, &servo3};
//int servoPins[] = {SERVO_PIN_1, SERVO_PIN_2, SERVO_PIN_3};
//for (int i = 0; i < 3; i++) {
// servoArray[i]->attach(servoPins[i]);
// servoArray[i]->write(0); // Set initial position to 0 degrees
//}
insertcoinpage();// done
}
// MAIN LOOP
void loop() {
while (phase == 1) {
checkCredits();
if (nextPhase()) break;
}
while (phase == 2) {
printSelectVariant();
for (int i = 0; i < 3; i++) { // Loop to handle multiple variants
dispenseVariant(i);
}
if (Cancel()) {
insertcoinpage(); // Reinitialize the insert coin page
break;
}
}
}
void insertcoinpage() // Function: prints "insert coin" //done
{
lcd.setCursor(0, 0);
lcd.print(" ");
delay(200);
lcd.setCursor(0, 0);
lcd.print(" Insert Coin");
delay(100);
printCredits();
}
void startlcd() // Function: turns on the LCD
{
lcd.init();
lcd.backlight();
lcd.clear();
}
void checkCredits() // Function: coins to credits and prints actual credits
{
if (digitalRead(coinSlot) == LOW) {
pulse++;
printCredits();
activateBuzzer();
}
}
void printCredits() // Function: only prints credits
{
sprintf(lcdBuffer, "%s %d%s", prefix, pulse, ZZ);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
}
void dispenseVariant(int index) // Function: handles variant dispensing
{
if (digitalRead(buttons[index]) == HIGH) {
if (pulse >= prices[index]) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing...");
delay(500);
pulse -= prices[index];
//activateServo(servos[index]);
printCredits();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Insufficient");
lcd.setCursor(0, 1);
lcd.print(" Balance ");
delay(500);
lcd.clear();
phase = 1;
insertcoinpage();
printCredits();
}
}
}
bool nextPhase() // Function: moves to the next phase
{
if (digitalRead(Enter) == HIGH) {
phase += 1;
return true; // Return true to indicate phase change
}
return false;
}
bool Cancel() // Function: cancels the operation
{
if (digitalRead(back) == HIGH) {
phase = 1;
return true; // Return true to indicate cancellation
}
return false;
}
void printSelectVariant() // Function: prints "Select Variant"
{
lcd.setCursor(0, 0);
lcd.print("Select Variant");
delay(100);
lcd.setCursor(0, 0);
lcd.print(" ");
delay(100);
}
//void activateServo(Servo* servo) // Function: activates a servo
//{
// servo->write(90); // Example position for activation
// delay(500); // Time for the servo to stay in position
// servo->write(0); // Return to initial position
//}
void activateBuzzer() // Function: activates the buzzer
{
tone(buzzer, 3000); // Set the buzzer to sound at 3000 Hz
delay(20); // Sound for 20 milliseconds
noTone(buzzer); // Stop the buzzer
delay(40); // Delay for 40 milliseconds
}