Load cell that triggers a motor

Hi! I am gonna need some help since I am still very new with these stuff but very interessted in learning more. I am trying to build a treat dispenser with a load cell of 5kg and HX711. I am using Arduino Uno, a lcd display and a stepped motor.
I did a lot of research but it got me just that far.
I would appreciate if anyone could help me but until then I will keep researching.
Thanks in advance!

#include <LiquidCrystal.h>
#include <Stepper.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define DT A0
#define SCK A1
#define sw 9

long sample=0;
float val=0;
long count=0;
const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int rolePerMinute = 15;         // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper myStepper(stepsPerRevolution, 6, 8, 7, 9);

unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}

void setup()
{
  myStepper.setSpeed(rolePerMinute);
  // initialize the serial port:
  Serial.begin(9600);
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print(" Weight ");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(1000);
lcd.clear();
calibrate();
}

void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
lcd.setCursor(0,0);
lcd.print("Measured Weight");
lcd.setCursor(0,1);
lcd.print(w);
lcd.print("g ");

if(digitalRead(sw)==0)
{
val=0;
sample=0;
w=0;
count=0;
calibrate();
}
else if (count < 50) {
  motor();
}
}

void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
}
sample/=100;
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
}
val=val/100.0;
val=val/100.0; // put here your calibrating weight
lcd.clear();
}
void motor() {  
  // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
  
}

First things first, go to the Arduino IDE and hit ctrl-t to auto-format your code. The indentation convention that experienced programmers use isn't just to make things pretty. It really helps in visualizing the structure of your code. After that, repost your code and let's have a look at it.

Also, you haven't really asked a question. Your code does something. How is that different than what you want it to do?

I manged to calibrate the load cell but I cant manage to use it as a trigger. I got very confused where the motor loop should go.

I was trying to make it trigger when it drops under 50 grams and maybe I put some timer on it so its automatic but I didnt get to that part yet.

btw I did like you asked and hit that ctrl+t

thanks for taking a look at it

H#include <LiquidCrystal.h>
#include <Stepper.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define DT A0
#define SCK A1
#define sw 9

long sample = 0;
float val = 0;
long count = 0;
const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int rolePerMinute = 15;         // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper myStepper(stepsPerRevolution, 6, 8, 7, 9);

unsigned long readCount(void)
{
  unsigned long Count;
  unsigned char i;
  pinMode(DT, OUTPUT);
  digitalWrite(DT, HIGH);
  digitalWrite(SCK, LOW);
  Count = 0;
  pinMode(DT, INPUT);
  while (digitalRead(DT));
  for (i = 0; i < 24; i++)
  {
    digitalWrite(SCK, HIGH);
    Count = Count << 1;
    digitalWrite(SCK, LOW);
    if (digitalRead(DT))
      Count++;
  }
  digitalWrite(SCK, HIGH);
  Count = Count ^ 0x800000;
  digitalWrite(SCK, LOW);
  return (Count);
}

void setup()
{
  myStepper.setSpeed(rolePerMinute);
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(SCK, OUTPUT);
  pinMode(sw, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print(" Weight ");
  lcd.setCursor(0, 1);
  lcd.print(" Measurement ");
  delay(1000);
  lcd.clear();
  calibrate();
}

void loop()
{
  count = readCount();
  int w = (((count - sample) / val) - 2 * ((count - sample) / val));
  lcd.setCursor(0, 0);
  lcd.print("Measured Weight");
  lcd.setCursor(0, 1);
  lcd.print(w);
  lcd.print("g ");

  if (digitalRead(sw) == 0)
  {
    val = 0;
    sample = 0;
    w = 0;
    count = 0;
    calibrate();
  }
  else if (count < 50) {
    motor();
  }
}

void calibrate()
{
  lcd.clear();
  lcd.print("Calibrating...");
  lcd.setCursor(0, 1);
  lcd.print("Please Wait...");
  for (int i = 0; i < 100; i++)
  {
    count = readCount();
    sample += count;
  }
  sample /= 100;
  lcd.clear();
  lcd.print("Put 100g & wait");
  count = 0;
  while (count < 1000)
  {
    count = readCount();
    count = sample - count;
  }
  lcd.clear();
  lcd.print("Please Wait....");
  delay(2000);
  for (int i = 0; i < 100; i++)
  {
    count = readCount();
    val += sample - count;
  }
  val = val / 100.0;
  val = val / 100.0; // put here your calibrating weight
  lcd.clear();
}
void motor() {
  // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

}

If w is the weight in grams shouldn't you be checking for w < 50?

Even when I changed it now. I only get the Calibrating... please wait on my lcd and then I put the 100g weight and he does the same again and again like its jumping over some commands

How is your switch wired? I'm going to guess it is a switch input issue with wiring.

Put some Serial.println in your code so you can see what is getting executed. In particular the if and the else section in your loop() function.

Actually, your program will be easier to debug if you add one capability at a time. I would write a simple sketch that reads your switch. When you are convinced that works then add the code that reads the scale. Then after that add the code that turns the motor.

Alternatively, you could write individual sketches for the switch, the motor, and the scale and then combine them. That is the way I always do it.