diff --git a/examples/Calibration/Calibration.ino b/examples/Calibration/Calibration.ino index 6cbf25a..7922203 100644 --- a/examples/Calibration/Calibration.ino +++ b/examples/Calibration/Calibration.ino @@ -19,7 +19,9 @@ */ #include +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) #include +#endif //pins: const int HX711_dout = 4; //mcu > HX711 dout pin @@ -38,7 +40,7 @@ void setup() { LoadCell.begin(); unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time - boolean _tare = false; //set this to false if you don't want tare to be performed in the next step + 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() || LoadCell.getSignalTimeoutFlag()) { Serial.println("Timeout, check MCU>HX711 wiring and pin designations"); diff --git a/examples/Persistent_zero_offset/Persistent_zero_offset.ino b/examples/Persistent_zero_offset/Persistent_zero_offset.ino new file mode 100644 index 0000000..565349b --- /dev/null +++ b/examples/Persistent_zero_offset/Persistent_zero_offset.ino @@ -0,0 +1,108 @@ +/* + ------------------------------------------------------------------------------------- + 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. + If you have other time consuming code running (i.e. a graphical LCD), consider calling update() from an interrupt routine, + see example file "Read_1x_load_cell_interrupt_driven.ino". + + This is an example sketch on how to use this library +*/ + +#include + +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) +#include +#endif + +//pins: +const int HX711_dout = 4; //mcu > HX711 dout pin +const int HX711_sck = 5; //mcu > HX711 sck pin + +//HX711 constructor: +HX711_ADC LoadCell(HX711_dout, HX711_sck); + +const int calVal_eepromAdress = 0; +const int tareOffsetVal_eepromAdress = 4; +unsigned long t = 0; + +void setup() { + Serial.begin(57600); delay(10); + Serial.println(); + Serial.println("Starting..."); + + LoadCell.begin(); + float calibrationValue; // calibration value (see example file "Calibration.ino") + calibrationValue = 696.0; // uncomment this if you want to set the calibration value in the sketch + +#if defined(ESP8266)|| defined(ESP32) + EEPROM.begin(512); +#endif + + //EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom + + //restore the zero offset value from eeprom: + long tare_offset = 0; + EEPROM.get(tareOffsetVal_eepromAdress, tare_offset); + LoadCell.setTareOffset(tare_offset); + boolean _tare = false; //set this to false as the value has been resored from eeprom + + unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time + LoadCell.start(stabilizingtime, _tare); + if (LoadCell.getTareTimeoutFlag()) { + Serial.println("Timeout, check MCU>HX711 wiring and pin designations"); + while (1); + } + else { + LoadCell.setCalFactor(calibrationValue); // set calibration value (float) + Serial.println("Startup is complete"); + } +} + +void loop() { + static boolean newDataReady = 0; + const int serialPrintInterval = 250; //increase value to slow down serial print activity + + // check for new data/start next conversion: + if (LoadCell.update()) newDataReady = true; + + // get smoothed value from the dataset: + if (newDataReady) { + if (millis() > t + serialPrintInterval) { + float i = LoadCell.getData(); + Serial.print("Load_cell output val: "); + Serial.println(i); + newDataReady = 0; + t = millis(); + } + } + + // receive command from serial terminal, send 't' to initiate tare operation: + if (Serial.available() > 0) { + char inByte = Serial.read(); + if (inByte == 't') refreshOffsetValueAndSaveToEEprom(); + } +} + +// zero offset value (tare), calculate and save to EEprom: +void refreshOffsetValueAndSaveToEEprom() { + long _offset = 0; + Serial.println("Calculating tare offset value..."); + LoadCell.tare(); // calculate the new tare / zero offset value (blocking) + _offset = LoadCell.getTareOffset(); // get the new tare / zero offset value + EEPROM.put(tareOffsetVal_eepromAdress, _offset); // save the new tare / zero offset value to EEprom + LoadCell.setTareOffset(_offset); // set value as library parameter (next restart it will be read from EEprom) + Serial.print("New tare offset value:"); + Serial.print(_offset); + Serial.print(", saved to EEprom adr:"); + Serial.println(tareOffsetVal_eepromAdress); +} diff --git a/examples/Read_1x_load_cell/Read_1x_load_cell.ino b/examples/Read_1x_load_cell/Read_1x_load_cell.ino index abe36a6..fb07c42 100644 --- a/examples/Read_1x_load_cell/Read_1x_load_cell.ino +++ b/examples/Read_1x_load_cell/Read_1x_load_cell.ino @@ -19,7 +19,9 @@ */ #include +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) #include +#endif //pins: const int HX711_dout = 4; //mcu > HX711 dout pin diff --git a/examples/Read_1x_load_cell_interrupt_driven/Read_1x_load_cell_interrupt_driven.ino b/examples/Read_1x_load_cell_interrupt_driven/Read_1x_load_cell_interrupt_driven.ino index 36d08fb..c145219 100644 --- a/examples/Read_1x_load_cell_interrupt_driven/Read_1x_load_cell_interrupt_driven.ino +++ b/examples/Read_1x_load_cell_interrupt_driven/Read_1x_load_cell_interrupt_driven.ino @@ -19,7 +19,9 @@ */ #include +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) #include +#endif const int HX711_dout = 3; //mcu > HX711 dout pin, must be external interrupt capable! const int HX711_sck = 5; //mcu > HX711 sck pin diff --git a/examples/Read_2x_load_cell/Read_2x_load_cell.ino b/examples/Read_2x_load_cell/Read_2x_load_cell.ino index 143ce5d..77be1d0 100644 --- a/examples/Read_2x_load_cell/Read_2x_load_cell.ino +++ b/examples/Read_2x_load_cell/Read_2x_load_cell.ino @@ -9,7 +9,9 @@ // Settling time (number of samples) and data filtering can be adjusted in the config.h file #include +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) #include +#endif //pins: const int HX711_dout_1 = 4; //mcu > HX711 no 1 dout pin diff --git a/examples/Testing/Testing.ino b/examples/Testing/Testing.ino index cd0c8cf..3fabc68 100644 --- a/examples/Testing/Testing.ino +++ b/examples/Testing/Testing.ino @@ -19,7 +19,9 @@ */ #include +#if defined(ESP8266)|| defined(ESP32) || defined(AVR) #include +#endif //pins: const int HX711_dout = 6; //mcu > HX711 dout pin diff --git a/library.properties b/library.properties index 9e8a2e0..3082fb0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=HX711_ADC -version=1.2.6 +version=1.2.7 author=Olav Kallhovd maintainer=Olav Kallhovd sentence=Library for the HX711 24-bit ADC for weight scales. diff --git a/src/HX711_ADC.cpp b/src/HX711_ADC.cpp index 051f7b9..0a6eed1 100644 --- a/src/HX711_ADC.cpp +++ b/src/HX711_ADC.cpp @@ -96,7 +96,7 @@ int HX711_ADC::startMultiple(unsigned long t) } isFirst = 0; } - if(millis() - startMultipleTimeStamp > startMultipleWaitTime) { + if((millis() - startMultipleTimeStamp) < startMultipleWaitTime) { update(); //do conversions during stabilization time yield(); return 0; @@ -145,7 +145,7 @@ int HX711_ADC::startMultiple(unsigned long t, bool dotare) } isFirst = 0; } - if(millis() - startMultipleTimeStamp > startMultipleWaitTime) { + if((millis() - startMultipleTimeStamp) < startMultipleWaitTime) { update(); //do conversions during stabilization time yield(); return 0;