90 lines
1.9 KiB
C
90 lines
1.9 KiB
C
#define SAMPLES 10
|
|
|
|
#include <Q2HX711.h>
|
|
|
|
#ifndef _HELPER_H_
|
|
#include "helper.h"
|
|
#endif
|
|
|
|
// Scales
|
|
Q2HX711 hx711(A1, A0);
|
|
|
|
byte debug_level;
|
|
|
|
bool SetupScales(byte dbg_level)
|
|
{
|
|
debug_level = dbg_level;
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - setup_scales\n", millis());
|
|
}
|
|
|
|
bool res;
|
|
res = true;
|
|
|
|
// Use D10 to regulate power
|
|
pinMode(D10, OUTPUT);
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - setup_scale done\n", millis());
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
long ReadScale(char channel)
|
|
{
|
|
if (channel == 'B') {
|
|
hx711.setGain(128);
|
|
} else {
|
|
hx711.setGain(32);
|
|
}
|
|
delay(500);
|
|
|
|
long res;
|
|
int const num_scale_readings = 25; // number of instantaneous scale readings to calculate the median
|
|
|
|
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13
|
|
long readings[num_scale_readings]; // create arry to hold readings
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - my_read_average, measurements:\n", millis());
|
|
}
|
|
|
|
for (int i = 0; i < num_scale_readings; i++) {
|
|
readings[i] = hx711.read(); // fill the array with instantaneous readings from the scale
|
|
if (debug_level > 1) {
|
|
gCatena.SafePrintf("Reading %d: %d\n", i, readings[i]);
|
|
}
|
|
}
|
|
|
|
res = median(readings, num_scale_readings); // calculate median
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("Median of %d samples: %d\n", num_scale_readings, res);
|
|
float sdev;
|
|
sdev = stddev(readings, num_scale_readings);
|
|
gCatena.SafePrintf("Standard Deviation: %d.%03d\n", (int)sdev, (int)abs(sdev * 1000) % 1000);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void PowerdownScale()
|
|
{
|
|
// Disable Power
|
|
digitalWrite(D10, LOW);
|
|
}
|
|
|
|
void PowerupScale()
|
|
{
|
|
// Enable Power
|
|
digitalWrite(D10, HIGH);
|
|
|
|
// we wait 400ms (settling time according HX711 datasheet @ 10 SPS
|
|
delay(400);
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - setup_scale done\n", millis());
|
|
}
|
|
}
|