diff --git a/README.md b/README.md index cb23750..0f25377 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,19 @@ Wires between HX711 and load cell should be twisted and kept as short as possibl Most available HX711 modules seems to follow the reference design, but be aware that some modules are poorly designed with under-sized capacitors, and noisy readings. The Sparkfun module seems to differ from most other available modules as it has some additional components for noise reduction. +Update 1.1.0: +- Data set configuration (data set, number of samples) has been moved from HX711_ADC.h to the new file config.h +- Included "yield" in function tare() to avoid crash on ESP8266 if wiring is incorrect +- Timeout added to the tare() function to avoid endless loop if wiring is incorrect +- Added function getSingleConversionRaw() for testing and debugging purpose +- Added function getReadIndex() for testing and debugging purpose +- Added function getConversionTime() for testing and debugging purpose +- Added function getSPS() for testing and debugging purpose +- Added function getTareTimeoutFlag() for testing and debugging purpose +- Added example sketch "HX711_ADC_Calibration" with option to save the calibration value to eeprom +- Added example sketch "HX711_ADC_Testing" with some of the above mentioned functions for tare timeout, samplerate, conversion time, etc +- In example sketches setup, option included to fetch calibration value from eeprom + Update 1.0.2: - Implemented requested function setTareOffset(long newoffset) - Initializing of the index counter in the class declaration: readIndex = 0 diff --git a/examples/Calibrate/Calibrate.ino b/examples/Calibrate/Calibrate.ino deleted file mode 100644 index 622f2bd..0000000 --- a/examples/Calibrate/Calibrate.ino +++ /dev/null @@ -1,75 +0,0 @@ -//------------------------------------------------------------------------------------- -// HX711_ADC.h -// Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales -// Olav Kallhovd sept2017 -// Tested with : HX711 asian module on channel A and YZC-133 3kg load cell -// Tested with MCU : Arduino Nano -//------------------------------------------------------------------------------------- -/* This is an example sketch on how to find correct calibration factor for your HX711: - - Power up the scale and open Arduino serial terminal - - After stabelizing and tare is complete, put a known weight on the load cell - - Observe values on serial terminal - - Adjust the calibration factor until output value is same as your known weight: - - Sending 'l' from the serial terminal decrease factor by 1.0 - - Sending 'L' from the serial terminal decrease factor by 10.0 - - Sending 'h' from the serial terminal increase factor by 1.0 - - Sending 'H' from the serial terminal increase factor by 10.0 - - Sending 't' from the serial terminal call tare function - - Observe and note the value of the new calibration factor - - Use this new calibration factor in your sketch -*/ - -#include - -//HX711 constructor (dout pin, sck pin) -HX711_ADC LoadCell(4, 5); - -long t; - -void setup() { - Serial.begin(9600); - Serial.println("Wait..."); - LoadCell.begin(); - long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time - LoadCell.start(stabilisingtime); - LoadCell.setCalFactor(696.0); // user set calibration factor (float) - Serial.println("Startup + tare is complete"); -} - -void loop() { - //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS - //longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop) - LoadCell.update(); - - //get smoothed value from data set + current calibration factor - if (millis() > t + 250) { - float i = LoadCell.getData(); - float v = LoadCell.getCalFactor(); - Serial.print("Load_cell output val: "); - Serial.print(i); - Serial.print(" Load_cell calFactor: "); - Serial.println(v); - t = millis(); - } - - //receive from serial terminal - if (Serial.available() > 0) { - float i; - char inByte = Serial.read(); - if (inByte == 'l') i = -1.0; - else if (inByte == 'L') i = -10.0; - else if (inByte == 'h') i = 1.0; - else if (inByte == 'H') i = 10.0; - else if (inByte == 't') LoadCell.tareNoDelay(); - if (i != 't') { - float v = LoadCell.getCalFactor() + i; - LoadCell.setCalFactor(v); - } - } - - //check if last tare operation is complete - if (LoadCell.getTareStatus() == true) { - Serial.println("Tare complete"); - } - -} \ No newline at end of file diff --git a/examples/Calibration/Calibration.ino b/examples/Calibration/Calibration.ino new file mode 100644 index 0000000..7d83476 --- /dev/null +++ b/examples/Calibration/Calibration.ino @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------------- +// HX711_ADC.h +// Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales +// Olav Kallhovd sept2017 +// Tested with : HX711 asian module on channel A and YZC-133 3kg load cell +// Tested with MCU : Arduino Nano, ESP8266 +//------------------------------------------------------------------------------------- +// This is an example sketch on how to use this library +// Settling time (number of samples) and data filtering can be adjusted in the config.h file + +// This example shows how to calibrate the load cell and optionally save the calibration +// value to EEPROM, and also how to change the value. +// The value can later be fetched from EEPROM in your project sketch. + +#include +#include + +//HX711 constructor (dout pin, sck pin): +HX711_ADC LoadCell(4, 5); + +int eepromAdress = 0; + +long t; + +void calibrate() { + Serial.println("***"); + Serial.println("Start calibration:"); + Serial.println("It is assumed that the mcu was started with no load applied to the load cell."); + Serial.println("Now, place your known mass on the loadcell,"); + Serial.println("then send the weight of this mass (i.e. 100.0) from serial monitor."); + float m = 0; + boolean f = 0; + while (f == 0) { + LoadCell.update(); + if (Serial.available() > 0) { + m = Serial.parseFloat(); + if (m != 0) { + Serial.print("Known mass is: "); + Serial.println(m); + f = 1; + } + else { + Serial.println("Invalid value"); + } + } + } + float c = LoadCell.getData() / m; + LoadCell.setCalFactor(c); + Serial.print("Calculated calibration value is: "); + Serial.print(c); + Serial.println(", use this in your project sketch"); + f = 0; + Serial.print("Save this value to EEPROM adress "); + Serial.print(eepromAdress); + Serial.println("? y/n"); + while (f == 0) { + if (Serial.available() > 0) { + char inByte = Serial.read(); + if (inByte == 'y') { + #if defined(ESP8266) + EEPROM.begin(sizeof c); + #endif + EEPROM.put(eepromAdress, c); + #if defined(ESP8266) + EEPROM.commit(); + //EEPROM.begin(sizeof c); + #endif + EEPROM.get(eepromAdress, c); + Serial.print("Value "); + Serial.print(c); + Serial.print(" saved to EEPROM address: "); + Serial.println(eepromAdress); + f = 1; + + } + else if (inByte == 'n') { + Serial.println("Value not saved to EEPROM"); + f = 1; + } + } + } + Serial.println("End calibration"); + Serial.println("For manual edit, send 'c' from serial monitor"); + Serial.println("***"); +} + +void changeSavedCalFactor() { + float c = LoadCell.getCalFactor(); + boolean f = 0; + Serial.println("***"); + Serial.print("Current value is: "); + Serial.println(c); + Serial.println("Now, send the new value from serial monitor, i.e. 696.0"); + while (f == 0) { + if (Serial.available() > 0) { + c = Serial.parseFloat(); + if (c != 0) { + Serial.print("New calibration value is: "); + Serial.println(c); + LoadCell.setCalFactor(c); + f = 1; + } + else { + Serial.println("Invalid value, exit"); + return; + } + } + } + f = 0; + Serial.print("Save this value to EEPROM adress "); + Serial.print(eepromAdress); + Serial.println("? y/n"); + while (f == 0) { + if (Serial.available() > 0) { + char inByte = Serial.read(); + if (inByte == 'y') { + #if defined(ESP8266) + EEPROM.begin(sizeof c); + #endif + EEPROM.put(eepromAdress, c); + #if defined(ESP8266) + EEPROM.commit(); + //EEPROM.begin(sizeof c); + #endif + EEPROM.get(eepromAdress, c); + Serial.print("Value "); + Serial.print(c); + Serial.print(" saved to EEPROM address: "); + Serial.println(eepromAdress); + f = 1; + } + else if (inByte == 'n') { + Serial.println("Value not saved to EEPROM"); + f = 1; + } + } + } + Serial.println("End change calibration value"); + Serial.println("***"); +} + +void setup() { + Serial.begin(9600); delay(10); + Serial.println(); + Serial.println("Starting..."); + LoadCell.begin(); + long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time + LoadCell.start(stabilisingtime); + if (LoadCell.getTareTimeoutFlag()) { + Serial.println("Tare timeout, check MCU>HX711 wiring and pin designations"); + } + else { + LoadCell.setCalFactor(1.0); // user set calibration value (float) + Serial.println("Startup + tare is complete"); + } + while (!LoadCell.update()); + calibrate(); +} +void loop() { + //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS + //longer delay in sketch will reduce effective sample rate (be carefull with delay() in the loop) + LoadCell.update(); + + //get smoothed value from the data set + if (millis() > t + 250) { + float i = LoadCell.getData(); + Serial.print("Load_cell output val: "); + Serial.println(i); + t = millis(); + } + + //receive from serial terminal + if (Serial.available() > 0) { + float i; + char inByte = Serial.read(); + if (inByte == 't') LoadCell.tareNoDelay(); + else if (inByte == 'c') changeSavedCalFactor(); + } + + //check if last tare operation is complete + if (LoadCell.getTareStatus() == true) { + Serial.println("Tare complete"); + } + +} 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 e0b9dda..754ad93 100644 --- a/examples/Read_1x_load_cell/Read_1x_load_cell.ino +++ b/examples/Read_1x_load_cell/Read_1x_load_cell.ino @@ -3,34 +3,51 @@ // Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales // Olav Kallhovd sept2017 // Tested with : HX711 asian module on channel A and YZC-133 3kg load cell -// Tested with MCU : Arduino Nano +// Tested with MCU : Arduino Nano, ESP8266 //------------------------------------------------------------------------------------- // This is an example sketch on how to use this library -// Settling time (number of samples) and data filtering can be adjusted in the HX711_ADC.h file +// Settling time (number of samples) and data filtering can be adjusted in the config.h file #include +#include -//HX711 constructor (dout pin, sck pin) +//HX711 constructor (dout pin, sck pin): HX711_ADC LoadCell(4, 5); +const int eepromAdress = 0; + long t; void setup() { - Serial.begin(9600); - Serial.println("Wait..."); + + float calValue; // calibration value + calValue = 696.0; // uncomment this if you want to set this value in the sketch + #if defined(ESP8266) + //EEPROM.begin(sizeof calValue); // uncomment this if you use ESP8266 and want to fetch the value from eeprom + #endif + //EEPROM.get(eepromAdress, calValue); // uncomment this if you want to fetch the value from eeprom + + Serial.begin(9600); delay(10); + Serial.println(); + Serial.println("Starting..."); LoadCell.begin(); long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time LoadCell.start(stabilisingtime); - LoadCell.setCalFactor(696.0); // user set calibration factor (float) - Serial.println("Startup + tare is complete"); + if(LoadCell.getTareTimeoutFlag()) { + Serial.println("Tare timeout, check MCU>HX711 wiring and pin designations"); + } + else { + LoadCell.setCalFactor(calValue); // set calibration value (float) + Serial.println("Startup + tare is complete"); + } } void loop() { //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS - //longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop) + //use of delay in sketch will reduce effective sample rate (be carefull with use of delay() in the loop) LoadCell.update(); - //get smoothed value from data set + current calibration factor + //get smoothed value from data set if (millis() > t + 250) { float i = LoadCell.getData(); Serial.print("Load_cell output val: "); 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 160c32d..49ca16a 100644 --- a/examples/Read_2x_load_cell/Read_2x_load_cell.ino +++ b/examples/Read_2x_load_cell/Read_2x_load_cell.ino @@ -6,19 +6,39 @@ // Tested with MCU : Arduino Nano //------------------------------------------------------------------------------------- // This is an example sketch on how to use this library for two ore more HX711 modules -// Settling time (number of samples) and data filtering can be adjusted in the HX711_ADC.h file +// Settling time (number of samples) and data filtering can be adjusted in the config.h file #include +#include //HX711 constructor (dout pin, sck pin) HX711_ADC LoadCell_1(4, 5); //HX711 1 HX711_ADC LoadCell_2(6, 7); //HX711 2 +const int eepromAdress_1 = 0; // eeprom adress for calibration value load cell 1 +const int eepromAdress_2 = 4; // eeprom adress for calibration value load cell 2 + long t; void setup() { - Serial.begin(9600); - Serial.println("Wait..."); + + float calValue_1; // calibration value load cell 1 + float calValue_2; // calibration value load cell 2 + + calValue_1 = 696.0; // uncomment this if you want to set this value in the sketch + calValue_2 = 733.0; // uncomment this if you want to set this value in the sketch + #if defined(ESP8266) + //EEPROM.begin(sizeof calValue_1); // uncomment this if you use ESP8266 and want to fetch the value from eeprom + #endif + //EEPROM.get(eepromAdress_1, calValue_1); // uncomment this if you want to fetch the value from eeprom + #if defined(ESP8266) + //EEPROM.begin(sizeof calValue_2); // uncomment this if you use ESP8266 and want to fetch the value from eeprom + #endif + //EEPROM.get(eepromAdress_2, calValue_2); // uncomment this if you want to fetch the value from eeprom + + Serial.begin(9600); delay(10); + Serial.println(); + Serial.println("Starting..."); LoadCell_1.begin(); LoadCell_2.begin(); long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time @@ -28,14 +48,14 @@ void setup() { if (!loadcell_1_rdy) loadcell_1_rdy = LoadCell_1.startMultiple(stabilisingtime); if (!loadcell_2_rdy) loadcell_2_rdy = LoadCell_2.startMultiple(stabilisingtime); } - LoadCell_1.setCalFactor(696.0); // user set calibration factor (float) - LoadCell_2.setCalFactor(733.0); // user set calibration factor (float) + LoadCell_1.setCalFactor(calValue_1); // user set calibration value (float) + LoadCell_2.setCalFactor(calValue_2); // user set calibration value (float) Serial.println("Startup + tare is complete"); } void loop() { //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS - //longer delay in scetch will reduce effective sample rate (be carefull with delay() in loop) + //longer delay in scetch will reduce effective sample rate (be carefull with use of delay() in the loop) LoadCell_1.update(); LoadCell_2.update(); @@ -68,4 +88,4 @@ void loop() { Serial.println("Tare load cell 2 complete"); } -} \ No newline at end of file +} diff --git a/examples/Testing/Testing.ino b/examples/Testing/Testing.ino new file mode 100644 index 0000000..aa157e2 --- /dev/null +++ b/examples/Testing/Testing.ino @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------------- +// HX711_ADC.h +// Arduino master library for HX711 24-Bit Analog-to-Digital Converter for Weigh Scales +// Olav Kallhovd sept2017 +// Tested with : HX711 asian module on channel A and YZC-133 3kg load cell +// Tested with MCU : Arduino Nano, ESP8266 +//------------------------------------------------------------------------------------- +// This is an example sketch on how to use this library +// Settling time (number of samples) and data filtering can be adjusted in the config.h file + +#include +#include + +//HX711 constructor (dout pin, sck pin): +HX711_ADC LoadCell(4, 5); + +int eepromAdress = 0; + +long t; + +void setup() { + float calValue; // calibration value + calValue = 696.0; // uncomment this if you want to set this value in the sketch + #if defined(ESP8266) + //EEPROM.begin(sizeof calValue); // uncomment this if you use ESP8266 and want to fetch this value from eeprom + #endif + //EEPROM.get(eepromAdress, calValue); // uncomment this if you want to fetch this value from eeprom + + Serial.begin(9600); delay(10); + Serial.println(); + Serial.println("Starting..."); + LoadCell.begin(); + long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time + LoadCell.start(stabilisingtime); + if (LoadCell.getTareTimeoutFlag()) { + Serial.println("Tare timeout, check MCU>HX711 wiring and pin designations"); + } + else { + LoadCell.setCalFactor(calValue); // set calibration factor (float) + Serial.println("Startup + tare is complete"); + } + while (!LoadCell.update()); + Serial.print("Calibration factor: "); + Serial.println(LoadCell.getCalFactor()); + Serial.print("HX711 measured conversion time ms: "); + Serial.println(LoadCell.getConversionTime()); + Serial.print("HX711 measured sampling rate HZ: "); + Serial.println(LoadCell.getSPS()); + Serial.print("HX711 measured settlingtime ms: "); + Serial.println(LoadCell.getSettlingTime()); + Serial.println("Note that the settling time may increase significantly if you use delay() in your sketch!"); + if (LoadCell.getSPS() < 7) { + Serial.println("!!Sampling rate is lower than specification, check MCU>HX711 wiring and pin designations"); + } + else if (LoadCell.getSPS() > 100) { + Serial.println("!!Sampling rate is higher than specification, check MCU>HX711 wiring and pin designations"); + } +} +void loop() { + //update() should be called at least as often as HX711 sample rate; >10Hz@10SPS, >80Hz@80SPS + //use of delay in sketch will reduce effective sample rate (be carefull with delay() in the loop) + LoadCell.update(); + + //get smoothed value from data set + current calibration factor + if (millis() > t + 250) { + float i = LoadCell.getData(); + Serial.print("Load_cell output val: "); + Serial.println(i); + t = millis(); + } +} diff --git a/keywords.txt b/keywords.txt index 3a6255b..844a88e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -22,11 +22,18 @@ setCalFactor KEYWORD2 getCalFactor KEYWORD2 getData KEYWORD2 getSingleConversion KEYWORD2 +getSingleConversionRaw KEYWORD2 +getReadIndex KEYWORD2 +getConversionTime KEYWORD2 +getSPS KEYWORD2 +getTareTimeoutFlag KEYWORD2 +disableTareTimeout KEYWORD2 +getSettlingTime KEYWORD2 powerDown KEYWORD2 powerUp KEYWORD2 getTareOffset KEYWORD2 getTareStatus KEYWORD2 - +setTareOffset KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/library.properties b/library.properties index 1be276a..d83406b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=HX711_ADC -version=1.0.2 +version=1.1.0 author=Olav Kallhovd maintainer=Olav Kallhovd sentence=Arduino library for the HX711 24-bit ADC for weight scales diff --git a/src/HX711_ADC.cpp b/src/HX711_ADC.cpp index b146ae2..670903f 100644 --- a/src/HX711_ADC.cpp +++ b/src/HX711_ADC.cpp @@ -42,30 +42,33 @@ void HX711_ADC::begin(uint8_t gain) /* start(t): will do conversions continuously for 't' +400 milliseconds (400ms is min. settling time at 10SPS). * Running this for 1-5s before tare() seems to improve the tare accuracy */ -void HX711_ADC::start(unsigned int t) +int HX711_ADC::start(unsigned int t) { t += 400; while(millis() < t) { getData(); + yield(); } tare(); tareStatus = 0; } +/* startMultiple(t): use this if you have more than one load cell and you want to do tare and stabilization simultaneously. + Will do conversions continuously for 't' +400 milliseconds (400ms is min. settling time at 10SPS). +* Running this for 1-5s before tare() seems to improve the tare accuracy */ int HX711_ADC::startMultiple(unsigned int t) { if(startStatus == 0) { + unsigned long ts = millis(); if(isFirst) { t += 400; //min time for HX711 to be stable - timeStamp = millis(); isFirst = 0; } - if(millis() < timeStamp + t) { - //update(); //do conversions during stabi time - update(); + if(millis() < ts + t) { + update(); //do conversions during stabilization time return 0; } - else { //do tare after stabi time + else { //do tare after stabilization time is up doTare = 1; update(); if(convRslt == 2) { @@ -83,8 +86,17 @@ void HX711_ADC::tare() uint8_t rdy = 0; doTare = 1; tareTimes = 0; + tareTimeoutFlag = 0; + unsigned long timeout = millis() + tareTimeOut; while(rdy != 2) { rdy = update(); + if (!tareTimeoutDisable) { + if (millis() > timeout) { + tareTimeoutFlag = 1; + break; // Prevent endless loop if no HX711 is connected + } + } + yield(); } } @@ -111,13 +123,12 @@ float HX711_ADC::getCalFactor() //raw data is divided by this value to convert t return calFactor; } -//call update() in loop +//call function update() in loop //if conversion is ready; read out 24 bit data and add to data set, returns 1 //if tare operation is complete, returns 2 //else returns 0 uint8_t HX711_ADC::update() { - //#ifndef USE_PC_INT byte dout = digitalRead(doutPin); //check if conversion is ready if (!dout) { conversion24bit(); @@ -125,7 +136,6 @@ uint8_t HX711_ADC::update() } else convRslt = 0; return convRslt; - //#endif } float HX711_ADC::getData() // return fresh data from the moving average data set @@ -155,6 +165,8 @@ long HX711_ADC::smoothedData() uint8_t HX711_ADC::conversion24bit() //read 24 bit data and start the next conversion { + conversionTime = micros() - conversionStartTime; + conversionStartTime = micros(); unsigned long data = 0; uint8_t dout; convRslt = 0; @@ -219,17 +231,73 @@ void HX711_ADC::setTareOffset(long newoffset) tareOffset = newoffset; } -//for testing only: -//if ready: returns 24 bit data and starts the next conversion, else returns -1 +//for testing and debugging: +//returns single conversion and starts the next conversion if ready, else returns -1 float HX711_ADC::getSingleConversion() { long data = 0; byte dout = digitalRead(doutPin); //check if conversion is ready if (!dout) { - //data = conversion24bit(); + data = conversion24bit(); data = data - tareOffset; float x = (float) data/calFactor; return x; } else return -1; } + +//for testing and debugging: +//returns single conversion 24 bit raw data and starts the next conversion if ready, else returns -1 +long HX711_ADC::getSingleConversionRaw() +{ + long data = 0; + byte dout = digitalRead(doutPin); //check if conversion is ready + if (!dout) { + data = conversion24bit(); + return data; + } + else return -1; +} + +//for testing and debugging: +//returns current value of readIndex +int HX711_ADC::getReadIndex() +{ + return readIndex; +} + +//for testing and debugging: +//returns latest conversion time in millis +float HX711_ADC::getConversionTime() +{ + return conversionTime/1000.0; +} + +//for testing and debugging: +//returns the HX711 samples ea seconds based on the latest conversion time. +//The HX711 can be set to 10SPS or 80SPS. For general use the recommended setting is 10SPS. +float HX711_ADC::getSPS() +{ + float sps = 1000000.0/conversionTime; + return sps; +} + +//for testing and debugging: +//returns the tare timeout flag from the last tare operation. +//0 = no timeout, 1 = timeout +bool HX711_ADC::getTareTimeoutFlag() +{ + return tareTimeoutFlag; +} + +void HX711_ADC::disableTareTimeout() +{ + tareTimeoutDisable = 1; +} + +long HX711_ADC::getSettlingTime() +{ + long st = getConversionTime() * DATA_SET; + return st; +} + diff --git a/src/HX711_ADC.h b/src/HX711_ADC.h index 5571f70..ce0fbbf 100644 --- a/src/HX711_ADC.h +++ b/src/HX711_ADC.h @@ -10,10 +10,11 @@ #define HX711_ADC_h #include +#include "config.h" -#define SAMPLES 16 // no of samples in moving average data set, value must be 4, 8, 16, 32 or 64 -#define IGN_HIGH_SAMPLE 1 // adds one sample to the set and ignore peak high sample, value must be 0 or 1 -#define IGN_LOW_SAMPLE 1 // adds one sample to the set and ignore peak low sample, value must be 0 or 1 +/* +Note: HX711_ADC configuration values has been moved to file config.h +*/ #define DATA_SET SAMPLES + IGN_HIGH_SAMPLE + IGN_HIGH_SAMPLE // total samples in memory @@ -43,14 +44,21 @@ class HX711_ADC void setGain(uint8_t gain = 128); //value should be 32, 64 or 128* void begin(); void begin(uint8_t gain); - void start(unsigned int t); // start and tare one HX711 + int start(unsigned int t); // start and tare one HX711 int startMultiple(unsigned int t); //start and tare multiple HX711 simultaniously void tare(); // zero the scale, wait for tare to finnish void tareNoDelay(); // zero the scale, do tare in loop without waiting for tare to finnish void setCalFactor(float cal); //calibration factor, raw data is divided by this value to convert to readable data float getCalFactor(); // returns the current calibration factor float getData(); // returns data from the moving average data set - float getSingleConversion(); //for testing + float getSingleConversion(); //for testing and debugging + long getSingleConversionRaw(); //for testing and debugging + int getReadIndex(); //for testing and debugging + float getConversionTime(); //for testing and debugging + float getSPS(); //for testing and debugging + bool getTareTimeoutFlag(); //for testing and debugging + void disableTareTimeout(); //for testing and debugging + long getSettlingTime(); //for testing and debugging void powerDown(); void powerUp(); bool getTareStatus(); // returns 1 if tareNoDelay() operation is complete @@ -68,7 +76,8 @@ class HX711_ADC long dataSampleSet[DATA_SET + 1]; // data set, make voltile if interrupt is used long tareOffset; int readIndex = 0; - long timeStamp; + unsigned long conversionStartTime; + unsigned long conversionTime; uint8_t isFirst = 1; uint8_t tareTimes; const uint8_t divBit = DIVB; @@ -76,6 +85,9 @@ class HX711_ADC bool startStatus; uint8_t convRslt; bool tareStatus; + const unsigned int tareTimeOut = (DATA_SET * 110) + 500; // tare timeout time in ms, no of samples * 110ms (10SPS + 10%) + 500ms margin + bool tareTimeoutFlag; + bool tareTimeoutDisable = 0; }; #endif diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..c6cee1e --- /dev/null +++ b/src/config.h @@ -0,0 +1,18 @@ +/* +HX711_ADC configuration + +Allowed values for "SAMPLES" is 4, 8, 16, 32 or 64. +Higher value = improved filtering/smoothing of returned value, but longer setteling time and increased memory usage +Lower value = visa versa + +The settling time can be calculated as follows: +Settling time = SAMPLES + IGN_HIGH_SAMPLE + IGN_LOW_SAMPLE / SPS + +Example on calculating settling time using the values SAMPLES = 16, IGN_HIGH_SAMPLE = 1, IGN_LOW_SAMPLE = 1, and HX711 sample rate set to 10SPS: +(16+1+1)/10 = 1.8 seconds settling time + +*/ + +#define SAMPLES 16 // no of samples in moving average data set, value must be 4, 8, 16, 32 or 64 +#define IGN_HIGH_SAMPLE 1 // adds one sample to the set and ignore peak high sample, value must be 0 or 1 +#define IGN_LOW_SAMPLE 1 // adds one sample to the set and ignore peak low sample, value must be 0 or 1 \ No newline at end of file