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

Allow forcing a read and a few fixes #38

Merged
merged 5 commits into from
Sep 15, 2015

Conversation

matthijskooijman
Copy link
Contributor

I've found that when the Arduino employs sleeping, it can no longer properly read the sensor since millis() doesn't tick while sleeping (and the library now insists that the Arduino has been awake for 2 seconds since the last reading). I fixed this by adding a "force" parameter to the read methods.

While I was there, I did a few more cleanups of some code I encountered along the way, which should slightly simplify and shrink things (without losing any functionality).

This allows forcing a read, even if the previous read was less than 2
seconds ago. This is useful in cases where the millis() timer is not
reliable, such as when sleeping. In this case, it is up to the caller to
ensure that at least 2 seconds elapse between calls with force set to
true.
There was some handling of millis() overflow that is not needed.
Standard unsigned subtraction and wraparound already works as expected,
so this extra check can be removed (it even hurts, since it introduces 2
seconds after a wraparound where no new data will be read, even if it
could otherwise happen).

Also, this prevents calling millis() a second time, since its value is
already known.
By cleverly setting _lastreadtime in begin() to make sure that it looks
like the last read was alrady 2000 ms ago, even on startup when millis()
still returns 0, there is no longer a need to keep a separate
firstreading variable, saving a byte of memory and a bit of code.
There's only 5 bytes of data being transferred (2x2 bytes of data, 1
byte checksum), so no need to keep a useless byte.
Previously, the mode was configured to INPUT and then written HIGH,
which works on AVR but isn't very portable. Instead, this uses the
INPUT_PULLUP pin mode (available since Arduino 1.0.1) which is portable.
@matthijskooijman
Copy link
Contributor Author

@tdicola, any chance to merge this soon? I'm using this library in my book (which I'm finalizing this week), so I'd like to refer to a released version if possible. Of this commits, I really need the force parameter, the other ones are just cosmetic improvements.

@tdicola tdicola merged commit f9c3f32 into adafruit:master Sep 15, 2015
@tdicola
Copy link
Contributor

tdicola commented Sep 15, 2015

Awesome thanks for submitting those great fixes! Just gave it a quick test on an Uno and everything works well. Merged it in and will bump the version number so the library manager picks it up. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants