#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
#define LED_PIN 13
#define SCROLL_BUTTON_PIN 8
#define SELECT_BUTTON_PIN 9
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2);
float minWeightThreshold = 100.0; // Minimum weight threshold in grams
int currentIngredientIndex = 0;
const char* ingredients[] = {"Salt", "Rice", "Pulses", "Sugar", "Masalas"};
const int numIngredients = 5;
String selectedIngredient = "";
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(SCROLL_BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistors
pinMode(SELECT_BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistors
// Initialize the LCD and HX711
lcd.init();
lcd.backlight();
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare(); // Reset the scale to zero
displayIngredientSelection();
}
void loop() {
// Ingredient selection
if (digitalRead(SCROLL_BUTTON_PIN) == LOW) {
scrollIngredients();
delay(300); // Debounce delay
}
if (digitalRead(SELECT_BUTTON_PIN) == LOW) {
selectIngredient();
delay(300); // Debounce delay
}
// Weight monitoring only after ingredient is selected
if (selectedIngredient != "") {
float weight = scale.get_units(10); // Get average of 10 readings
// Display current weight
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(weight);
lcd.print("g "); // Ensure proper clearing
// Turn on LED if weight is below the threshold
if (weight < minWeightThreshold) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(1000); // Update weight every second
}
}
// Function to scroll through the ingredient list
void scrollIngredients() {
currentIngredientIndex = (currentIngredientIndex + 1) % numIngredients;
displayIngredientSelection();
}
// Function to select an ingredient
void selectIngredient() {
selectedIngredient = ingredients[currentIngredientIndex];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selected: ");
lcd.print(selectedIngredient);
delay(1000);
lcd.clear();
}
// Function to display the ingredient selection on the LCD
void displayIngredientSelection() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Ingredient:");
lcd.setCursor(0, 1);
lcd.print(ingredients[currentIngredientIndex]);
}