#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <HX711_ADC.h> // need to install
#define LCD_ADDRESS 0x27 // I2C address of your LCD
#define LCD_COLUMNS 16 // Number of columns in your LCD
#define LCD_ROWS 2 // Number of rows in your LCD
HX711_ADC LoadCell(A1, A0); // parameters: dt pin A1, sck pin A0;
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
#define BUZZER_PIN A3
// Button pin
#define BUTTON_PIN A2
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
struct Product {
int code; // Four-digit product code
String name;
float weight;
float price;
};
float totalPrice = 0;
float totalWeight = 0;
Product products[] = {
{1234, "AMUL MILK", 500, 80},
{2345, "RIN SOAP", 160, 10},
{3456, "SUGAR", 200, 60},
// Add more products as needed
};
struct CartItem {
String name;
float weight;
float price;
};
CartItem cart[10]; // Array to store cart items
int cartItemCount = 0; // Counter for cart items
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight();
LoadCell.begin(); // start connection to HX711
LoadCell.start(2000); // load cells gets 2000ms of time to stabilize
LoadCell.setCalFactor(-211.106); // calibration factor for load cell => dependent on your individual setup
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
pinMode(BUZZER_PIN, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("WELCOME");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code: ");
//digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer on
}
void displayCart() {
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value
//float totalPrice = 0;
//float totalWeight = 0;
if (i -totalWeight >= 5) {
digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer on
delay(2000); // Wait for 1 second
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
} else if (i - totalWeight <= -5) {
digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer on
delay(2000); // Wait for 1 second
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
} else if (i - totalWeight == -5) {
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
if (digitalRead(BUTTON_PIN) == HIGH) {
lcd.clear();
lcd.print("scan your card");
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cart Items:");
delay(1000);
for (int i = 0; i < cartItemCount; i++) {
lcd.clear();
lcd.setCursor(0, i + 1);
lcd.setCursor(0, 0);
lcd.print(cart[i].name);
//lcd.print(": RS");
//delay(2000);
lcd.setCursor(0, 1);
lcd.print(cart[i].price);
lcd.print("RS");
//lcd.print(", ");
//delay(2000);
lcd.setCursor(8, 1);
lcd.print(cart[i].weight);
lcd.println(" g");
delay(2000);
totalPrice += cart[i].price;
totalWeight += cart[i].weight;
}
// Display total price and weight
lcd.clear();
lcd.setCursor(0, cartItemCount + 1);
lcd.setCursor(0, 0);
lcd.print("TotalRs: Rs");
lcd.print(totalPrice);
//delay(2000);
lcd.setCursor(1, cartItemCount + 2);
lcd.setCursor(0, 1);
lcd.print("TotalW: ");
lcd.print(totalWeight);
lcd.print("g");
delay(2000);
}
void loop() {
LoadCell.update(); // retrieves data from the load cell
float i = LoadCell.getData(); // get output value
//float totalPrice = 0;
//float totalWeight = 0;
if (i -totalWeight >= 5) {
digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer on
delay(2000); // Wait for 1 second
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
} else if (i - totalWeight <= -5) {
digitalWrite(BUZZER_PIN, HIGH); // Turn the buzzer on
delay(2000); // Wait for 1 second
digitalWrite(BUZZER_PIN, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
}
else if (i - totalWeight == -5) {
digitalWrite(BUZZER_PIN, LOW);
// Turn the buzzer off
if (digitalRead(BUTTON_PIN) == HIGH) {
lcd.clear();
lcd.print("scan your card");
}
}
char key = keypad.getKey();
static String enteredCode = "";
if (key != NO_KEY && enteredCode.length() < 4) {
enteredCode += key;
lcd.setCursor(0, 1);
lcd.print(enteredCode);
}
if (enteredCode.length() == 4) {
int productIndex = -1;
int code = enteredCode.toInt();
for (int i = 0; i < sizeof(products) / sizeof(products[0]); i++) {
if (code == products[i].code) {
productIndex = i;
break;
}
}
if (productIndex != -1) {
lcd.clear();
lcd.setCursor(0, 0);
//lcd.print("Product: ");
lcd.print(products[productIndex].name);
lcd.setCursor(0, 1);
//lcd.print("Weight: ");
lcd.print(products[productIndex].weight);
lcd.print("g");
lcd.setCursor(9, 1);
//lcd.print("Price: $");
lcd.print(products[productIndex].price);
lcd.print("Rs");
// Wait for key input
while (true) {
char inputKey = keypad.getKey();
if (inputKey == 'A') {
lcd.clear();
lcd.print("Product Added");
delay(1000);
// Multiply by 2 if A is pressed twice
if (keypad.getKey() == 'A') {
lcd.clear();
lcd.print("Product Added");
delay(1000);
products[productIndex].price *= 2;
products[productIndex].weight *= 2;
}
// Add product to cart
cart[cartItemCount].name = products[productIndex].name;
cart[cartItemCount].weight = products[productIndex].weight;
cart[cartItemCount].price = products[productIndex].price;
cartItemCount++;
break;
} else if (inputKey == 'D') {
lcd.clear();
lcd.print("Product Removed");
delay(1000);
// Divide by 2 only if previously multiplied by 2
if (keypad.getKey() == 'D' && products[productIndex].price > products[productIndex].price / 2 && products[productIndex].weight > products[productIndex].weight / 2) {
products[productIndex].price /= 2;
products[productIndex].weight /= 2;
lcd.clear();
lcd.print("Product Removed");
delay(1000);
}
// Remove product from cart
for (int i = 0; i < cartItemCount; i++) {
if (cart[i].name == products[productIndex].name) {
cartItemCount--;
for (int j = i; j < cartItemCount; j++) {
cart[j] = cart[j + 1];
}
break;
}
}
break;
} else if (inputKey == 'B') {
// Go back to entering code
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter code: ");
enteredCode = "";
break;
} else if (inputKey == 'C') {
// Display cart items
displayCart();
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cart Items:");
//enteredCode = "";
break;
}
}
}// Compare product weight with threshold
else {
lcd.clear();
lcd.print("PRODUCTNOTFOUND");
enteredCode = ""; // Reset entered code
}
//enteredCode = ""; // Reset entered code
}
}