129 lines
3.4 KiB
C
129 lines
3.4 KiB
C
#include <Wire.h>
|
|
|
|
#ifndef _HELPER_H_
|
|
#include "helper.h"
|
|
#endif
|
|
|
|
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
|
|
|
|
#define SAMPLES 10
|
|
|
|
NAU7802 myScale; //Create instance of the NAU7802 class
|
|
|
|
byte debug_level;
|
|
|
|
byte interruptPin = A0;
|
|
|
|
bool SetupScales(byte dbg_level)
|
|
{
|
|
debug_level = dbg_level;
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - SetupScales start\n", millis());
|
|
}
|
|
pinMode(interruptPin, INPUT);
|
|
Wire.begin();
|
|
|
|
if (!myScale.begin())
|
|
{
|
|
gCatena.SafePrintf("Scale not detected. Please check wiring. Freezing...\n");
|
|
return false;
|
|
}
|
|
gCatena.SafePrintf("Scale detected!\n");
|
|
|
|
myScale.setIntPolarityHigh();
|
|
|
|
myScale.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR);
|
|
myScale.setSampleRate(NAU7802_SPS_80);
|
|
myScale.setLDO(NAU7802_LDO_3V3);
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - SetupScales done\n", millis());
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
long ReadScale(char channel)
|
|
{
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - ReadScale Start\n", millis());
|
|
}
|
|
|
|
uint8_t channelNumber;
|
|
if (channel == 'B') {
|
|
channelNumber = NAU7802_CHANNEL_1;
|
|
} else {
|
|
channelNumber = NAU7802_CHANNEL_2;
|
|
}
|
|
long startTime = millis();
|
|
myScale.setChannel(channelNumber);
|
|
myScale.calibrateAFE();
|
|
|
|
long res;
|
|
int const num_scale_readings = SAMPLES; // 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
|
|
|
|
for (int i = 0; i < num_scale_readings; i++) {
|
|
while (digitalRead(interruptPin) == LOW) {
|
|
//while(! myScale.available()) {
|
|
// we set a timeout of 60 seconds for the measurement...
|
|
if ((millis() - startTime) > 60000) {
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("Timeout while reading scale...\n");
|
|
}
|
|
return 0;
|
|
}
|
|
delay(1);
|
|
}
|
|
readings[i] = myScale.getReading(); // fill the array with instantaneous readings from the scale
|
|
}
|
|
|
|
long duration = millis() - startTime;
|
|
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);
|
|
float sdev_proc;
|
|
sdev_proc = 100 * (sdev / float(res));
|
|
gCatena.SafePrintf("Standard Deviation: %d.%03d\n", (int)sdev, (int)abs(sdev * 1000) % 1000);
|
|
gCatena.SafePrintf("Standard Deviation / Median (Percent): %d.%03d\n", (int)sdev_proc, (int)abs(sdev_proc * 1000) % 1000);
|
|
gCatena.SafePrintf("Duration (ms): %d\n", duration);
|
|
}
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - ReadScale Done\n", millis());
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void PowerdownScale()
|
|
{
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - PowerdownScale Start\n", millis());
|
|
}
|
|
myScale.powerDown();
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - PowerdownScale Done\n", millis());
|
|
}
|
|
}
|
|
|
|
void PowerupScale()
|
|
{
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - PowerupScale Start\n", millis());
|
|
}
|
|
myScale.powerUp(); //Power up scale. This scale takes ~600ms to boot and take reading.
|
|
|
|
// we wait 100 ms to give it time to stabilze
|
|
delay(100);
|
|
|
|
if (debug_level > 0) {
|
|
gCatena.SafePrintf("%010d - PowerupScale Done\n", millis());
|
|
}
|
|
}
|