Skip to content

Commit e82a3a6

Browse files
author
Nathan Seidle
committed
Allow user to set average amount on getWeight()
1 parent 8fd9ea2 commit e82a3a6

2 files changed

+23
-22
lines changed

src/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool NAU7802::waitForCalibrateAFE(uint32_t timeout_ms)
127127

128128
while ((cal_ready = calAFEStatus()) == NAU7802_CAL_IN_PROGRESS)
129129
{
130-
if ((timeout_ms > 0) && ((millis() - begin) > timeout_ms))
130+
if ((timeout_ms > 0) && ((millis() - begin) > timeout_ms))
131131
{
132132
break;
133133
}
@@ -252,15 +252,15 @@ int32_t NAU7802::getReading()
252252
valueRaw |= (uint32_t)_i2cPort->read() << 8; //MidSB
253253
valueRaw |= (uint32_t)_i2cPort->read(); //LSB
254254

255-
// the raw value coming from the ADC is a 24-bit number, so the sign bit now
256-
// resides on bit 23 (0 is LSB) of the uint32_t container. By shifting the
257-
// value to the left, I move the sign bit to the MSB of the uint32_t container.
258-
// By casting to a signed int32_t container I now have properly recovered
259-
// the sign of the original value
260-
int32_t valueShifted = (int32_t)(valueRaw << 8);
255+
// the raw value coming from the ADC is a 24-bit number, so the sign bit now
256+
// resides on bit 23 (0 is LSB) of the uint32_t container. By shifting the
257+
// value to the left, I move the sign bit to the MSB of the uint32_t container.
258+
// By casting to a signed int32_t container I now have properly recovered
259+
// the sign of the original value
260+
int32_t valueShifted = (int32_t)(valueRaw << 8);
261261

262-
// shift the number back right to recover its intended magnitude
263-
int32_t value = ( valueShifted >> 8 );
262+
// shift the number back right to recover its intended magnitude
263+
int32_t value = (valueShifted >> 8);
264264

265265
return (value);
266266
}
@@ -286,6 +286,7 @@ int32_t NAU7802::getAverage(uint8_t averageAmount)
286286
}
287287
if (millis() - startTime > 1000)
288288
return (0); //Timeout - Bail with error
289+
delay(1);
289290
}
290291
total /= averageAmount;
291292

@@ -330,9 +331,9 @@ float NAU7802::getCalibrationFactor()
330331
}
331332

332333
//Returns the y of y = mx + b using the current weight on scale, the cal factor, and the offset.
333-
float NAU7802::getWeight(bool allowNegativeWeights)
334+
float NAU7802::getWeight(bool allowNegativeWeights, uint8_t samplesToTake)
334335
{
335-
int32_t onScale = getAverage(8);
336+
int32_t onScale = getAverage(samplesToTake);
336337

337338
//Prevent the current reading from being less than zero offset
338339
//This happens when the scale is zero'd, unloaded, and the load cell reports a value slightly less than zero value

src/SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,35 @@ typedef enum
164164
class NAU7802
165165
{
166166
public:
167-
NAU7802(); //Default constructor
167+
NAU7802(); //Default constructor
168168
bool begin(TwoWire &wirePort = Wire, bool reset = true); //Check communication and initialize sensor
169-
bool isConnected(); //Returns true if device acks at the I2C address
169+
bool isConnected(); //Returns true if device acks at the I2C address
170170

171-
bool available(); //Returns true if Cycle Ready bit is set (conversion is complete)
171+
bool available(); //Returns true if Cycle Ready bit is set (conversion is complete)
172172
int32_t getReading(); //Returns 24-bit reading. Assumes CR Cycle Ready bit (ADC conversion complete) has been checked by .available()
173173
int32_t getAverage(uint8_t samplesToTake); //Return the average of a given number of readings
174174

175175
void calculateZeroOffset(uint8_t averageAmount = 8); //Also called taring. Call this with nothing on the scale
176-
void setZeroOffset(int32_t newZeroOffset); //Sets the internal variable. Useful for users who are loading values from NVM.
177-
int32_t getZeroOffset(); //Ask library for this value. Useful for storing value into NVM.
176+
void setZeroOffset(int32_t newZeroOffset); //Sets the internal variable. Useful for users who are loading values from NVM.
177+
int32_t getZeroOffset(); //Ask library for this value. Useful for storing value into NVM.
178178

179179
void calculateCalibrationFactor(float weightOnScale, uint8_t averageAmount = 8); //Call this with the value of the thing on the scale. Sets the calibration factor based on the weight on scale and zero offset.
180180
void setCalibrationFactor(float calFactor); //Pass a known calibration factor into library. Helpful if users is loading settings from NVM.
181181
float getCalibrationFactor(); //Ask library for this value. Useful for storing value into NVM.
182182

183-
float getWeight(bool allowNegativeWeights = false); //Once you've set zero offset and cal factor, you can ask the library to do the calculations for you.
183+
float getWeight(bool allowNegativeWeights = false, uint8_t samplesToTake = 8); //Once you've set zero offset and cal factor, you can ask the library to do the calculations for you.
184184

185185
bool setGain(uint8_t gainValue); //Set the gain. x1, 2, 4, 8, 16, 32, 64, 128 are available
186186
bool setLDO(uint8_t ldoValue); //Set the onboard Low-Drop-Out voltage regulator to a given value. 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.2, 4.5V are avaialable
187187
bool setSampleRate(uint8_t rate); //Set the readings per second. 10, 20, 40, 80, and 320 samples per second is available
188188
bool setChannel(uint8_t channelNumber); //Select between 1 and 2
189189

190-
bool calibrateAFE(); //Synchronous calibration of the analog front end of the NAU7802. Returns true if CAL_ERR bit is 0 (no error)
191-
void beginCalibrateAFE(); //Begin asynchronous calibration of the analog front end of the NAU7802. Poll for completion with calAFEStatus() or wait with waitForCalibrateAFE().
190+
bool calibrateAFE(); //Synchronous calibration of the analog front end of the NAU7802. Returns true if CAL_ERR bit is 0 (no error)
191+
void beginCalibrateAFE(); //Begin asynchronous calibration of the analog front end of the NAU7802. Poll for completion with calAFEStatus() or wait with waitForCalibrateAFE().
192192
bool waitForCalibrateAFE(uint32_t timeout_ms = 0); //Wait for asynchronous AFE calibration to complete with optional timeout.
193-
NAU7802_Cal_Status calAFEStatus(); //Check calibration status.
193+
NAU7802_Cal_Status calAFEStatus(); //Check calibration status.
194194

195-
bool reset(); //Resets all registers to Power Of Defaults
195+
bool reset(); //Resets all registers to Power Of Defaults
196196

197197
bool powerUp(); //Power up digital and analog sections of scale, ~2mA
198198
bool powerDown(); //Puts scale into low-power 200nA mode
@@ -214,7 +214,7 @@ class NAU7802
214214
const uint8_t _deviceAddress = 0x2A; //Default unshifted 7-bit address of the NAU7802
215215

216216
//y = mx+b
217-
int32_t _zeroOffset; //This is b
217+
int32_t _zeroOffset; //This is b
218218
float _calibrationFactor; //This is m. User provides this number so that we can output y when requested
219219
};
220220
#endif

0 commit comments

Comments
 (0)