Measuring battery voltage, which way is best?
-
My pull request has now been merged to the MySensors development branch. I have updated the battery page to show the example sketch for internal measurement. Note that the internal measurement method does not work on all supported mcus (not esp8266 for example) but I don't think it matter much since the battery page recommends a Pro Mini which works.
Special thanks to @hek for tracking down a tricky display problem with the sketch inclusion, and to @Yveaux, @user2684 and @tekka for feedback on the pull request.
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
-
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
@skywatch Rounding up to the nearest int ?
-
@mfalkvidd Good News! :)
But I am curious where the "+ 0.5" vomes from in this line.....
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;???
@skywatch as @zboblamont said, it makes sure we have ”proper” rounding. In C, floating point numbers are always rounded down Ehrn converted to an integer. So 42.8 becomes 42. By adding 0.5, we get the rounding expected by most humans in most cases.
To read about different rounding methods, see https://en.wikipedia.org/wiki/Rounding#Rounding_to_the_nearest_integer
My personal favorite is called ”banker’s rounding” but I did not use it in this sketch.
-
@zboblamont & @mfalkvidd - Thank you for that. It never occured to me that rounding might need some help. But now it makes logical sense.
@mfalkvidd I guess if you used 'bankers rounding' you would always end up with 0.