Hx711 loadSensor;
public int CalibrationFactor { get; set; } = 0;
public double CalibrationWeight { get; set; } = 1.6;
public override async Task Initialize()
{
Resolver.Log.Info("Initialize...");
loadSensor = new Hx711(Device.Pins.D04, Device.Pins.D03);
if (CalibrationFactor == 0)
{
GetAndDisplayCalibrationUnits(loadSensor);
}
else
{
await Task.Delay(500);
loadSensor.SetCalibrationFactor(CalibrationFactor, new Mass(CalibrationWeight, Mass.UnitType.Grams));
loadSensor.Tare();
}
loadSensor.Updated += (sender, values) => Resolver.Log.Info($"Mass is now returned {values.New.Grams:N2}g");
}
public override Task Run()
{
loadSensor.StartUpdating(TimeSpan.FromSeconds(2));
return Task.CompletedTask;
}
public void GetAndDisplayCalibrationUnits(Hx711 sensor)
{
Resolver.Log.Info($"Beginning Calibration. First we'll tare (set a zero).");
Resolver.Log.Info($"Make sure scale bed is clear. Next step in 5 seconds...");
Thread.Sleep(5000);
sensor.Tare();
Resolver.Log.Info($"Place a known weight on the scale. Next step in 5 seconds...");
Thread.Sleep(5000);
var factor = sensor.CalculateCalibrationFactor();
Resolver.Log.Info($"Your scale's Calibration Factor is: {factor}. Enter this into the code for future use.");
}