From b32d8d57ac9ffc615e686109a661f3b079b80ec2 Mon Sep 17 00:00:00 2001 From: wang Date: Wed, 30 Aug 2017 13:39:20 +0800 Subject: [PATCH] fix variable type declare. Add the delay when doing set_gain or power_up. Add new demo, base on HX711SerialBegin. Signed-off-by: wang --- HX711.cpp | 18 ++-- HX711.h | 6 +- examples/HX711SerialDemo/HX711SerialDemo.ino | 107 +++++++++++++++++++ 3 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 examples/HX711SerialDemo/HX711SerialDemo.ino diff --git a/HX711.cpp b/HX711.cpp index c80524e..13d75cb 100644 --- a/HX711.cpp +++ b/HX711.cpp @@ -7,11 +7,13 @@ void yield(void) {}; #endif -HX711::HX711(byte dout, byte pd_sck, byte gain) { +HX711::HX711(byte dout, byte pd_sck, byte gain) : + OFFSET(0), SCALE(1.0) { begin(dout, pd_sck, gain); } -HX711::HX711() { +HX711::HX711() : + GAIN(1), PD_SCK(-1), DOUT(-1), OFFSET(0), SCALE(1.0) { } HX711::~HX711() { @@ -45,7 +47,7 @@ void HX711::set_gain(byte gain) { } digitalWrite(PD_SCK, LOW); - read(); + read_average(1 + 6); //6 times is invalid. } long HX711::read() { @@ -95,16 +97,16 @@ long HX711::read_average(byte times) { return sum / times; } -double HX711::get_value(byte times) { +long HX711::get_value(byte times) { return read_average(times) - OFFSET; } float HX711::get_units(byte times) { - return get_value(times) / SCALE; + return (float) get_value(times) / SCALE; } void HX711::tare(byte times) { - double sum = read_average(times); + long sum = read_average(times); set_offset(sum); } @@ -130,5 +132,9 @@ void HX711::power_down() { } void HX711::power_up() { + long dump = 0; digitalWrite(PD_SCK, LOW); + //When power up, default gain is 128, channel A. + //rate is 10Hz, so 400ms is 5 times. + dump = read_average(6); //5 times is invalid. } diff --git a/HX711.h b/HX711.h index 23b2234..600973e 100644 --- a/HX711.h +++ b/HX711.h @@ -13,8 +13,8 @@ class HX711 byte PD_SCK; // Power Down and Serial Clock Input Pin byte DOUT; // Serial Data Output Pin byte GAIN; // amplification factor - long OFFSET = 0; // used for tare weight - float SCALE = 1; // used to return weight in grams, kg, ounces, whatever + long OFFSET; // used for tare weight + float SCALE; // used to return weight in grams, kg, ounces, whatever public: // define clock and data pin, channel, and gain factor @@ -46,7 +46,7 @@ class HX711 long read_average(byte times = 10); // returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do - double get_value(byte times = 1); + long get_value(byte times = 1); // returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration // times = how many readings to do diff --git a/examples/HX711SerialDemo/HX711SerialDemo.ino b/examples/HX711SerialDemo/HX711SerialDemo.ino new file mode 100644 index 0000000..f54e086 --- /dev/null +++ b/examples/HX711SerialDemo/HX711SerialDemo.ino @@ -0,0 +1,107 @@ +#include "HX711.h" + +HX711 scale; +void loop_test_hx711() { + int which = 0; + uint32_t preMillis = 0L; + while (which < 5) { + //Power cycle takes around 400ms so only do so if our test rate is greater than 500ms + if ((millis() - preMillis) >= 5000) { + switch (which) { + case 0: + case 2: + case 4: + scale.power_down(); + Serial.print(F("powerDownScale\n")); + delay(1000); + Serial.print(F("powerUpScale\n")); + scale.power_up(); + break; + case 1: + scale.set_gain(128); + Serial.print(F("set_gain=128\n")); + break; + case 3: + scale.set_gain(64); + Serial.print(F("set_gain=64\n")); + break; + default: + which = 0; + break; + } + preMillis = millis(); + which++; + } + + float currentReading = 0; + //Print raw reading + long rawReading = scale.read(); + Serial.print(millis()); + Serial.print(F(",")); + Serial.print(rawReading, HEX); + Serial.print(F(",\t")); + //Take average of readings with calibration and tare taken into account + currentReading = scale.get_units(1); + Serial.print(millis()); + Serial.print(F(",")); + //Print calibrated reading + Serial.print(currentReading, 3); + Serial.println(); + } + Serial.println(F("Loop test is done!")); +} +void setup() { + Serial.begin(38400); + Serial.println("HX711 Demo"); + Serial.println("Initializing the scale"); + // parameter "gain" is ommited; the default value 128 is used by the library + // HX711.DOUT - pin #A1 + // HX711.PD_SCK - pin #A0 + scale.begin(A1, A0); + loop_test_hx711(); + + Serial.println("Before setting up the scale:"); + Serial.print("read: \t\t"); + Serial.println(scale.read()); // print a raw reading from the ADC + + Serial.print("read average: \t\t"); + Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC + + Serial.print("get value: \t\t"); + Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet) + + Serial.print("get units: \t\t"); + Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided + // by the SCALE parameter (not set yet) + + scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details + scale.tare(); // reset the scale to 0 + + Serial.println("After setting up the scale:"); + + Serial.print("read: \t\t"); + Serial.println(scale.read()); // print a raw reading from the ADC + + Serial.print("read average: \t\t"); + Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC + + Serial.print("get value: \t\t"); + Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare() + + Serial.print("get units: \t\t"); + Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided + // by the SCALE parameter set with set_scale + + Serial.println("Readings:"); +} + +void loop() { + Serial.print("one reading:\t"); + Serial.print(scale.get_units(), 1); + Serial.print("\t| average:\t"); + Serial.println(scale.get_units(10), 1); + + scale.power_down(); // put the ADC in sleep mode + delay(5000); + scale.power_up(); +}