some more tuning

This commit is contained in:
Joerg Lehmann 2020-05-25 18:07:56 +02:00
parent 1ce266ab22
commit de0fef6971
2 changed files with 12 additions and 31 deletions

View File

@ -56,7 +56,7 @@ enum {
| |
\****************************************************************************/ \****************************************************************************/
static const int32_t fwVersion = 20200523; static const int32_t fwVersion = 20200525;
static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages; static const byte INIT_PACKAGE_INTERVAL = 100; // send an init package every 100 packages;
static const byte MAX_VALUES_TO_SEND = 8; static const byte MAX_VALUES_TO_SEND = 8;

View File

@ -8,8 +8,7 @@
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h" #include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h"
#define SAMPLES 10 #define SAMPLES 5
#define IGNORE_READINGS 5
NAU7802 myScale; //Create instance of the NAU7802 class NAU7802 myScale; //Create instance of the NAU7802 class
@ -28,17 +27,14 @@ bool InitializeScales()
result &= myScale.reset(); //Reset all registers result &= myScale.reset(); //Reset all registers
result &= myScale.powerUp(); //Power on analog and digital sections of the scale result &= myScale.powerUp(); //Power on analog and digital sections of the scale
// we wait 100 ms to give it time to stabilze
delay(100);
result &= myScale.setIntPolarityHigh(); result &= myScale.setIntPolarityHigh();
result &= myScale.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V result &= myScale.setLDO(NAU7802_LDO_3V3); //Set LDO to 3.3V
result &= myScale.setGain(NAU7802_GAIN_128); //Set gain to 128 result &= myScale.setGain(NAU7802_GAIN_128); //Set gain to 128
result &= myScale.setSampleRate(NAU7802_SPS_80); //Set samples per second to 10 result &= myScale.setSampleRate(NAU7802_SPS_40); //Set samples per second to 40
result &= myScale.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing. result &= myScale.setRegister(NAU7802_ADC, 0x30); //Turn off CLK_CHP. From 9.1 power on sequencing.
result &= myScale.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR); result &= myScale.clearBit(NAU7802_PGA_PWR_PGA_CAP_EN, NAU7802_PGA_PWR);
result &= myScale.setRegister(NAU7802_OTP_B1, 0x30); //result &= myScale.setRegister(NAU7802_OTP_B1, 0x30);
result &= myScale.setRegister(NAU7802_PGA, NAU7802_PGA_OUT_EN | NAU7802_PGA_CHP_DIS); //result &= myScale.setRegister(NAU7802_PGA, NAU7802_PGA_OUT_EN | NAU7802_PGA_CHP_DIS);
result &= myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel result &= myScale.calibrateAFE(); //Re-cal analog front end when we change gain, sample rate, or channel
@ -91,27 +87,11 @@ long ReadScale(char channel)
} }
} }
int32_t dummy; if (myScale.available()) {
int const ignore_readings = IGNORE_READINGS; // number of first <n> readings to ignore long dummy = myScale.getReading();
}
int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median int const num_scale_readings = SAMPLES; // number of instantaneous scale readings to calculate the median
for (int i = 0; i < ignore_readings; i++) {
//while (digitalRead(interruptPin) == LOW) {
while (! myScale.available()) {
if ((millis() - startTime) > 60000) {
if (debug_level > 0) {
gCatena.SafePrintf("Timeout while reading scale (dummy values)...\n");
}
break;
}
delay(1);
}
dummy = myScale.getReading();
if (debug_level > 0) {
gCatena.SafePrintf("Dummy Reading int32_t: %d\n", dummy);
}
}
// we use the median, not the average, see https://community.particle.io/t/boron-gpio-provides-less-current-than-electrons-gpio/46647/13 // 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 long readings[num_scale_readings]; // create arry to hold readings
for (int i = 0; i < num_scale_readings; i++) { for (int i = 0; i < num_scale_readings; i++) {
@ -126,11 +106,12 @@ long ReadScale(char channel)
} }
delay(1); delay(1);
} }
int32_t reading = myScale.getReading(); long reading = myScale.getReading();
if (debug_level > 0) { if (debug_level > 0) {
gCatena.SafePrintf("Reading int32_t: %d\n", reading); gCatena.SafePrintf("Reading: %d\n", reading);
} }
readings[i] = long(reading); // fill the array with instantaneous readings from the scale readings[i] = reading; // fill the array with instantaneous readings from the scale
delay(10);
} }
long duration = millis() - startTime; long duration = millis() - startTime;