// Include necessary libraries
#include <Wire.h>
#include <HX711.h>
#include <Adafruit_SSD1306.h>
#include <Stepper.h>
// Define pins
#define LOADCELL_DOUT_PIN 2
#define LOADCELL_SCK_PIN 3
#define STEPPER_PINS 4
#define STEPPER_PIN1 8
#define STEPPER_PIN2 9
#define STEPPER_PIN3 10
#define STEPPER_PIN4 11
#define LED_PIN1 5
#define LED_PIN2 6
#define BUZZER_PIN 0
// Initialize Load cell and Stepper motor
HX711 scale;
Stepper stepper(STEPPER_PINS, STEPPER_PIN1, STEPPER_PIN2, STEPPER_PIN3, STEPPER_PIN4);
Adafruit_SSD1306 display(128, 32, &Wire);
void setup() {
// Initialize LED & Buzzer pins
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Initialize Load Cell
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
// Initialize Display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Initialize Stepper Motor
stepper.setSpeed(2000);
}
void loop() {
// Get load cell reading
float weight = scale.get_units(5);
// Clear Display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Weight: " + String(weight));
display.display();
delayMicroseconds(500);
// Stepper Motor Control
if (weight > 160) {
digitalWrite(LED_PIN2, HIGH);
stepper.step(-100);
} else if (weight > 160) {
digitalWrite(LED_PIN2, HIGH);
stepper.step(100);
} else if (weight < 2) {
stepper.step(0);
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN2, LOW);
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN1, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
}