// for https://forum.arduino.cc/t/steppermottor-control-with-loadcell-reading/1111071
// https://wokwi.com/projects/361202490307141633
#include <AccelStepper.h>
#include <HX711.h>
#define DOUT 3
#define CLK 2
HX711 scale;
void CalibrationLoadCell();
//AccelStepper stepperforward(AccelStepper::FULL4WIRE, 8, 9, 10, 7);
//AccelStepper stepperbackward(AccelStepper::FULL4WIRE, 8, 9, 10, 7);
AccelStepper stepper(AccelStepper::FULL4WIRE, 8, 9, 10, 7);
const int uppin = 6;
const int downpin = 5;
const int testpin = 4;
int uppinstate = 0;
int downpinstate = 0;
int testpinstate = 0;
int forward = 0;
int backward = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(2000000);
scale.begin(3, 2);
scale.set_scale(102.637);
pinMode(uppin, INPUT_PULLUP);
pinMode(downpin, INPUT_PULLUP);
pinMode(testpin, INPUT_PULLUP);
stepper.setMaxSpeed(700);
//stepperbackward.setMaxSpeed(-700);
stepper.setSpeed(700);
//stepperbackward.setSpeed(-700);
// digitalWrite(uppin, LOW);
//digitalWrite(downpin, HIGH);
//digitalWrite(testpin, HIGH);
}
void loop() {
int testpinstate = digitalRead(testpin);
int uppinstate = digitalRead(uppin);
int downpinstate = digitalRead(downpin);
if (testpinstate == LOW) {
stepper.setCurrentPosition(0);
int revolutions = 2;
stepper.move(10000);
while (stepper.currentPosition() < stepper.targetPosition())
stepper.runSpeedToPosition();
Serial.print(stepper.currentPosition());
CalibrationLoadCell();
}
//stepperbackward.move(-10000);
//stepperbackward.runSpeedToPosition()
if (uppinstate == HIGH) {
stepper.runSpeed();
CalibrationLoadCell();
}
if (downpinstate == LOW) {
stepper.runSpeed();
CalibrationLoadCell();
}
}
void CalibrationLoadCell()
{
scale.set_scale(102.637);
float grams = scale.get_units(15);
float grams_new = grams - 822.5 - 211;
float newtons = grams_new * 0.0098;
Serial.print(grams_new, 1);
Serial.print(",");
Serial.print(newtons, 1);
Serial.println();//
}