Leeds, UK
Offline
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #30 on: August 11, 2012, 12:54:08 pm » |
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: 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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #31 on: August 11, 2012, 01:04:17 pm » |
It gave me some errors during compiling so i had to change the code to this: 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
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #32 on: August 11, 2012, 01:16:36 pm » |
Never mind, already figured it out. Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #33 on: August 11, 2012, 01:52:43 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 .
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #34 on: August 15, 2012, 12:18:13 pm » |
I ran into a problem and i can't solve it. I'm using this 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
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #35 on: August 15, 2012, 12:27:13 pm » |
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.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #36 on: August 15, 2012, 01:10:34 pm » |
if(counting == 1){someFloat = (float) someFloat + 0.1;} If someFloat is a float, there is no reason to cast it to a float. 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. 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
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #37 on: August 15, 2012, 03:32:21 pm » |
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: time -= (float)exponent; //remove the exponent to leave just the fraction. should be: 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
Faraday Member
Karma: 30
Posts: 2918
I only know some basic electricity....
|
 |
« Reply #38 on: August 15, 2012, 06:47:11 pm » |
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
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 192
|
 |
« Reply #39 on: August 16, 2012, 04:59:10 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: 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
|
|
|
|
|
|