#include <Servo.h> // Servo library
#include <HX711_ADC.h> // HX711 library
#include <LiquidCrystal_I2C.h> // LCD library
#include <Keypad.h> // Keypad library
// Define pin numbers
const int confirmButtonPin = 13; // Pin number for the confirmation button
const int taree = 12; // Pin number for the tare button
const int buzzerPin = 24; // Pin number for the buzzer
const int buttonA3 = 37; // Pin number for button A3
const int buttonA4 = 39; // Pin number for button A4
const int buttonA5 = 41; // Pin number for button A5
int valuer = 0; // Variable to track the number of decimal places for weight input
HX711_ADC LoadCell(4, 5); // Load cell initialization
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD initialization
Servo myservo1;
Servo myservo2;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {37, 39, 41, 43}; // R1, R2, R3, R4
byte colPins[COLS] = {45, 47, 49, 51}; // C1, C2, C3, C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
unsigned long selectContainerTimer = 0; // Timer for SELECT CONTAINER message
float count = 0; // Variable to store the weight count
bool containerSelected = false;
void selectContainer(int container);
void printWeight(float value, int row);
void beepBuzzer(int numBeeps);
void setup() {
pinMode(confirmButtonPin, INPUT_PULLUP);
pinMode(taree, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonA3, INPUT_PULLUP);
pinMode(buttonA4, INPUT_PULLUP);
pinMode(buttonA5, INPUT_PULLUP);
myservo1.attach(9); // Attaches the servo on pin 9 to the servo object
myservo2.attach(10); // Attaches the servo on pin 10 to the servo object
LoadCell.begin(); // Start connection to HX711
LoadCell.start(9000); // Load cells get 1000ms of time to stabilize
LoadCell.setCalFactor(412); // Calibrate your load cell with a 100g weight, and change the value according to readings
lcd.begin(20, 4); // Begins connection to the LCD module
lcd.backlight(); // Turns on the backlight
lcd.setCursor(18, 0); // Set cursor to first row and position it at 18th column
lcd.print("g"); // Print "g" at the top right corner of the LCD
lcd.setCursor(0, 2); // Set cursor to second row
lcd.print("Enter Grams: ");
lcd.setCursor(18, 2);
lcd.print(" g");
lcd.setCursor(0, 3); // Set cursor to third row
lcd.print("Confirm: "); // Print out confirmation label
lcd.setCursor(14, 3); // Set cursor position for confirmation value
lcd.print("0.00 g"); // Print the initial confirmation value with two decimal places and leading zero
lcd.setCursor(0, 1); // Set cursor to the fourth row
lcd.print("Select: "); // Print out the selection label
}
void loop() {
char key = keypad.getKey(); // Read the keypad
if (key != NO_KEY) {
if (key == 'A') { // Container 1 selected
selectContainer(1);
lcd.print("Container 1");
} else if (key == 'B') { // Container 2 selected
selectContainer(2);
lcd.print("Container 2");
} else if (key == 'C') { // Container 3 selected
selectContainer(3);
lcd.print("Container 3");
} else if (key == '#') {
// Erase number from 1 column
if (valuer > 0) {
lcd.setCursor(12 + valuer - 0, 2); // Set cursor to the position of the last entered digit
lcd.print(" "); // Clear the previous digit
valuer--;
count /= 10; // Remove the last digit from the count
}
} else if (key == '*') {
// Insert decimal point if not already present
if (valuer < 5 && count == (int)count) { // Check if decimal point is not already present and there are less than 6 digits
lcd.setCursor(13 + valuer, 2); // Set cursor to the position where the decimal point will be inserted
lcd.print("."); // Print the decimal point
valuer++; // Increment the number of digits
}
} else {
// Handle numeric input
if (valuer < 6 && key >= '0' && key <= '9') {
count = count * 10 + (key - '0'); // Update the count with the new digit
valuer++;
lcd.setCursor(12 + valuer, 2); // Set cursor to the position of the last entered digit
lcd.print(key); // Print the entered digit
}
}
}
// Confirm button pressed
if (digitalRead(confirmButtonPin) == LOW) {
if (containerSelected) {
printWeight(count, 3); // Print the count value on the screen with 2 decimal places
} else {
lcd.setCursor(0, 1); // Set cursor position to second row
lcd.print("SELECT CONTAINER"); // Print out the message to select container
selectContainerTimer = millis(); // Start the timer for SELECT CONTAINER message
beepBuzzer(3); // Beep the buzzer 3 times
}
}
// If SELECT CONTAINER message is displayed for 5 seconds, clear the message and reset containerSelected
if (millis() - selectContainerTimer >= 5000 && !containerSelected) {
lcd.setCursor(0, 1);
lcd.print("Select: "); // Clear the SELECT CONTAINER message
containerSelected = false; // Reset containerSelected flag
lcd.setCursor(0, 3);
lcd.print(""); // Clear the confirmation message
}
// Update current weight display
lcd.setCursor(0, 0); // Set cursor to first row
lcd.print("Current: "); // Print out to LCD with a space after colon
LoadCell.update(); // Retrieve data from the load cell
float currentWeight = LoadCell.getData(); // Get current weight value from the load cell
printWeight(currentWeight, 0); // Print out the current weight with two decimal places
// Servo control based on weight comparison
if (currentWeight > count) {
// Adjust servo position accordingly
myservo1.write(90); // Example: move servo to 90 degrees
} else {
// Adjust servo position accordingly
myservo1.write(0); // Example: move servo to 0 degrees
}
// Tare functionality
if (currentWeight < 0) {
lcd.setCursor(0, 3); // Set cursor to the fourth row
lcd.print("click Taring..."); // Print out taring message
}
// Tare button pressed
if (digitalRead(taree) == LOW) {
lcd.setCursor(0, 3); // Set cursor to the fourth row
lcd.print("Taring..."); // Print out taring message
delay(1000); // Wait for stability
LoadCell.start(10); // Perform tare operation
lcd.setCursor(0, 3);
lcd.print("Confirm "); // Clear the taring message after 1 second
}
}
void selectContainer(int container) {
lcd.setCursor(8, 1); // Set cursor position for container selection
lcd.print("Container "); // Print out the
lcd.print(container); // Print out the container number
containerSelected = true;
lcd.setCursor(0, 1); // Set cursor to the fourth row
lcd.print("Select: "); // Print out the selection label
delay(200); // Delay to prevent multiple presses on the button
}
void printWeight(float value, int row) {
lcd.setCursor(14, row); // Set cursor position for weight value
lcd.print(value, 2); // Print out the weight value with two decimal places
lcd.print(" g"); // Print the unit
}
void beepBuzzer(int numBeeps) {
for (int i = 0; i < numBeeps; i++) {
tone(buzzerPin, 1000); // Generate a tone of 1000 Hz
delay(100); // Wait
noTone(buzzerPin); // Turn off the tone
delay(100); // Wait
}
}