forked from johnspiegel/AveragingScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAveragingScale.ino
574 lines (507 loc) · 15.8 KB
/
AveragingScale.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*********************************************************************
TODO:
"Coffee Mode"
Auto-off after a while
*********************************************************************/
#include <Wire.h>
#include <U8g2lib.h>
#include <ArduinoLowPower.h>
#include "HX711.h"
#include "buttons.h"
#include "CircularBuffer.h"
// 1.3" SH1106 DiyMall display.
#if defined(__AVR__)
U8G2_SH1106_128X64_NONAME_2_HW_I2C display(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#else
U8G2_SH1106_128X64_NONAME_F_HW_I2C display(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
#endif
// 0.96" SSD1306 DiyMall display.
// U8G2_SSD1306_128X64_NONAME_1_HW_I2C display(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 7;
// Control Button wiring
const int POWER_BUTTON_PIN = 2;
const int ZERO_BUTTON_PIN = 3;
const int AVG_BUTTON_PIN = 9;
const int COFFEE_BUTTON_PIN = 10;
// This a calibrated value from my scale.
#if defined(__AVR__)
constexpr double GRAMS_PER_RAW = 363.79 / 135703.0;
#else
constexpr double GRAMS_PER_RAW = 0.0009570014631347681;
#endif
inline long tenths(double f) {
return round(f*10);
}
char* float_to_str(float f) {
static char fbuf[16];
// 7-wide: handle negative thousands with one decimal point:
// |1234567|
// |-2345.7|
if (f > 99999.9) {
f = 99999.9;
}
if (f < -9999.9) {
f = -9999.9;
}
#if defined(__AVR__)
return dtostrf(f, 7, 1, fbuf);
#else
float absf = abs(f);
long t = tenths(absf);
snprintf(fbuf, sizeof(fbuf), "%5d.%1d", t/10, t % 10);
if (f < 0) {
int i = 3;
for (i = 3; i >= 0 && fbuf[i] != ' '; i--) {}
fbuf[i] = '-';
}
return fbuf;
#endif
}
class AveragingScale {
private:
HX711 *hx711_; // Not owned!
bool wiredBackwards_;
int32_t zero_;
double unitsPerRaw_;
#if defined(__AVR__)
CircularBuffer<int32_t, 50> buf_;
#else
CircularBuffer<int32_t, 100> buf_;
#endif
bool averaging_;
int32_t averagingSum_;
int averagingSampleCount_;
public:
AveragingScale(HX711* hx711, double unitsPerRaw=1.0, bool wiredBackwards=false) :
hx711_(hx711),
wiredBackwards_(wiredBackwards),
zero_(0),
unitsPerRaw_(unitsPerRaw),
averaging_(false)
{}
int32_t readRaw() {
int32_t v = hx711_->read();
if (wiredBackwards_) {
v = -v;
}
buf_.push(v);
if (averaging_) {
averagingSum_ += v;
averagingSampleCount_++;
}
return v;
}
struct RawAverageStats {
int32_t minValue;
int32_t maxValue;
int32_t sum;
int32_t sampleCount;
int32_t average;
};
double getAverageUnits(byte sampleCount) const {
return rawToUnits(getRawAverage(sampleCount).average);
}
RawAverageStats getRawAverage(byte sampleCount) const {
RawAverageStats stats;
if (buf_.size() == 0) {
return stats;
}
stats.sum = 0;
stats.sampleCount = 0;
stats.minValue = INT32_MAX;
stats.maxValue = INT32_MIN;
for (int i = 0; i < sampleCount && i < buf_.size(); i++, stats.sampleCount++) {
int32_t v = buf_[i];
stats.sum += v;
stats.minValue = min(stats.minValue, v);
stats.maxValue = max(stats.maxValue, v);
}
stats.average = stats.sum / stats.sampleCount;
return stats;
}
void setZero(int32_t zero) {
Serial.print(F("ZERO! Old: "));
Serial.print(zero_);
Serial.print(F(" new: "));
Serial.println(zero);
stopAveraging();
zero_ = zero;
}
void zero(byte sampleCount = 10) {
while (buf_.size() < sampleCount) {
readRaw();
}
RawAverageStats rawAvg = getRawAverage(sampleCount);
zero_ = getRawAverage(sampleCount).average;
Serial.print(F("Zero! zero_: "));
Serial.print(zero_);
Serial.print(F(" "));
Serial.print(rawAvg.sum);
Serial.print(F(" "));
Serial.print(rawAvg.sampleCount);
Serial.print(F(" "));
Serial.print(buf_.size());
Serial.println();
}
int32_t getRaw() const {
if (averaging_) {
return averagingSum_ / averagingSampleCount_;
} else {
// Smooth a it a bit over the last 200-300ms.
double instantUnits = getInstantUnits();
for (int seconds = 5; seconds > 0; seconds--) {
long rawAverage = getRawAverage(/*sampleCount=*/ seconds * 10).average;
if (abs(rawToUnits(rawAverage) - instantUnits) < (0.3/seconds)) {
Serial.print(F("Using "));
Serial.print(seconds);
Serial.print(F("s average: "));
Serial.print(rawToUnits(rawAverage));
Serial.print(F(" - instant: "));
Serial.print(instantUnits);
Serial.println();
return rawAverage;
}
}
return buf_[0];
}
}
int32_t get() const {
return getRaw() - zero_;
}
float getUnits() const {
return get() * unitsPerRaw_;
}
int32_t getInstant() const {
return buf_[0] - zero_;
}
double getInstantUnits() const {
return getInstant() * unitsPerRaw_;
}
double rawToUnits(long raw) const {
return (raw - zero_) * unitsPerRaw_;
}
void startAveraging() {
averaging_ = true;
averagingSum_ = buf_[0];
averagingSampleCount_= 1;
}
void stopAveraging() {
averaging_ = false;
}
double mostStableRecentAverage() {
double holdValue = getUnits();
double unitsValue = holdValue;
double minDelta = holdValue;
for (int i = 0; i < buf_.size() && averagingSampleCount_ > 0; i++) {
averagingSum_ -= buf_[i];
averagingSampleCount_--;
double newUnitsValue = getUnits();
double delta = abs(newUnitsValue - unitsValue);
if (delta < minDelta) {
Serial.print(F("Maybe HOLD AVG: "));
Serial.print(unitsValue);
Serial.print(F(" averagingSampleCount_: "));
Serial.println(averagingSampleCount_);
holdValue = unitsValue;
minDelta = delta;
}
unitsValue = newUnitsValue;
}
return holdValue;
}
bool isAveraging() const {
return averaging_;
}
void trackZeroDrift() {
if (averaging_) {
return;
}
const byte sampleCount = 20; // 2s at 10 samples-per-second.
RawAverageStats rawAvg = getRawAverage(sampleCount);
// Don't try to track drift if zero isn't stable.
if ((rawAvg.maxValue - rawAvg.minValue) * unitsPerRaw_ > 1.0) {
return;
}
// Don't track drift if the change is too large.
// And don't make lots of zero adjustments for small changes either.
float diffUnits = abs((rawAvg.average - zero_) * unitsPerRaw_);
if (diffUnits > 1.0 || diffUnits < 0.3) {
return;
}
Serial.print(F("AutoZero! millis: "));
Serial.print(millis());
Serial.print(F(" oldzero: "));
Serial.print(zero_);
Serial.print(F(" newzero: "));
Serial.print(rawAvg.average);
Serial.print(F(" diffUnits: "));
Serial.print(diffUnits);
Serial.println();
zero_ = rawAvg.average;
}
};
HX711 hx711;
AveragingScale ascale(&hx711, GRAMS_PER_RAW, /*wiredBackwards=*/false);
void wakeUp() {
Serial.println(F("-------------------------"));
Serial.print(F("Yawn.. waking up! millis: "));
Serial.print(millis());
Serial.println();
Serial.print(F("POWER PIN: "));
Serial.print(digitalRead(POWER_BUTTON_PIN));
Serial.println();
}
void goToSleep() {
Serial.println(F("Yawn.. going to sleep!"));
Serial.print(F("POWER PIN: "));
Serial.print(digitalRead(POWER_BUTTON_PIN));
Serial.println();
Serial.println(F("Putting display to sleep"));
display.setPowerSave(1);
Serial.println(F("Sleeping the HX711"));
hx711.power_down();
// Wait for button to settle.
long delayUntil = millis() + 200;
while (millis() < delayUntil) {
if (digitalRead(POWER_BUTTON_PIN) == LOW) {
Serial.println(F("Zero still down!"));
delayUntil = millis() + 200;
}
}
Serial.println(F("going to sleep"));
LowPower.attachInterruptWakeup(POWER_BUTTON_PIN, wakeUp, FALLING);
LowPower.deepSleep();
Serial.println(F("Yawn.. done sleeping?!"));
}
void setup() {
Serial.begin(9600);
Serial.println(F("-------------------------"));
Serial.println(F("Starting display..."));
display.begin();
display.setFont(u8g2_font_profont29_tr);
display.firstPage();
do {
display.setCursor(0, 16);
display.println(F("not your"));
display.setCursor(0, 16+23);
display.println(F(" average"));
display.setCursor(0, 16+46);
display.println(F(" scale"));
} while( display.nextPage() );
long splashStartMillis = millis();
Serial.println(F("Starting scale..."));
hx711.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.println(F("Scale started"));
Serial.println(F("Watching button pins..."));
watchDigitalPin(POWER_BUTTON_PIN);
watchDigitalPin(ZERO_BUTTON_PIN);
watchDigitalPin(AVG_BUTTON_PIN);
watchDigitalPin(COFFEE_BUTTON_PIN);
// Prime the scale and then zero while splash screen is up.
long delayUntilMillis = splashStartMillis + 1000;
while (millis() < delayUntilMillis) {
ascale.readRaw();
}
ascale.zero(/*sampleCount=*/ 5);
Serial.print(F("Splash start time: "));
Serial.print(splashStartMillis);
Serial.println(F("ms"));
Serial.println(F("Setup complete. time: "));
Serial.print(millis());
Serial.println(F("ms"));
Serial.println(F("-------------------------"));
}
void newDisplay( U8G2 *u8g2,
double grams,
bool averaging,
double instantGrams,
const CircularBuffer<double, 2> holds) {
char buf[16] = {0};
// Hold at the top.
if (holds.size()) {
u8g2->setFont(u8g2_font_profont22_tr);
snprintf(buf, sizeof(buf), "%sg", float_to_str(holds[0]));
u8g2->setCursor(128 - u8g2->getStrWidth(buf), 23);
u8g2->print(buf);
u8g2->setCursor(0, 23);
u8g2->print("H");
}
// Current value big at bottom
u8g2->setFont(u8g2_font_profont29_tr);
snprintf(buf, sizeof(buf), "%sg", float_to_str(grams));
u8g2->setCursor(u8g2->getDisplayWidth() - u8g2->getStrWidth(buf),
u8g2->getDisplayHeight() + u8g2->getDescent());
u8g2->print(static_cast<char*>(buf));
if (ascale.isAveraging()) {
u8g2->setFont(u8g2_font_profont29_tr);
// Make an "x-bar" by drawing a line across the top of an "x".
u8g2->setCursor(0, u8g2->getDisplayHeight() + u8g2->getDescent());
u8g2->print("x");
u8g2->drawBox(0, u8g2->getDisplayHeight() + u8g2->getDescent() - 19,
14, 3);
}
}
void displayCoffee(U8G2 *u8g2, double grams, bool averaging, long coffeeTime) {
char buf[16];
// Time at top
u8g2->setFont(u8g2_font_profont29_tr);
u8g2->setCursor(0, 23);
int minutes = (coffeeTime / 1000) / 60;
int seconds = (coffeeTime / 1000) % 60;
snprintf(buf, sizeof(buf), " %2d:%02ds", minutes, seconds);
u8g2->setCursor(u8g2->getDisplayWidth() - u8g2->getStrWidth(buf),
23);
u8g2->print(buf);
// Current value big at bottom
u8g2->setFont(u8g2_font_profont29_tr);
snprintf(buf, sizeof(buf), "%sg", float_to_str(grams));
u8g2->setCursor(u8g2->getDisplayWidth() - u8g2->getStrWidth(buf),
u8g2->getDisplayHeight() + u8g2->getDescent());
u8g2->print(static_cast<char*>(buf));
if (ascale.isAveraging()) {
u8g2->setFont(u8g2_font_profont29_tr);
// Make an "x-bar" by drawing a line across the top of an "x".
u8g2->setCursor(0, u8g2->getDisplayHeight() + u8g2->getDescent());
u8g2->print("x");
u8g2->drawBox(0, u8g2->getDisplayHeight() + u8g2->getDescent() - 19,
14, 3);
}
}
void loop() {
CircularBuffer<double, 2> holds;
bool coffeeMode = false;
long coffeeStart = 0;
int smoothCount = 0;
double prevValue = ascale.getUnits();
long lastLoop = millis();
while (true) {
long readStart = millis();
ascale.readRaw();
long readDone = millis();
Serial.print(F("Time to complete previous loop: "));
Serial.print(readStart - lastLoop);
Serial.print(" : ");
Serial.print(F(" Time to readRaw: "));
Serial.print(readDone - readStart);
Serial.println();
/*
*/
lastLoop = readStart;
double rawValue = ascale.getRaw();
// double value = ascale.getUnits();
double value = ascale.rawToUnits(rawValue);
// Don't distract with tiny fluctuations.
if (abs(value - prevValue) < 0.1 &&
abs(tenths(value) - tenths(prevValue)) == 1
/* && smoothCount < 5 */ ) {
Serial.print(F("Using smoothed value. SmoothCount: "));
Serial.print(smoothCount);
Serial.print(F(" value: "));
Serial.print(value);
Serial.print(F(" tenths: "));
Serial.print(tenths(value));
Serial.print(F(" prevValue: "));
Serial.print(prevValue);
Serial.print(F(" tenths: "));
Serial.print(tenths(prevValue));
Serial.println();
value = prevValue;
smoothCount++;
} else {
prevValue = value;
smoothCount = 0;
}
// Don't show -0.0.
if (abs(value) < 0.5) {
value = 0.0;
}
if (coffeeMode) {
if (coffeeStart == 0 && value >= 1.0) {
coffeeStart = millis();
}
}
display.firstPage();
do {
if (coffeeMode) {
displayCoffee(&display, value, ascale.isAveraging(), coffeeStart ? millis() - coffeeStart : 0);
} else {
newDisplay(&display, value, ascale.isAveraging(), ascale.getInstantUnits(), holds);
}
} while ( display.nextPage() );
// Serial.print(F("Value: "));
// Serial.println(ascale.getUnits());
// Serial.print(F("Value: "));
// Serial.println(float_to_str(ascale.getUnits()));
if (wasPressed(POWER_BUTTON_PIN)) {
Serial.print(F("POWER was pressed"));
Serial.println();
goToSleep();
setup();
return;
}
if (wasPressed(ZERO_BUTTON_PIN)) {
Serial.print(F("ZERO was pressed"));
Serial.println();
ascale.setZero(ascale.getRaw());
} else {
ascale.trackZeroDrift();
}
if (wasPressed(AVG_BUTTON_PIN)) {
Serial.print(F("AVG was pressed; "));
if (!ascale.isAveraging()) {
Serial.print(F("Averaging on."));
ascale.startAveraging();
} else {
Serial.print(F("Averaging off."));
holds.push(value);
ascale.stopAveraging();
}
Serial.println();
} else if (ascale.isAveraging()) {
Serial.println("averaging: ");
Serial.println(value);
Serial.println(ascale.getAverageUnits(10));
Serial.println(abs(ascale.getRawAverage(10).average * GRAMS_PER_RAW));
auto avg = ascale.getRawAverage(10);
Serial.println(avg.average);
Serial.println(avg.sum);
Serial.println(avg.sampleCount);
double recentAverage = ascale.getAverageUnits(10);
if ((value > 10.0 && abs(recentAverage) < 1.0) ||
(value > 1.0 && value <= 10.0 && abs(recentAverage / value) < 0.1)) {
double holdValue = ascale.mostStableRecentAverage();
holds.push(holdValue);
Serial.println("STOP averaging: ");
ascale.stopAveraging();
}
}
if (wasPressed(COFFEE_BUTTON_PIN)) {
Serial.print(F("COFFEE was pressed"));
Serial.println();
if (coffeeMode) {
Serial.print(F("Drink up!"));
Serial.println();
coffeeMode = false;
} else {
coffeeMode = true;
coffeeStart = 0;
Serial.print(F("Zeroing..."));
Serial.println();
ascale.setZero(rawValue);
ascale.zero(/*sampleCount=*/ 5);
Serial.print(F("Coffee Mode on!"));
Serial.println();
}
}
if (int p = getPhantoms()) {
Serial.print(F("Phantom buttom presses: "));
Serial.println(p);
}
if (int b = getBounces()) {
Serial.print(F("Bounces: "));
Serial.println(b);
}
}
}