#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <EEPROM.h>
// Pin definitions
#define DOUT 4
#define CLK 3
// EEPROM addresses
#define EEPROM_ITEM_NAME 0
#define EEPROM_ITEM_CODE 20
#define EEPROM_ITEM_WEIGHT 40
#define EEPROM_REORDER_LIMIT 44
#define EEPROM_BIN_WEIGHT 48
// HX711
HX711 scale;
// LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Variables
const float calibration=2.380;
const char pass[4] = "1234";
char itemName[20];
char itemCode[8];
float itemWeight;
int reorderLimit;
float binWeight;
float currentWeight;
int numberOfItems;
long timer;
bool limiter = false;
void setup() {
Serial.begin(9600);
Serial.print("Enter code: ");
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize HX711
scale.begin(DOUT, CLK);
// Load data from EEPROM
EEPROM.get(EEPROM_ITEM_NAME, itemName);
EEPROM.get(EEPROM_ITEM_CODE, itemCode);
//EEPROM.get(EEPROM_ITEM_WEIGHT, itemWeight);
EEPROM.get(EEPROM_REORDER_LIMIT, reorderLimit);
//EEPROM.get(EEPROM_BIN_WEIGHT, binWeight);
updateDisplay();
}
void loop() {
if (Serial.available() > 0) {
char code[4];
readSerialString(code, 6);
Serial.println(code);
if (pass[0] == code[0])
if (pass[1] == code[1])
if (pass[2] == code[2])
if (pass[3] == code[3])
{
Serial.println("Setting Mode");
handleSerialInput();
}
else
{
Serial.println("Wrong password!");
Serial.print("Enter Code: ");
}
else
{
Serial.println("Wrong password!");
Serial.print("Enter Code: ");
}
else
{
Serial.println("Wrong password!");
Serial.print("Enter Code: ");
}
else
{
Serial.println("Wrong password!");
Serial.print("Enter Code: ");
}
}
currentWeight = ((scale.get_units(10)*calibration) - binWeight);
numberOfItems = (int)(currentWeight / itemWeight);
updateDisplay();
if (numberOfItems < reorderLimit) {
if (timer < millis() - 4000)
{
lcd.noBacklight();
timer = millis();
limiter = true;
}
if (limiter == true)
{
if (timer < millis() - 1000)
{
lcd.backlight();
limiter = false;
timer = millis();
}
}
} else {
lcd.backlight();
}
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Item: ");
lcd.print(itemName);
lcd.setCursor(0, 1);
lcd.print("Code: ");
lcd.print(itemCode);
lcd.setCursor(0, 2);
lcd.print("Reorder Limit: ");
lcd.print(reorderLimit);
lcd.setCursor(0, 3);
lcd.print("Items: ");
lcd.print(numberOfItems);
lcd.print(" Wt: ");
lcd.print(currentWeight, 1);
lcd.print("g");
}
void handleSerialInput() {
Serial.print("Enter item name:");
readSerialString(itemName, 20);
Serial.println(itemName);
EEPROM.put(EEPROM_ITEM_NAME, itemName);
Serial.print("Enter item code:");
readSerialString(itemCode, 8);
Serial.println(itemCode);
EEPROM.put(EEPROM_ITEM_CODE, itemCode);
Serial.print("Enter item weight (g):");
itemWeight = readSerialFloat();
Serial.println(itemWeight);
EEPROM.put(EEPROM_ITEM_WEIGHT, itemWeight);
Serial.print("Enter reorder limit:");
reorderLimit = readSerialInt();
Serial.println(reorderLimit);
EEPROM.put(EEPROM_REORDER_LIMIT, reorderLimit);
Serial.print("Enter bin weight (g):");
binWeight = readSerialFloat();
Serial.println(binWeight);
EEPROM.put(EEPROM_BIN_WEIGHT, binWeight);
Serial.println("Settings Completed");
Serial.print("Enter Code: ");
updateDisplay();
}
void readSerialString(char* buffer, int length) {
int index = 0;
while (index < length - 1) {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n') {
break;
}
buffer[index++] = c;
}
}
buffer[index] = '\0';
Serial.flush();
delay(200);
}
float readSerialFloat() {
while(Serial.available())
Serial.read();
while (!Serial.available()) { }
float las=Serial.parseFloat();
Serial.flush();
delay(200);
return las;
}
int readSerialInt() {
while(Serial.available())
Serial.read();
while (Serial.available() == 0) { }
int las= Serial.parseInt();
Serial.flush();
delay(200);
return las;
}