/*
-------------------------------------------------------------------------------------
HX711_ADC
Arduino library for HX711 24-Bit Analog-to-Digital Converter for Weight Scales
Olav Kallhovd sept2017
-------------------------------------------------------------------------------------
*/
/*
Settling time (number of samples) and data filtering can be adjusted in the config.h file
For calibration and storing the calibration value in eeprom, see example file "Calibration.ino"
The update() function checks for new data and starts the next conversion. In order to acheive maximum effective
sample rate, update() should be called at least as often as the HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS.
This example shows how call the update() function from an ISR with interrupt on the dout pin.
Try this if you experince longer settling time due to time consuming code in the loop(),
i.e. if you are refreshing an graphical LCD, etc.
The pin used for dout must be external interrupt capable.
*/
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
#include "SevSeg.h"
const int HX711_dout = 2; //mcu > HX711 dout pin, must be external interrupt capable!
const int HX711_sck = 14; //mcu > HX711 sck pin
SevSeg sevseg; //Instantiate a seven segment object
const int buttonPin = 16;
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
volatile boolean newDataReady;
int buttonState = 0;
boolean ShowDecimals = false;
void setup() {
// Serial.begin(57600); delay(10);
// Serial.println();
pinMode(buttonPin, INPUT_PULLUP);
if (!digitalRead(buttonPin)) ShowDecimals = true;
byte numDigits = 4;
byte digitPins[] = { 10, 11, 12, 15 };
byte segmentPins[] = { 3, 4, 5, 6, 7, 8, 9,13 };
sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
float calibrationValue; // calibration value
calibrationValue =0.42; // uncomment this if you want to set this value in the sketch
LoadCell.begin();
//LoadCell.setReverseOutput();
unsigned long stabilizingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
while (1);
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
}
attachInterrupt(digitalPinToInterrupt(HX711_dout), dataReadyISR, FALLING);
}
//interrupt routine:
void dataReadyISR() {
if (LoadCell.update()) {
newDataReady = 1;
}
}
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW)
{
LoadCell.tareNoDelay();
}
sevseg.refreshDisplay();
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > t) {
float i = LoadCell.getData();
newDataReady = 0;
if ((i<1000) && ShowDecimals) sevseg.setNumberF(i,1);
else sevseg.setNumber(i);
// Serial.println(i);
t = millis();
}
}
//check if last tare operation is complete
// if (LoadCell.getTareStatus() == true) {
//
// }
}