-
Notifications
You must be signed in to change notification settings - Fork 544
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
Halting CPU by the "read()" function at the end of the "set_gain(byte gain)" when no response from the module #135
Comments
Dear Hamid, thanks for writing in and for spotting this issue. I will split my answer into two halves. Does
|
According to the datasheet:
So to set gain and channel you need to pulse the clock pin a number of times (25 times for channel A, gain 128, 26 times for channel B, gain 32, 27 times for channel A, gain 64). However, since that's done when you call the read function anyway, the call to read() in set_gain() can be removed. At least that's what I think, any other feedback is welcome, as usual. I honestly don't remember why it was done like that, I probably thought the chip needs to have the gain set before the actual reading, but now I don't think that's the case. |
like what bodge said the gain value always sending by any read() requests so I do not see the necessity of read() when changing gain. |
Removing
|
digitalWrite(PD_SCK, LOW); | |
read(); |
is actually redundant to
Lines 147 to 157 in 0b07b31
// 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 | |
delayMicroseconds(1); | |
#endif | |
digitalWrite(PD_SCK, LOW); | |
#if ARCH_ESPRESSIF | |
delayMicroseconds(1); | |
#endif | |
} |
then we should just get rid of it and start testing it on real hardware, right? Would you be able to do that, Hamid?
It would be a huge improvement to remove the interaction with the hardware from set_gain()
altogether, as it would significantly reduce the chance to get caught into the spin lock when starting the driver before the timeout-enabled functions and will enable the library to operate without any hardware attached at all.
Using wait_ready_timeout()
But if there's any reason then you can use your new ready-timeout request function [...] and move the
read()
inside thewait_ready_timeout()
condition.
While this is the case already in the example code like
HX711/examples/HX711_timeout_example/HX711_timeout_example.ino
Lines 16 to 22 in 0b07b31
if (scale.wait_ready_timeout(1000)) { | |
long reading = scale.read(); | |
Serial.print("HX711 reading: "); | |
Serial.println(reading); | |
} else { | |
Serial.println("HX711 not found."); | |
} |
set_gain()
before and the hardware is faulty or not even connected at all.
Yes Andreas but you can not use " if (scale.wait_ready_timeout(1000)) { begin() } " because you should first initialized the pins which happens in begin() function. |
Sure, but the overall program will start working as intended as is, because your proposal will mitigate the last chance to preliminary get into the spin lock through calling Saying this, I still might be missing an important detail you might want to tell me here. If this is the case, please keep going telling me the truth! Otherwise: All good and go ahead.
Thanks already! Will you be able to send a pull request with your amendments and tell us about the hardware you tested this on? |
Are you planning to modify your code to avoid this issue? |
Hi and best regards to the creators.
the "read()" function at the end of the "set_gain(byte gain)" cause halting CPU when there is no response from module when calling "begin()" .
I think the simplest solution is to remove the "read()" request at the end of "set_gain(byte gain)" function.
The text was updated successfully, but these errors were encountered: