Pages: [1]   Go Down
Author Topic: Display floating point temperature on 7-segment display  (Read 1036 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks,

Barry
Logged

Offline Offline
Sr. Member
****
Karma: 8
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Multiply the float by 100 and convert it to an integer.

Code:
int intTemp = floatTemp * 100;

This will turn 12.3456 into 1234.
Now you can display the digits onto the 7 segment display and simply enable the dot before the last two digits.
Logged

Offline Offline
Edison Member
*
Karma: 32
Posts: 1370
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try dtostrf()
http://forum.arduino.cc/index.php?topic=85523.0

I used this for this exact thing, and simply did a search for the '.' and printed the digits on either side as needed.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

USA
Offline Offline
Jr. Member
**
Karma: 2
Posts: 87
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Multiply the float by 100 and convert it to an integer.

Code:
int intTemp = floatTemp * 100;

This will turn 12.3456 into 1234.
Now you can display the digits onto the 7 segment display and simply enable the dot before the last two digits.

How about if the temperature is 100.1?
Logged


Offline Offline
Sr. Member
****
Karma: 8
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about if the temperature is 100.1?

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Sr. Member
****
Karma: 8
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
while (intTemp > 0)
{
    byte digit = intTemp % 10;
    // Write digit to display
    intTemp /= 10;
}
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 362
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's probably just me but I see no reason to display Fahrenheit temperatures to 1/100 of a degree accuracy.

Is the sensor even that accurate?
Logged

Offline Offline
Sr. Member
****
Karma: 8
Posts: 272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Btw. which Adafruit display do you have (link?)?
I'm sure there is a datasheet to go with it....

Is it any of these:
http://www.adafruit.com/category/37_103
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

barry
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int2str, no need to reply, I figured out how your code works. Very clever!

Thanks,

Barry
Logged

Pages: [1]   Go Up
Jump to: