/*
Source: https://www.electroschematics.com/pressure-sensor-guide/
*/
#include <HX711.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
const int dirPin = 2;
const int stepPin = 3;
const byte dout_pin = A1;
const byte sck_pin = A0;
unsigned long prevTime = 0;
float motorSpeed = 0;
float sensorReading = 0;
HX711 scale; // define object called scale
LiquidCrystal_I2C lcd(0x27, 16, 2);
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin); //define object as myStepper
void setup() {
Serial.begin(115200);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dout_pin, INPUT);
pinMode(sck_pin, INPUT);
//digitalWrite(dirPin, HIGH);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("HX711 Demo with");
lcd.setCursor(0,1);
lcd.print("HX711 Library");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing");
lcd.setCursor(0,1);
lcd.print("the scale");
delay (1500);
lcd.clear();
// parameter "gain" is ommited; the default value 128 is used by the library
// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0
scale.begin(dout_pin, sck_pin);
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(420.f); //2280.f // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop() {
if (millis() >= prevTime+1000){ //read scale every 1 second
float sensorReading = (scale.get_units());
Serial.print(sensorReading);
Serial.print("\t\t");
prevTime=millis();
motorSpeed = sensorReading*40; //update motorSpeed
//motorSpeed=200;
Serial.println(motorSpeed);
}
// set the motor speed:
myStepper.setAcceleration(35);
myStepper.setMaxSpeed(1000);
myStepper.setSpeed(motorSpeed);
myStepper.runSpeed();
//scale.power_down();// put the ADC in sleep mode
//delay(5000);
//scale.power_up();
}