Go Down

### Topic: RGB to HEX (Read 7639 times)previous topic - next topic

#### Tom Carpenter

#30
##### Aug 11, 2012, 07:54 pmLast Edit: Aug 11, 2012, 07:56 pm by Tom Carpenter Reason: 1
Thats because %d means an integer. %f means a floating point, however due to the way the arduino compiler is setup, floating point in the **printf functions is disabled.

For 1dp, you can do something like this:

Code: [Select]
`float time = 0.1;int exponent = (int)time; //exponent is the number to the left of the decimal pointtime -= (float)exponent; //remove the exponent to leave just the fraction.int fraction = (int)(time*10.0); //fraction is the 1st number to the right of the decimal pointchar timechar[5] = {0};sprintf(buffer,"%d.%d",exponent,fraction);uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);`
~Tom~

#### lyron

#31
##### Aug 11, 2012, 08:04 pm
It gave me some errors during compiling so i had to change the code to this:
Code: [Select]
`float someFloat;float time = 0.1;int exponent = (int)someFloat; //exponent is the number to the left of the decimal pointtime -= (float)exponent; //remove the exponent to leave just the fraction.int fraction = (int)(someFloat*10.0); //fraction is the 1st number to the right of the decimal pointchar timechar[5] = {0};sprintf(timechar,"%d.%d",exponent,fraction);uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);`

But now it just prints "0.0" to the screen...

#### lyron

#32
##### Aug 11, 2012, 08:16 pm
Never mind, already figured it out. Thanks!

#### Tom Carpenter

#33
##### Aug 11, 2012, 08:52 pm
Yeah sorry, that was me copying code and forgetting to change the variable names. You'll need to be using the version that I corrected, as the one in your post would always give 0.0 .
~Tom~

#### lyron

#34
##### Aug 15, 2012, 07:18 pm
I ran into a problem and i can't solve it.

I'm using this code:
Code: [Select]
`while (counting == 1){if(digitalRead(leftpin) == LOW){counting = 0;}if(counting == 1){someFloat = (float) someFloat + 0.1;}int exponent = (int)someFloat; //exponent is the number to the left of the decimal pointtime -= (float)exponent; //remove the exponent to leave just the fraction.int fraction = (int)(someFloat*10.0); //fraction is the 1st number to the right of the decimal pointchar timechar[5] = {0};sprintf(timechar,"%d.%d Seconds",exponent,fraction);uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);delay(100);}  `

And now when it's counting it does fine, until it reaches one second, after that it does this:
1.10
1.11
1.12
1.13
1.14
~
1.19
2.20
2.21
~
2.29
3.30

Does anybody know what is causing this?

#### AWOL

#35
##### Aug 15, 2012, 07:27 pm
Quote
Does anybody know what is causing this?
Yes; inappropriate use of "float".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### PaulS

#36
##### Aug 15, 2012, 08:10 pm
Code: [Select]
`if(counting == 1){someFloat = (float) someFloat + 0.1;}`
If someFloat is a float, there is no reason to cast it to a float.

Code: [Select]
`int exponent = (int)someFloat; //exponent is the number to the left of the decimal point`
The portion on the left of the decimal point is NOT the exponent. Really poor choice of name.

Code: [Select]
`int fraction = (int)(someFloat*10.0); //fraction is the 1st number to the right of the decimal point`
Multiplying someFloat by 10, without having removed the integer portion will NOT give you correct results.

The art of getting good answers lies in asking good questions.

#### Tom Carpenter

#37
##### Aug 15, 2012, 10:32 pmLast Edit: Aug 15, 2012, 10:44 pm by Tom Carpenter Reason: 1
Maybe a poor choice of words on my part, technically it should be integral (or integer if you prefer). But never mind.

The problem is:
Code: [Select]
`time -= (float)exponent; //remove the exponent to leave just the fraction.`

should be:
Code: [Select]
`someFloat-= (float)exponent; //remove the exponent to leave just the fraction.`

You didn't look at the corrected version as I suggested.
~Tom~

#### GoForSmoke

#38
##### Aug 16, 2012, 01:47 am
not tested but I can spell cat without a dictionary:

float f = 12.3;
int a = f * 10.0;
int leftOfDec = (int) f;
int rightOfDec = a % 10;
Serial.print( leftOfDec );
Serial.print( "." );
Serial.print( rightOfDec );

Personally, I like to leave the floats out and if I need to work to 10ths I make my unit 10ths.

int f = 123; // as in 123 10ths
int leftOfDec = f / 10;
int rightOfDec = f % 10;
Serial.print( leftOfDec );
Serial.print( "." );
Serial.print( rightOfDec ); // prints out as 12.3 1's

That has advantages of running faster and never confusing 1 with .999999999 or similar.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### lyron

#39
##### Aug 16, 2012, 11:59 am

Maybe a poor choice of words on my part, technically it should be integral (or integer if you prefer). But never mind.

Already figured it out, this is the new code i'm using:
Code: [Select]
`int integral = (int)someFloat; //integral is the number to the left of the decimal pointint fraction = (int)((someFloat - integral)*10.0); //remove integral to leave just fraction, fraction is the 1st number to the right of the decimal pointchar timechar[5] = {0};sprintf(timechar,"%d.%d Seconds",integral,fraction);uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);`

And now my stopwatch function is working, thank you!

Go Up