I have temperature values from around 60 to 212 as floating point variables because I want one or two decimal point accuracy. These are to be displayed on Adafruit 7-segment displays. The documentation for these display devices is not very good, but I think I can figure out how to display one digit at a time and print a decimal point in front of the proper digit.
So, is there an easy way to pick off the four most significant digits of a floating point value, one at a time and detect which has the decimal point in front of it? I have checked the Arduino Language page and do not see any obvious solutions there.
Since this seems to be something many others have probably done, I am hoping there is best way to do it.
Take your float and convert it to an ascii string in a char array. Declare another array for what you will display.
Loop through the first four(if no decimal at all) or five (four digits and a decimal) elements in the array. Read through them one at a time and if they're digits add them to the display and if you hit a decimal point then deal with that separately. For all the seven segments I have the decimal is after the number, so it would mean when you hit a decimal you have to go back to the last digit you displayed and turn on the decimal point.
Then the display will read "100.10".
The decimal point always being in the same spot makes it easier to read and it usually looks better as well.
Consistency....
Worst case you can always check the last digit and if it's 0, divide by 10 and move the decimal point. still think 2 decimal digits consistently is preferred.
I can multiply by 10 when the temp is over 100 or multiply 100 when the temp is under 100 and end up with a four digit integer number and the location of the decimal point. That's great!
Now, how do I pick off one digit at a time from this four digit integer number so I can write it to the proper location on the four digit display. I can imagine a brute force method(count the number of times I can subtract 1000 without going below zero for the first digit etc.), but is there an easy way?
Converting to a character array also sounds easy, but I have to figure out how the result can be written to this display. As I said in my post, the documentation is not good.
oldradio:
Now, how do I pick off one digit at a time from this four digit integer number so I can write it to the proper location on the four digit display.
Right-to-left:
while (intTemp > 0)
{
byte digit = intTemp % 10;
// Write digit to display
intTemp /= 10;
}
Actually I only need .1 degree accuracy as the temperature sensor is .5 accurate. But, I have four digits to display, so why not. This is ona production still and temperatures below 100f aren't very imortant anyway.
int2str, could you explain the code you are suggesting? It looks elegant, but alas I'm not good enough to understand it. I do have the display at the link in your post, and believe me I have been trying to get meaningful documentation from them. It does not exist. Their web pages are a big easter egg hunt for information and there is no documentation that lists the commands (or whatever you call them) or their syntax for printing to the display. Instead, you go on to their forum and start asking questions.