#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin2 = 3;
const int buttonPin3 = 4; // Add button 3
const int buttonPin4 = 5; // Add button 4
int selectedProduct = 0;
bool isButtonPressed = false;
int led = 11;
String productNames[] = {"Fried", "Pizza", "Pasta", "Salad", "Drink"};
float productPrices[] = {6, 8, 7, 4, 1};
int menu = 0; // 0 for product details, 1 for shopping cart
String cartProducts[5];
float cartPrices[5];
int cartItemCount = 0;
int currentCartItem = -1; // Giữ thông tin đồ ăn hiện tại
int currentCartItemIndex = 0;
void setup() {
lcd.init();
lcd.backlight();
pinMode(led, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP); // Initialize button 3
pinMode(buttonPin4, INPUT_PULLUP); // Initialize button 4
}
void loop() {
if (digitalRead(buttonPin2) == LOW) {
menu = (menu + 1) % 3; // Switch between 0 and 1
delay(200); // Debouncing delay
}
if (menu == 0) {
displayWelcomeScreen();
} else if (menu == 1) {
handleButton3Press();
} else if (menu == 2) {
// Shopping cart menu
// Add logic for shopping cart menu
displayShoppingCart();
}
}
void displayWelcomeScreen() {
lcd.setCursor(2, 0);
lcd.print("Welcome to ");
lcd.setCursor(0, 1);
lcd.print("FastFood Chill");
}
void handleButton3Press() {
lcd.clear();
while (digitalRead(buttonPin2) == HIGH) {
if (digitalRead(buttonPin3) == HIGH) {
// Increment the selected product index
selectedProduct = (selectedProduct + 1) % 5;
// Display the details of the newly selected food item
lcd.setCursor(0, 0);
lcd.print("Food: ");
lcd.print(productNames[selectedProduct]);
lcd.setCursor(0, 1);
lcd.print("Price: $");
lcd.print(productPrices[selectedProduct], 2);
delay(10);
// Wait until button 3 is released
while (digitalRead(buttonPin3) == HIGH) {
// Check if button 2 is pressed to exit the loop
if (digitalRead(buttonPin2) == LOW) {
return;
}
if (digitalRead(buttonPin4) == LOW) {
lcd.setCursor(0, 0);
lcd.print("Added to Cart");
lcd.setCursor(0, 1);
lcd.print("Press 4 for Details");
// Add your logic to store the item in the cart array
cartProducts[currentCartItemIndex] = productNames[selectedProduct];
cartPrices[currentCartItemIndex] = productPrices[selectedProduct];
currentCartItemIndex++;
cartItemCount++;
// Wait until button 4 is released
while (digitalRead(buttonPin4) == LOW) {
delay(10);
}
delay(500); // Display the message for 2 seconds
// Clear the screen and go back to the food details
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Food: ");
lcd.print(productNames[selectedProduct]);
lcd.setCursor(0, 1);
lcd.print("Price: $");
lcd.print(productPrices[selectedProduct], 2);
}
delay(10);
}
}
}
}
void displayShoppingCart() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Shopping Cart:");
// Display items in the cart
for (int i = 0; i < cartItemCount; ++i) {
lcd.setCursor(0, i + 1);
lcd.print(cartProducts[i]);
lcd.setCursor(10, i + 1);
lcd.print("$");
lcd.print(cartPrices[i], 2);
}
// Display total price
lcd.setCursor(0, cartItemCount + 1);
lcd.print("Total: $");
float total = 0;
for (int i = 0; i < cartItemCount; ++i) {
total += cartPrices[i];
}
lcd.print(total, 2);
delay(500); // Display the shopping cart for 0.5 seconds
// Check if button 2 is pressed to exit the loop
while (digitalRead(buttonPin2) == HIGH) {
delay(10);
}
// Clear the screen and go back to the food details
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Food: ");
lcd.print(productNames[selectedProduct]);
lcd.setCursor(0, 1);
lcd.print("Price: $");
lcd.print(productPrices[selectedProduct], 2);
}