Pages: [1]   Go Down
 Author Topic: Float to array?  (Read 202 times) 0 Members and 1 Guest are viewing this topic.
Nova Scotia
Offline
Full Member
Karma: 3
Posts: 129
 « on: November 20, 2012, 03:25:30 pm » Bigger Smaller Reset

I have a few seven segment leds and I want the temperature ic float displayed. The code is set up so an integer is assigned to each digit.
(working test code)
int a=1;
int b=2;
int c=3;
write_digits(a,b,c);

Is there an easy way to have a float to array? Then my temperature ic could do something like:
float temperature = getTemp();
somehow put the float into an array
write_digits(temp[0],temp[1],temp[2],temp[3]);
 Logged

California
Online
Edison Member
Karma: 51
Posts: 2186
 « Reply #1 on: November 20, 2012, 03:30:56 pm » Bigger Smaller Reset

dtostrf()

Although, since you know the precision you want, you could just multiply it by 10^n, where n is how many digits you want, assign it to an int, and send it through atoi().
 « Last Edit: November 20, 2012, 03:40:16 pm by Arrch » Logged

Offline
Edison Member
Karma: 114
Posts: 2205
 « Reply #2 on: November 20, 2012, 03:31:08 pm » Bigger Smaller Reset

Quote
Then my temperature ic could do something like:

Typically done with divisions:
Code:
c=temperature % 10;  temperature / = 10;
b=temperature % 10;  temperature / = 10;
a=temperature % 10;

For that to be fast and accurate, you want temperature to be an integer.

So here is the irony of your proposal: you want to convert an integer into float, only to have to convert it back to integer so you can do what you are trying to do.

 Logged

Netherlands
Offline
Tesla Member
Karma: 101
Posts: 9553
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #3 on: November 20, 2012, 03:40:29 pm » Bigger Smaller Reset

dhenry is 100% right, integer math is much faster. however the last - % -modulo - is not needed

Code:
float temperature = getTemp();
int t = (int)temperature;
c= t % 10;  t /= 10;
b=t % 10;   t /= 10;
a=t;  // assume temp = 0..999
write_digits(a,b,c);
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Nova Scotia
Offline
Full Member
Karma: 3
Posts: 129
 « Reply #4 on: November 20, 2012, 07:36:31 pm » Bigger Smaller Reset

It works! Thanks to all for the help. I'm trying to figure it now since I stick to hardware design and rarely write code. I have no idea what the line t /= 10; is yet but i'll figure it out. I think I have the rest of it in my head.

Mike
 Logged

California
Online
Edison Member
Karma: 51
Posts: 2186
 « Reply #5 on: November 20, 2012, 09:09:53 pm » Bigger Smaller Reset

I have no idea what the line t /= 10; is yet but i'll figure it out. I think I have the rest of it in my head.

Mike

It's shorthand for writing out the full assignment statement:

t = t / 10.

In English, it says take the variable t, divide it by 10 and store it back in t.
 Logged

 Pages: [1]   Go Up