I am reading battery voltage and lets say I get 12.61. The whole number and the decimal part will both be small enough to send each as a byte. I can use floor to get the 12, but how do I get the 61? I've tried modf but it returns a double.
What about just subtracting the integer value from the original value?
Something like this maybe
uint8_t byte1 = voltage;
uint8_t byte2 = (voltage - byte1) * 100;
Then multiplying x 100 to get rid of the decimal. That might work.
This issue with this is that my battery voltage is returned as a float not an uint8_t. I also have to add floor to the (voltage - floor(byte1). Even still I am losing some resolution somewhere. If voltage is 12.66, sometimes byte2 will be 58 or 64. Not sure what is going on there.
Yes voltage is a float, and storing a float in an int will truncate the decimal part of the float, so you don't need to use floor
(and this function returns a float anyway)
A 16 bit integer, e.g. int16_t i=100*(float_value)
, will also fit into two bytes. Send the integer and be done with it.
How do I add the integer into my array of bytes? Currently I just assign myArray[3]= byte1 and 4 to byte2. Do I have to do something special to send the int in these two bytes?
If I don't use floor it seems to subtract the voltage from the voltage which is 0, then multiplies that by 100 which always returns 0. Or am I missing something?
What "array of bytes"? If you have questions about code, post ALL the code, using code tags.
int16_t i=100*(float_value);
myArray[3]= i >> 8;
myArray[4]= i & 0xff;
thank you b707. My code is extremely to large to post. lol.
Very unlikely.
How do you know it's 12.66? And I assume you know that no "analog" measurement is "perfect"?
There is aways SOME noise and drift, and some offset & linearity error, plus ANY analog-to-digital conversion can be on the hairy edge between two counts. Do you know how much voltage resolution 1-count represents with your hardware? i.e. Maybe one count represents the difference between 0.58 & 0.64?
The "regular" Arduino with the 10-bit ADC (0-1023) and the default 5V reference has a resolution of about 5mV so when converted to voltage the readings "jump" in (about) 5mV increments.
Why?
The less someone know, the longer his code
Also, my code is not for the public. This isn't for a hobby project. Yes, there is some smoothing that has to be done on the readings. I take an average of a number of readings and based off a known good vref circuit that is designed specifically for vref input. So it is pretty close. I now have it with the averaging code to within .02 volts. That is close enough for this project. The reading was not the problem, it was getting the reading from the equipment to the handheld. I could have put the code on the handheld to do the math from the readings but I have more than one piece of equipment that the handheld talks to and each of those has its own circuitry and of course, my luck, most of the vref values are different. Therefore I need to do the calculations before I send the data. My hardware actually isn't arduino but the coding still works the same :).
You don't need to post your project code, just an example sketch that shows the problem
For a commercial product, hire coders by posting in the Arduino forum Jobs and Paid Collaboration forum section.
I just needed a question answered. Not the whole project written. But okay.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.