Trying to read weight value from serial monitor, then use that value in script

So I am trying to make a sorting bin that works on weight, I have a load cell hooked up to the HX711 amp and two servo motors hooked up.

I want my first servo (myservoH) to move after its has got the weight of the object between 65.0 and 80.0 grams (I will eventually want 3 weight classes, Greater than, less than and between weights) . After weight tare the servo just loops the movement I have for it not when I call it.

I am pretty sure I am just not calling the weight and then telling the servo to move when its called.

The load cell and and servos work individually but trying to get them to work together is stressing me out. My coding skills is very poor so I am sure it is a easy fix but I just cant see it, Thank you in advance for any help!!

#include <HX711.h>


#define DOUT  3
#define CLK  2

#include <Servo.h>

Servo myservoD;  // create servo object to control a servo
Servo myservoH;
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position



HX711 scale(DOUT, CLK);

float calibration_factor = -290; //-7050 worked for my 440lb max scale setup

void setup()

{
  myservoD.attach(5);  // attaches the servo on pin 5 to the servo object
  myservoH.attach(6);
 
  
  Serial.begin(9600);
  Serial.println("HX711 calibration sketch");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press + or a to increase calibration factor");
  Serial.println("Press - or z to decrease calibration factor");

  scale.set_scale();
  scale.tare();  //Reset the scale to 0

  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
}

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor

  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1);
  Serial.print(" g"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  if(Serial.available())
  
 //   char temp = Serial.read();
 //   if(temp == '+' || temp == 'a')
 //     calibration_factor += 10;
 //   else if(temp == '-' || temp == 'z')
 //     calibration_factor -= 10;


  if ((scale.get_units(),1)> 65.0 && (scale.get_units(),1) < 80.0)
 for (pos = 0; pos >= 180; pos += 1)    // goes from 180 degrees to 0 degrees 
{
     myservoH.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservoH.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  
  }
}

see Serial Input Basics on how to read properly data coming down the Serial line (and you can use standard C functions to parse the buffer, have a look at stdlib.h and string.h)

This could also be useful for you: Planning and Implementing a Program

PS: You are not writing a script but a C++ program :slight_smile:

Mradobe:
So I am trying to make a sorting bin that works on weight, I have a load cell hooked up to the HX711 amp and two servo motors hooked up.

That does not sound as if the weight value is coming from the Serial Monitor.

...R

Robin2:
That does not sound as if the weight value is coming from the Serial Monitor.

...R

It is measuring the right weight, as I know the weight of the object just not sure how I get that value back to my program and use it.

(Picture of serial monitor)

Thanks for the help I am super stuck!

Mradobe:
It is measuring the right weight, as I know the weight of the object just not sure how I get that value back to my program and use it.

There is no need to get the value back to your program because it never left the program in the first place.

Try this little change

void loop() {

  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  float scaleValue = scale.get_units();

  Serial.print("Reading: ");
  Serial.print(scaleValue, 1);

and if that works then the value from the scale will be in the variable scaleValue and you can use it for other purposes

...R