#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <HX711.h>
// Initialize the I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 for the LCD
// Keypad setup
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] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {A3, A2, A1, A0}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define pins for sensors and actuators
const int hx711DTPin = 3; // HX711 DT pin
const int hx711SCKPin = 2; // HX711 SCK pin
const int motorPin1 = 10; // IN1 for L298N
const int motorPin2 = 11; // IN2 for L298N
const int vibratorMotorPin = 12; // Digital pin for vibrator motor
const int buttonPin = 4; // Digital pin for push button
// HX711 setup
HX711 scale;
// Variable to store the desired weight
float targetWeight = 0;
float currentWeight = 0;
bool weightSet = false; // Flag to check if weight is set
void setup() {
// Initialize the LCD
lcd.init(); // Initialize the 16x2 LCD
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Wazan Dubao:");
// Initialize pins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(vibratorMotorPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Button pin as input with internal pull-up resistor
// Initialize HX711
scale.begin(hx711DTPin, hx711SCKPin);
scale.set_scale(); // Adjust to your calibration factor
scale.tare(); // Reset the scale to zero
lcd.setCursor(0, 1);
lcd.print("Tare complete");
delay(1000); // Allow some time to read the tare message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter weight:");
}
void loop() {
if (!weightSet) {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
targetWeight = targetWeight * 10 + (key - '0');
lcd.setCursor(0, 1);
lcd.print(targetWeight);
lcd.print("g ");
} else if (key == '#') {
// Set the weight
weightSet = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight set:");
lcd.setCursor(0, 1);
lcd.print(targetWeight);
lcd.print("g");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready to start.");
} else if (key == '*') {
// Reset weight
targetWeight = 0;
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
} else {
// Ready to dispense material
lcd.setCursor(0, 1);
lcd.print("Press # to start");
char key = keypad.getKey();
if (key == '#') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dispensing...");
dispenseMaterial();
}
}
// Check if the button is pressed to trigger motor sequence
if (digitalRead(buttonPin) == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button pressed");
// Turn on motorPin2 for 2 seconds
digitalWrite(motorPin2, HIGH);
delay(1500);
digitalWrite(motorPin2, LOW);
// Delay between motor actions if needed
delay(100); // Adjust as necessary
// Turn on motorPin1 for 2 seconds
digitalWrite(motorPin1, HIGH);
delay(1000);
digitalWrite(motorPin1, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("2 baar # Dubao .");
}
}
void dispenseMaterial() {
bool vibratorOn = false;
digitalWrite(vibratorMotorPin, LOW);
currentWeight = 0; // Reset current weight at the start
while (currentWeight < targetWeight) {
// Read weight from the load cell
currentWeight = scale.get_units();
if (currentWeight < targetWeight - 10) {
// Activate vibrator motor at full speed
digitalWrite(vibratorMotorPin, HIGH);
} else if (currentWeight < targetWeight) {
// Vibrator motor runs intermittently
digitalWrite(vibratorMotorPin, HIGH);
delay(100);
digitalWrite(vibratorMotorPin, LOW);
delay(100);
if (!vibratorOn) {
vibratorOn = true;
}
}
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(currentWeight);
lcd.print("g ");
}
digitalWrite(vibratorMotorPin, LOW);
closeBucket();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Complete!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pedal dubao.");
currentWeight = 0;
weightSet = false; // Reset the weight set flag for next use
}
void closeBucket() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(0); // Adjust the delay to control how long the motor runs to close the bucket
stopMotor();
}
void stopMotor() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}