Using millis with load cell and servomotor does not work.

Hello. :slight_smile:

Please understand that English is not familiar and there is a lack of explanation.

Using a load cell
We have experienced a delay in the weighing of the servomotor using the ‘delay’ function.

I searched hard and found how to use the millis function instead.

So the servo motor works
When combined with the load cell’s hx711 code
Servo motor does not work.

After testing one by one,

units = scale.get_units (), 10;

I found a problem with the motor running when I removed this line.

But of course if you remove this line
Because we can not load the weight properly …

I sincerely ask for help to find a solution.

If you use ‘delay’ instead of ‘millis’
It worked fine in the same code.
Except for the side effects of ‘delay’.


#include “HX711.h”

#define DT A1
#define SCK A0

HX711 scale(DT, SCK);

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

#include <Servo.h>
Servo servo;

long servointerval = 200;
long previousMillis = 0;
long previousMillis2 = 0;
int val = 0;

int motor = 9;

void setup() {

servo.attach(motor);

pinMode(motor,OUTPUT);

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

long zero_factor = scale.read_average(); //Get a baseline reading

}

void loop() {

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

** units = scale.get_units(), 10; // this is problem line**

if (units < 0) {
units = 0.0;
}

for(val = 60; val <= 90; val += 1){
unsigned long servoMillis = millis();
if(servoMillis - previousMillis > servointerval)
{
previousMillis = servoMillis;
servo.write (val);
}
}

for(val = 90; val >= 60; val -= 1){
unsigned long servoMillis2 = millis();
if(servoMillis2 - previousMillis2 > servointerval)
{
previousMillis2 = servoMillis2;
servo.write (val);
}

}

 for (val = 60; val <= 90; val += 1)
  {
    unsigned long servoMillis = millis();
    if (servoMillis - previousMillis > servointerval)
    {
      previousMillis = servoMillis;
      servo.write (val);
    }
  }

There is nothing in this code to slow down the for loop so it will execute very quickly, because if servoMillis - previousMillis > servointerval is not true then the for loop will move onto the next value and nothing will be written to the servo.

Here is an example program that uses millis() timing to move a servo slowly

#include <Servo.h>

unsigned long servoMoveMillis;
unsigned long currentMillis;
unsigned long servoInterval = 50;
int servoStep = 1;
byte servoUpperLimit = 150;
byte servoLowerLimit = 30;
byte servoPosition = 90;

Servo theServo;

void setup()
{
  Serial.begin(115200);
  theServo.attach(A5);
  theServo.write(servoPosition);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - servoMoveMillis >= servoInterval)
  {
    servoPosition += servoStep;
    if (servoPosition > servoUpperLimit || servoPosition < servoLowerLimit)
    {
      servoPosition -= servoStep;
      servoStep *= -1;
    }
    theServo.write(servoPosition);
    servoMoveMillis = currentMillis;
  }
  //other code that must not be blocked goes here
}