I have a question about programming code that I would like to ask you. Thank you

To convert

#include "HX711.h"

// Wiring settings
const int DT_PIN = 5;
const int SCK_PIN = 6;

const int scale_factor = -2.85; //Scale parameter, obtained from the calibration program

HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing the scale");

  scale.begin(DT_PIN, SCK_PIN);

  Serial.println("Before setting up the scale:");
  
  Serial.println(scale.get_units(5), 0); //The value before the scale parameter is set

  scale.set_scale(scale_factor); // Set scale parameters
  scale.tare(); //reset to zero

  Serial.println("After setting up the scale:");

  Serial.println(scale.get_units(5), 0); //The value after setting the scale parameter

  Serial.println("Readings:"); //Do not put anything on the electronic scale before this message
}

void loop() {
  
  Serial.println(scale.get_units(1), 3);

  //scale.power_down(); // Enter sleep mode
  //delay(1);
  //scale.power_up(); // End sleep mode
}

This program is output into a program library, and then used with 2 buttons to control the motor. When button 1 is pressed, it will perform an extending action, and when it is released, it will stop. When button 2 is pressed, it will perform a retracting action, and when it is released, it will also stop (button It will continue to move when you press it, and it will stop when you release it). When the motor is extended, if the value output by the above program is between 31570 and 25830, it will all stop. There will be no response when the button is pressed. When it is retracted, the program If the output value is between 2922~-2818, everything will stop and there will be no response when pressing the button. I would like to ask how to write this paragraph?

You do not seem to have a problem with the IDE and hence your topic as been moved to a more suitable location on the forum.

Okay, I understand, thank you.

If it doesn’t matter what kind of motor or motor driver it is, is this logic correct?

#include "HX711.h"

const int DT_PIN = 5;
const int SCK_PIN = 6;
const float SCALE_FACTOR = -2.85;

const int buttonPin1 = 2; 
const int buttonPin2 = 3; 

const int motorOutExtend = 8; 
const int motorOutRetract = 9;

HX711 scale;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP); 
  pinMode(buttonPin2, INPUT_PULLUP); 

  pinMode(motorOutExtend, OUTPUT); 
  pinMode(motorOutRetract, OUTPUT); 
  Serial.begin(9600);

  scale.begin(DT_PIN, SCK_PIN);
  scale.set_scale(SCALE_FACTOR);
  scale.tare();
}

void loop() {
  float weight = scale.get_units(1);


  if (weight >= 25830 && digitalRead(buttonPin1) == LOW) {
    digitalWrite(motorOutExtend, LOW);
  } else {
    digitalWrite(motorOutExtend, HIGH);
  }


  if (weight <= 2922 && digitalRead(buttonPin2) == LOW) {
    digitalWrite(motorOutRetract, LOW);
  } else {
    digitalWrite(motorOutRetract, HIGH);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.