// C++ code
// Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // done
#include <Servo.h> // converted to esp32servo360.h
// Initialize the LCD //done
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x32 for a 16 chars and 2 line display //done
// Define servo pins //done
#define SERVO_PIN_1 9 //done
#define SERVO_PIN_2 10 //done
#define SERVO_PIN_3 11 //done
// Create servo objects
Servo servo1; //commented
Servo servo2; //commented
Servo servo3; //commented
// Define other pins //done
#define coinSlot 6 //done
#define coinSlotRGBFrame 2 //done
#define buzzer 13 //done
#define button1 4 //done
#define button2 7 //done
#define button3 8 //done
#define Enter 3 //done
#define back 12 //done
char lcdBuffer[16]; //done
char prefix[] = "Bal: Php"; //done
int pulse = 0; //done
char ZZ[] = ".00"; //done
int phase = 1; //done
// Define variant prices
const int prices[] = {5, 8, 10}; // Replace with the actual prices for variants //done
const int buttons[] = {button1, button2, button3}; //done
Servo* servos[] = {&servo1, &servo2, &servo3}; //done
// Function prototypes
void setup(); //done
void loop(); //done
void insertcoinpage(); //done
void startlcd(); //done
void checkCredits(); //done
void printCredits(); //done
void dispenseVariant(int index);
bool nextPhase(); // done
bool Cancel(); //done
void printSelectVariant(); //done
void activateServo(Servo* servo); //commented
void activateBuzzer(); //done
// 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}; //commented
int servoPins[] = {SERVO_PIN_1, SERVO_PIN_2, SERVO_PIN_3}; //commented
for (int i = 0; i < 3; i++) { //commented
servoArray[i]->attach(servoPins[i]); //commented
servoArray[i]->write(0); // Set initial position to 0 degrees //commented
} //commented
insertcoinpage(); //done
}
// MAIN LOOP
void loop() { //done
while (phase == 1) { //done
checkCredits(); //done
if (nextPhase()) break; //done
} //done
while (phase == 2) { //done
printSelectVariant(); //done
for (int i = 0; i < 3; i++) { // Loop to handle multiple variants //done
dispenseVariant(i); //done
} //done
if (Cancel()) { //done
insertcoinpage(); // Reinitialize the insert coin page //done
break; //done
} //done
} //done
} //done
// Functions
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 //done
{
lcd.init();
lcd.backlight();
lcd.clear();
}
void checkCredits() // Function: coins to credits and prints actual credits //done
{
if (digitalRead(coinSlot) == LOW) {
pulse++;
printCredits();
activateBuzzer();
}
}
void printCredits() // Function: only prints credits //done
{
sprintf(lcdBuffer, "%s %d%s", prefix, pulse, ZZ);
lcd.setCursor(0, 1);
lcd.print(lcdBuffer);
}
void dispenseVariant(int index) // Function: handles variant dispensing //done
{
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]); //commented
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 //done
{
if (digitalRead(Enter) == HIGH) {
phase += 1;
return true; // Return true to indicate phase change
}
return false;
}
bool Cancel() // Function: cancels the operation //done
{
if (digitalRead(back) == HIGH) {
phase = 1;
return true; // Return true to indicate cancellation
}
return false;
}
void printSelectVariant() // Function: prints "Select Variant" //done
{
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 //commented
{
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 //done
{
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
}