#include <HX711.h>
#include <LiquidCrystal.h>
//#include <Servo.h>
const int stepsPerRevolution = 1; // change this to fit the number of steps per revolution
// for your motor
// Pin Definitions for HX711
#define HX711_DT_PIN PA1 // Data Pin (PA1)
#define HX711_SCK_PIN PA2 // Clock Pin (PA2)
//const int servoPin = PA6; // Set the PWM pin (e.g., PA6)
const int Led = PA12 ;
#define DIR_PIN PA5
#define STEP_PIN D11
#define ENABLE_PIN PB13
#define STEP_DELAY 500
int potentiometerPin1 = PA0; // pin connected to the potentiometer
int potentiometerPin2 = PA4; // pin connected to the potentiometer
int buzzerPin = PA10;
int ledPin = PB4;
int potValue1 = 0; // Variable to store the potentiometer value
int potValue2 = 0; // Variable to store the potentiometer value
int thresholdBuzzer = 100; // Threshold value for Buzzer
int thresholdLED = 512; // Threshold value for LED
// Pin Definitions for LCD (4-bit mode)
#define RS PB5
#define E PB6
#define D4 PB0
#define D5 PB1
#define D6 PB2
#define D7 PB3
// Create HX711 object
HX711 scale;
// Initialize the LCD object (using 4-bit mode)
LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
float scale_factor = 420.0; // Adjust this value according to your load cell
float overload_threshold = 30.0; // Overload threshold in kg
//Servo myServo; // Create an instance of the Servo class called 'myServo'
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, INPUT);
// Initialize ENABLE pin as output
pinMode(ENABLE_PIN, OUTPUT);
// Disable the motor driver initially
digitalWrite(ENABLE_PIN, LOW); // Set to LOW to disable (active LOW for many drivers)
//myServo.attach(servoPin);
// Initialize serial communication for debugging
Serial.begin(9600);
pinMode(potentiometerPin1, INPUT);
pinMode(potentiometerPin2, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize HX711
scale.begin(HX711_DT_PIN, HX711_SCK_PIN);
// Initialize the LCD
lcd.begin(16, 2);
lcd.clear();
scale.set_scale(scale_factor); // Adjust to this calibration factor
scale.tare(); // Reset the scale to zero
pinMode(Led, OUTPUT);
digitalWrite(Led, LOW);
// Display initial message
lcd.setCursor(1, 0);
lcd.print("Initialize");
delay(2000);
lcd.clear();
}
void loop() {
// Read raw data from HX711
potValue1 = analogRead(potentiometerPin1);
potValue2= analogRead(potentiometerPin2);
Serial.println(potValue1);
Serial.println(potValue2);
long weight_raw = scale.read();
// Convert to calibrated weight
float weight_calibrated = (float)weight_raw / scale_factor;
// Print weight to serial monitor for debugging
Serial.print("Raw: ");
Serial.print(weight_raw);
Serial.print(" | Calibrated: ");
Serial.println(weight_calibrated);
if (potValue1 > thresholdBuzzer) {
digitalWrite(buzzerPin, HIGH); // Turn on Buzzer
}
else {
digitalWrite(buzzerPin, LOW); // Turn off Buzzer
}
if (potValue2 > thresholdLED) {
digitalWrite(ledPin, LOW); // Turn on LED
} else {
digitalWrite(ledPin, HIGH); // Turn off LED
}
// Small delay for stability
delay(100);
// Check for overload
if (weight_calibrated > overload_threshold) {
// Display overload message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OVERLOAD!");
lcd.setCursor(0, 1);
lcd.print("Weight: ");
lcd.print(weight_calibrated, 2);
lcd.print(" KG");
digitalWrite(Led, LOW);
} else {
// Display normal weight reading
lcd.setCursor(0, 0);
lcd.print(" Safe Weight:");
lcd.setCursor(0, 1);
lcd.print(weight_calibrated, 2); // Print weight with 2 decimal places
lcd.print(" KG");
digitalWrite(Led,HIGH);
}
if (digitalRead(Led) == HIGH && digitalRead(ledPin) == HIGH) {
// Both LEDs are ON, enable the motor driver
digitalWrite(ENABLE_PIN, HIGH); // Set to HIGH to enable (active HIGH)
// Rotate stepper motor
for (int i = 1; i < stepsPerRevolution ; i++) {
digitalWrite(STEP_PIN, HIGH); // Step pulse high
delayMicroseconds(STEP_DELAY); // Control step speed
digitalWrite(STEP_PIN, LOW); // Step pulse low
delayMicroseconds(STEP_DELAY); // Delay for next step
}
} else {
// Either or both LEDs are OFF, disable the motor driver
digitalWrite(ENABLE_PIN, LOW); // Set to LOW to disable
}
delay(500); // Update every 500ms
}