#include <Servo.h> // Servo library
#include <HX711_ADC.h> // HX711 library
#include <OneWire.h> // OneWire library
#include <LiquidCrystal_I2C.h> // LCD library
HX711_ADC LoadCell(4, 5); // Load cell initialization
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD initialization
int buttonPinIncrement = 11; // Pin number for the increment button
int buttonPinDecrement = 12; // Pin number for the decrement button
const int confirmButtonPin = 13; // Pin number for the confirmation button
const int buttonA3 = A3; // Pin number for Button A3
const int buttonA4 = A4; // Pin number for Button A4
const int buttonA5 = A5; // Pin number for Button A5
int taree = 6;
int buzzerPin = 24; // Pin number for the buzzer
unsigned long buttonPressedTime = 0; // Variable to store the time the button was pressed
unsigned long myservo1Time = 0; // Variable to store the time the button was pressed
unsigned long selectContainerTimer = 0; // Timer for SELECT CONTAINER message
float count = 0; // Variable to be increased or decreased by 0.01
int valuer = 0;
Servo myservo1;
Servo myservo2;
unsigned long previousMillis = 10000;
const long int interval = 1000;
unsigned long lastButtonPressTime = 0; // To track the last time a button was pressed
bool containerSelected = false;
void setup() {
pinMode(buttonPinIncrement, INPUT_PULLUP); // Enable internal pull-up resistor for the increment button
pinMode(buttonPinDecrement, INPUT_PULLUP); // Enable internal pull-up resistor for the decrement button
pinMode(confirmButtonPin, INPUT_PULLUP);
pinMode(buttonA3, INPUT_PULLUP); // Enable internal pull-up resistor for Button A3
pinMode(buttonA4, INPUT_PULLUP); // Enable internal pull-up resistor for Button A4
pinMode(buttonA5, INPUT_PULLUP); // Enable internal pull-up resistor for Button A5
pinMode(taree, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
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"); // Print out to LCD
lcd.setCursor(14, 2); // Set cursor position for weight value
lcd.print("0.00 g"); // Print out the initial value with two decimal places and leading zero
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() {
unsigned long currentMillis = millis(); // Check if 1000 milliseconds (1 second) have passed since the last update
if (currentMillis - previousMillis >= interval) previousMillis = currentMillis;
if (digitalRead(buttonPinIncrement) == LOW) {
count += 0.1; // Increase the counter value by 0.1
printWeight(count, 2); // Print the new value with 2 decimal places
delay(200); // Delay to prevent multiple presses on the button
}
if (digitalRead(buttonPinDecrement) == LOW) {
count -= 0.1; // Decrease the counter value by 0.1
if (count < 0) {
count = 0; // Ensure the counter doesn't go below zero
}
printWeight(count, 2); // Print out the new value with 2 decimal places
delay(200); // Delay to prevent multiple presses on the button
}
// Select container based on button press
if (digitalRead(buttonA3) == LOW) {
lcd.setCursor(8, 1); // Set cursor position for container selection
lcd.print("Container 1"); // Print out the selected container
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
} else if (digitalRead(buttonA4) == LOW) {
lcd.setCursor(8, 1); // Set cursor position for container selection
lcd.print("Container 2"); // Print out the selected container
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
} else if (digitalRead(buttonA5) == LOW) {
lcd.setCursor(8, 1); // Set cursor position for container selection
lcd.print("Container 3"); // Print out the selected container
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
}
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
}
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 i = LoadCell.getData(); // Get output value
if (i > count ) {
myservo1.write(90);
} else {
myservo1.write(0);
}
if (i < 0) {
lcd.setCursor(0, 3); // Set cursor to the fourth row
lcd.print("click Taring..."); // Print out taring message
}
printWeight(i, 0); // Print out the retrieved value to the second row with 2 decimal places
if (digitalRead(taree) == LOW) {
lcd.setCursor(0, 3); // Set cursor to the fourth row
lcd.print("Taring..."); // Print out taring message
delay(1000);
LoadCell.start(10);
lcd.setCursor(0, 3);
lcd.print("Confirm "); // Clear the taring message after 1 second
}
}
void printWeight(float value, int row) {
lcd.setCursor(14, row); // Set cursor position for weight value
char buffer[10];
dtostrf(value, 4, 2, buffer); // Format the float value with 2 decimal places
lcd.print(buffer); // Print out the formatted value
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
}
}