Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typo on doubleWrite pin check #252

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
35 changes: 35 additions & 0 deletions examples/HX711_2_basic_example/HX711_2_basic_example.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "HX711_2.h"

// HX711 circuit wiring
const int LOADCELL_SCK_PIN = 2;
const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_DOUT2_PIN = 4;

HX711_2 scale;

void setup() {
Serial.begin(57600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_DOUT2_PIN, LOADCELL_SCK_PIN);
}

void loop() {

scale.power_up();

if (scale.is_ready()) {
long reading[2];

scale.read(reading);

Serial.print("HX711 readings: ");
Serial.print(reading[0]);
Serial.print(" ");
Serial.println(reading[1]);
} else {
Serial.println("HX711 not found.");
}

scale.power_down();

delay(1000);
}
24 changes: 15 additions & 9 deletions src/HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ uint8_t shiftInSlow(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
for(i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH);
delayMicroseconds(1);
digitalWrite(clockPin, LOW);
if(bitOrder == LSBFIRST)
value |= digitalRead(dataPin) << i;
else
value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, LOW);
delayMicroseconds(1);
}
return value;
Expand Down Expand Up @@ -104,10 +104,12 @@ void HX711::set_gain(byte gain) {

}

long HX711::read() {
long HX711::read(unsigned long timeout) {

// Wait for the chip to become ready.
wait_ready();
if (!wait_ready_timeout(timeout)) {
return 0;
}

// Define structures for reading data into.
unsigned long value = 0;
Expand Down Expand Up @@ -152,11 +154,11 @@ long HX711::read() {
// Set the channel and the gain factor for the next reading using the clock pin.
for (unsigned int i = 0; i < GAIN; i++) {
digitalWrite(PD_SCK, HIGH);
#if ARCH_ESPRESSIF
#if FAST_CPU
delayMicroseconds(1);
#endif
digitalWrite(PD_SCK, LOW);
#if ARCH_ESPRESSIF
#if FAST_CPU
delayMicroseconds(1);
#endif
}
Expand Down Expand Up @@ -218,8 +220,8 @@ bool HX711::wait_ready_retry(int retries, unsigned long delay_ms) {
bool HX711::wait_ready_timeout(unsigned long timeout, unsigned long delay_ms) {
// Wait for the chip to become ready until timeout.
// https://github.com/bogde/HX711/pull/96
unsigned long millisStarted = millis();
while (millis() - millisStarted < timeout) {
unsigned long stopAt = millis() + timeout;
while (millis() < stopAt) {
if (is_ready()) {
return true;
}
Expand All @@ -239,15 +241,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 get_value(times) / (SCALE == 0 ? 1 : SCALE);
}

void HX711::tare(byte times) {
set_offset(0);
double sum = read_average(times);
set_offset(sum);
}
Expand All @@ -270,6 +273,9 @@ long HX711::get_offset() {

void HX711::power_down() {
digitalWrite(PD_SCK, LOW);
#if FAST_CPU
delayMicroseconds(1);
#endif
digitalWrite(PD_SCK, HIGH);
}

Expand Down
6 changes: 3 additions & 3 deletions src/HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HX711
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
float SCALE = 1.f; // used to return weight in grams, kg, ounces, whatever

public:

Expand Down Expand Up @@ -54,13 +54,13 @@ class HX711
void set_gain(byte gain = 128);

// waits for the chip to be ready and returns a reading
long read();
long read(unsigned long timeout = 1000);

// returns an average reading; times = how many times to read
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
Expand Down
Loading