#define ANIMATED
#include <MD_MAX72xx.h> // https://github.com/MajicDesigns/MD_MAX72XX
#include <MD_Parola.h> // https://github.com/MajicDesigns/MD_Parola
#include <HX711.h>
#include <SPI.h>
using Millis_t = decltype(millis());
using WeightStr_t = char[sizeof("0,000 kg")];
namespace gc {
constexpr uint8_t ClkPin {13};
constexpr uint8_t DataPin {11};
constexpr uint8_t CsPin {10};
constexpr uint8_t MaxLedDevices {5};
constexpr float ScaleCorrFactor {1 / 2.381}; // Unit in Grams
} // namespace gc
// Display
MD_Parola Mx = MD_Parola(MD_MAX72XX::PAROLA_HW, gc::CsPin, gc::MaxLedDevices);
HX711 Scale;
void getWeightStr(WeightStr_t WeightStr, long WeightTotal) {
WeightTotal = (WeightTotal + 5) / 10;
uint8_t WeightKg = WeightTotal / 100;
uint16_t WeigtGrams = WeightTotal % 100;
snprintf(WeightStr, sizeof(WeightStr_t), "%1d,%02d kg", WeightKg, WeigtGrams);
}
void showWeightStr(WeightStr_t WeightStr, textEffect_t Effect = PA_NO_EFFECT) {
Mx.displayClear();
Mx.displayText(WeightStr, PA_CENTER, 30, 0, Effect);
while (!Mx.displayAnimate()) { ; }
}
void setup() {
Serial.begin(115200);
Scale.begin(A1, A0);
Scale.set_scale(gc::ScaleCorrFactor);
Mx.begin();
Mx.setIntensity(15);
}
void loop() {
static WeightStr_t Buffer;
static long OldWeight {0};
#ifdef ANIMATED
static textEffect_t Animation[2] {PA_SCROLL_DOWN, PA_SCROLL_UP};
static uint8_t AIdx {0};
#endif
long WeightTotalInGrams = Scale.get_units(5);
if (OldWeight != WeightTotalInGrams) {
OldWeight = WeightTotalInGrams;
getWeightStr(Buffer, WeightTotalInGrams);
#ifdef ANIMATED
showWeightStr(Buffer, Animation[AIdx ^= 1]);
#else
showWeightStr(Buffer);
#endif
}
}