#include <HX711.h>
#include <PinChangeInterrupt.h>
#include <protothreads.h>
#include <AccelStepper.h>
HX711 scale;//initiates HX711 load cell amplifier
#define DT 5
#define SCK 6
#define dirPin 7
#define stepPin 8
#define tensioningPin 2
#define calibration_factor 420//This value is obtained using the SparkFun_HX711_Calibration sketch
#define motorInterfaceType 1
volatile float units;
float setpounds;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
static struct pt pt1, pt2;
static int protothreadReadScaleThread(struct pt* pt)
{
PT_BEGIN(pt);
while(true)
{
Serial.println("in scale loop");
units=scale.get_units(1);
}
PT_END(pt);
}
static int protothreadRunStepperThread(struct pt* pt)
{
PT_BEGIN(pt);
while(true)
{
Serial.println(units);
Serial.println("in stepper loop");
stepper.runSpeed();
}
PT_END(pt);
}
void setup()
{
Serial.begin(614400);
Serial.println("insetup");
scale.begin(DT, SCK);
scale.set_scale(calibration_factor);
stepper.setMaxSpeed(100);
stepper.setAcceleration(20);
stepper.moveTo(500);
digitalWrite(dirPin, HIGH);
PT_INIT(&pt1);
PT_INIT(&pt2);
stepper.setMaxSpeed(1000);
stepper.setSpeed(50);
}
void loop()
{
units=scale.get_units(3);
Serial.println(units);
delay(500);
protothreadRunStepperThread(&pt1);
protothreadReadScaleThread(&pt2);
}