[SOLVED] How do I show HEX as capital letters?

I have built a small unit consisting of 3 variable resistors and a ST7735 display connected to a nano.
The programme accepts the input from three analog pins and shows the three colours as circles then mixes them together to show the result of all three.
I have a small problem, the number beside the colour circle is in Hexadecimal but the letter is in lower case, is it possible to convert it to make it show in upper case?

/*
Arduino TFT text example for ST7735
LED - n/c
SCL - D13
SDA - D11
DC - D9
Reset - D8
CS - D10
GND - GND
VCC - 5v
  This example demonstrates how to draw text on the
  TFT with an Arduino. The Arduino reads the value
  of an analog sensor attached to pin A0, and writes
  the value to the LCD screen, updating every
  quarter second. (no need for the analog sensor).
see    examples/TFT/arduino/TFTDisplayText   for original
 */
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs   10
#define dc   9
#define rst  8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char numblue[4];//to print number on screen
char numgreen[4];
char numred[4];
void setup() {
Serial.begin(9600);
  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();
  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
}
void loop() {
TFTscreen.stroke(0,0,0);// erase the text you just wrote
TFTscreen.text(numblue,23,2);//text,left edge, down 70
TFTscreen.stroke(0,0,0);// erase the text you just wrote
TFTscreen.text(numgreen,23,26);//text,left edge, down 70
TFTscreen.stroke(0,0,0);// erase the text you just wrote
TFTscreen.text(numred,23,50);//text,left edge, down 70

int blue = analogRead(A0);// Read the value of the sensor on A0
//convert it so it can be read as a number on screen
blue = map(blue, 0, 1023, 0, 254);
String sensorblue = String(blue,HEX);//to print number on screen
sensorblue.toCharArray(numblue, 4);//to print number on screen

int green = analogRead(A1);// Read the value of the sensor on A1
//convert it so it can be read as a number on screen
green = map(green, 0, 1023, 0, 254);
String sensorgreen = String(green,HEX);//to print number on screen
sensorgreen.toCharArray(numgreen, 4);//to print number on screen

int red = analogRead(A2);// Read the value of the sensor on A2
//convert it so it can be read as a number on screen
red = map(red, 0, 1023, 0, 254);
String sensorred = String(red,HEX);//to print number on screen
sensorred.toCharArray(numred, 4);//to print number on screen

TFTscreen.stroke(255,0,0);//blue letters/numbers
TFTscreen.setTextSize(2);
TFTscreen.text(numblue,23,2);//print text,left edge, down 2

TFTscreen.stroke(0,255,0);//green letters/numbers
TFTscreen.setTextSize(2);
TFTscreen.text(numgreen,23,26);//print text,left edge, down 26

TFTscreen.stroke(0,0,255);//red letters/numbers
TFTscreen.setTextSize(2);
TFTscreen.text(numred,23,50);//print text,left edge, down 50
delay(500);
TFTscreen.stroke(blue,0,0);//blue colour for circle
TFTscreen.fill(blue,0,0);//fill circle with blue
TFTscreen.circle(10,10,10);//print circle
delay(10);
TFTscreen.stroke(0,green,0);//green colour for circle
TFTscreen.fill(0,green,0);//fill circle with green
TFTscreen.circle(10,33,10);//print circle
 delay(10);
TFTscreen.stroke(0, 0,red);//red colour for circle
TFTscreen.fill(0,0,red);//fill circle with red
TFTscreen.circle(10,57,10);//print circle
delay(10);
TFTscreen.stroke(blue,green,red);//just blue altering for box
TFTscreen.fill(blue,green,red);//just changing blue
TFTscreen.rect(10,70,30,21);//build rectangle
TFTscreen.circle(10,80,10);//put circle on one end
TFTscreen.circle(40,80,10);//put circle on other end
}

I don't know how with Strings.


// somewhere make a place for the text, you can reuse this
char printBuffer[32];  // just make it big enough plus 1

// get the value
red = map(red, 0, 1023, 0, 254);


// print it with formatting into the buffer
sprintf(printBuffer, "%02X", red);


// put it on the screen
TFTscreen.text(printBuffer, 23, 50); //print text,left edge, down 50

Sry it's going to have to be weaved into your code, but the main point is sprintf() for formatted printing.

HTH

a7

1 Like

One way is to use snprintf and specify the exact format you want rather than String and accepting what it gives you.

(Untested)

