Pages: 1 2 [3]   Go Down
Author Topic: RGB to HEX  (Read 1728 times)
0 Members and 1 Guest are viewing this topic.
Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
float time = 0.1;
int exponent = (int)time; //exponent is the number to the left of the decimal point
time -= (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 point
char timechar[5] = {0};
sprintf(buffer,"%d.%d",exponent,fraction);
uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);
« Last Edit: August 11, 2012, 12:56:27 pm by Tom Carpenter » Logged

~Tom~

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It gave me some errors during compiling so i had to change the code to this:
Code:
float someFloat;
float time = 0.1;
int exponent = (int)someFloat; //exponent is the number to the left of the decimal point
time -= (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 point
char 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...
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind, already figured it out. Thanks!
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

~Tom~

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran into a problem and i can't solve it.

I'm using this code:
Code:
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 point
time -= (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 point
char 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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does anybody know what is causing this?
Yes; inappropriate use of "float".
Logged

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(counting == 1){someFloat = (float) someFloat + 0.1;}
If someFloat is a float, there is no reason to cast it to a float.

Code:
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:
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.

Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
time -= (float)exponent; //remove the exponent to leave just the fraction.

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

You didn't look at the corrected version as I suggested.
« Last Edit: August 15, 2012, 03:44:41 pm by Tom Carpenter » Logged

~Tom~

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int integral = (int)someFloat; //integral is the number to the left of the decimal point
int fraction = (int)((someFloat - integral)*10.0); //remove integral to leave just fraction, fraction is the 1st number to the right of the decimal point
char 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!
Logged

Pages: 1 2 [3]   Go Up
Jump to: