int analogval = analogRead(lightpin); //It needs an int to fit.
char text[5] = {0}; //this is the char* bit
sprintf(text,"%d",analogval); // convert a number to a string.
uoled.Text(0,0, 2, 150, text,1); //print the string.
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);
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);
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 .
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
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.
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!