/*
Arduino TFT text example for ST7735
LED - n/c
SCL - D13
SDA - D11
DC - D9
Reset - D8
CS - D10
GND - GND
VCC - 5v
  This example demonstrates how to draw text on the
  TFT with an Arduino. The Arduino reads the value
  of an analog sensor attached to pin A0, and writes
  the value to the LCD screen, updating every
  quarter second. (no need for the analog sensor).
see    examples/TFT/arduino/TFTDisplayText   for original
 */
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char numblue[4];  //to print number on screen
char numgreen[4];
char numred[4];
void setup() {
   Serial.begin(9600);
   // Put this line at the beginning of every sketch that uses the GLCD:
   TFTscreen.begin();
   // clear the screen with a black background
   TFTscreen.background(0, 0, 0);
}
void loop() {
   TFTscreen.stroke(0, 0, 0);         // erase the text you just wrote
   TFTscreen.text(numblue, 23, 2);    //text,left edge, down 70
   TFTscreen.stroke(0, 0, 0);         // erase the text you just wrote
   TFTscreen.text(numgreen, 23, 26);  //text,left edge, down 70
   TFTscreen.stroke(0, 0, 0);         // erase the text you just wrote
   TFTscreen.text(numred, 23, 50);    //text,left edge, down 70

   int blue = analogRead(A0);  // Read the value of the sensor on A0
   //convert it so it can be read as a number on screen
   blue = map(blue, 0, 1023, 0, 254);
   snprintf(numblue, sizeof numblue, "%02X", blue);  //to print number on screen

   int green = analogRead(A1);  // Read the value of the sensor on A1
   //convert it so it can be read as a number on screen
   green = map(green, 0, 1023, 0, 254);
   snprintf(numgreen, sizeof numgreen, "%02X", green);  //to print number on screen

   int red = analogRead(A2);  // Read the value of the sensor on A2
   //convert it so it can be read as a number on screen
   red = map(red, 0, 1023, 0, 254);
   snprintf(numred, sizeof numred, "%02X", red);  //to print number on screen

   TFTscreen.stroke(255, 0, 0);  //blue letters/numbers
   TFTscreen.setTextSize(2);
   TFTscreen.text(numblue, 23, 2);  //print text,left edge, down 2

   TFTscreen.stroke(0, 255, 0);  //green letters/numbers
   TFTscreen.setTextSize(2);
   TFTscreen.text(numgreen, 23, 26);  //print text,left edge, down 26

   TFTscreen.stroke(0, 0, 255);  //red letters/numbers
   TFTscreen.setTextSize(2);
   TFTscreen.text(numred, 23, 50);  //print text,left edge, down 50
   delay(500);
   TFTscreen.stroke(blue, 0, 0);  //blue colour for circle
   TFTscreen.fill(blue, 0, 0);    //fill circle with blue
   TFTscreen.circle(10, 10, 10);  //print circle
   delay(10);
   TFTscreen.stroke(0, green, 0);  //green colour for circle
   TFTscreen.fill(0, green, 0);    //fill circle with green
   TFTscreen.circle(10, 33, 10);   //print circle
   delay(10);
   TFTscreen.stroke(0, 0, red);   //red colour for circle
   TFTscreen.fill(0, 0, red);     //fill circle with red
   TFTscreen.circle(10, 57, 10);  //print circle
   delay(10);
   TFTscreen.stroke(blue, green, red);  //just blue altering for box
   TFTscreen.fill(blue, green, red);    //just changing blue
   TFTscreen.rect(10, 70, 30, 21);      //build rectangle
   TFTscreen.circle(10, 80, 10);        //put circle on one end
   TFTscreen.circle(40, 80, 10);        //put circle on other end
}
1 Like

Yes, better. snprintf() is the modern safe way to do the sprintf(), the only disadvantage is that is hard to pronounce. :expressionless:

And using multiple small buffers is no large deal, so this is the same idea, weaved into @matelot's code.

I can't test it, but it looks plausible.

a7

1 Like

There is a toUpperCase() function for converting the String to all upper case letters
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/touppercase/

But my preference would be sprintf() or snprintf() since you are converting the String to a char array anyway.

2 Likes

Thank you so much for the responses.
The snprintf works perfectly, even if it is a bit hard to pronounce :).
I appreciate all your input, as you can see from the picture I have altered the positions of the elements so the numbering shows as it would in a coinverter.

Much better.

1 Like

Do people actually pronounce sprintf as sprint-eff, rather than ess-print-eff?
Huh. Reminds me of a function we used to clear out dead data: "bones()", which I thought was pronounced "bones", as in "make it dead." Actually, it was the complement of bzero(): "bee-ones." oops.

2 Likes

Does it even need a pronunciation at all?

As for myself, I am happy with (for example) uint32_t rather than unsigned long, because code isn't meant to be pronounced.

you pronounce it "S N print F"

there is some history there

initially you had the printf() function that means "print formatted" and the printing was going to the standard output.

if you wanted the result to go to a string (a c-string, a null terminated char array) then you would use sprintf(), pronounced "S print F", where the leading s means it was a string printf.

Then people got worried that if the char array you were passing as the first parameter was not large enough to hold the resulting string, you would have a buffer overflow and your program would crash and it was/has been used as a way to inject viruses with controlled buffer overflow.

So the function was extended to pass the maximum Number of bytes to be used in the buffer (the generated string has a length of at most n-1, leaving space for the additional terminating null character)

That's how you got to snprinf(). s because the output is in a string, n because you pass then the number of bytes to be used, print because that's what the function does and f because you provide a format