RGB to HEX

Arrch:
Another good example of why you should always explain what you are trying to accomplish instead of what you want to do.

Yes, i've learned that now. Here goes:

The documentation also says 'Definition at line 114 of file uOLED.cpp.'

Here's the part of uOLED.cpp and further:

void uOLED::Rectangle (char x1, char y1, char x2, char y2, int color, char filled)
{
  PenSize(filled);

  write(0x72);
  write(x1);
  write(y1);
  write(x2);
  write(y2);
  write(color >> 8);		
  write(color & 0xFF);
  res=RBack();
}

The (write) function is just

     pSerial->write(pData);

This is what the LCD datasheet has to say about color:

colour:   2 bytes (16 bits) define the background colour in RGB format: 
R4R3R2R1R0G5G4G3G2G1G0B4B3B2B1B0 where:
msb : R4R3R2R1R0G5G4G3
lsb :    G2G1G0B4B3B2B1B0

By the way, i have an uLCD-144 (SGC)

Hope this informs you

So then you don't have the full 8 (0-255) bits for each color. Red and blue can be 0-31, while green can be 0-63.

Ah, that is an RGB565 colour, not an RGB888. You can use the following to generate that.

int RGB = ((R & 0xF8) << 8) | ((G & 0xFC) << 3) | ((B & 0xF8) >> 3);

That assumes R,G,B are initially bytes where 255 = Full Colour, 0 = Black.

It compiles, fingers crossed!

Thank you all so much, it works.

Now i have just one question:

I also want to display text of the value from an analog pin. With this code:

char analogval = analogRead(lightpin);
uoled.Text(0,0, 2, 150, analogval,1);

But the Text function needs a char* i think, because i get this error when compiling:
Invalid conversion from 'char' to 'char*'.

analogRead returns 10 bits, so will not in a char, and is an unsigned value anyway.

AWOL:
so will not in a char

How would i do it then?

As such:

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.

Okay, thanks. That worked.

Now i'm trying to print a value with 1 decimal using this code:

float time = 0.1; 
char timechar[5] = {0};
sprintf(timechar,"%d", time);
uoled.TextGraphic(5,40, 1, 150, 1, 1, timechar, 1);

But on the screen, it shows up as '-13107'

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);

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

Never mind, already figured it out. Thanks!

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 .

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?

Does anybody know what is causing this?

Yes; inappropriate use of "float".

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.

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.

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!