4 digit 7 segment display hex conversion

I have a AZ-Delivery display that works with TM1637 library. To display a temperature I could use an Hex number like "22C". It works fine.

The library function is:

  display.showNumberHexEx(0x22C,0b00000000,false,3);          // shows: _22C_

If I replace the first parameter with a int variable (temp = 0x22C) it also works fine. Now I would want to make it work with any number like I could be composing a string (i.e. "0x" + int + "C") ... which function should I use for the conversion?
Tks
Flavio

Use the function in line 42.

It is a number, not a string. Try like this:

int t = 27;
display.showNumberHexEx(t * 16 + 0xC,0b00000000,false,3)

I moved your topic to an appropriate forum category @flavios.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

Line 42 of what? The library example?

Look at the libraries implementation, read it.

If I got it right... your line does not work, the result of 27*16+0xC is 1BC (that shows on the display as 1bC) - if I add one to yout t variable it goes 1CC so I won't be able to put the temp there ... sure there must be a way to write a function to put 22 decimal and shift it left to allow for the last 0xC digit and make it displayed as HEX.

We'd want 0x220 0x230 0x240 ... so 22 HEX to DEC = 34 * 16 + 0xC = 22C

The conversion of a string should be less complex. Or go to binary?
Flavio

This sketch demonstrate it...

int t = 0;
int tx = 0x1;

void setup() {
    Serial.begin(115200);
    Serial.println("Inizialize SERIAL...");
}

void loop() {
  t = 27;
  tx = t*16+0xC;
  Serial.println(tx, HEX);
}

You are correct, sorry about that!

It may seem less complex to you, but not for the Arduino, it's very inefficient.

Ultimately, it's all binary!

How many digits does your display have?

I did read it. This does not guarantee that I got it right :wink: ... sure I found no line 42 (in the example, the header or the .cpp)

I wonder if you refer to using the other function with a DEC input. This works (I used it for the temp negative values) BUT I cannot add the "C" for Centigrade that casually is also an HEX number.

I could write something more complex using the individual segments, but I was just asking to avoid it for a more elegant solution...
Flavio

Four! I'll use it for time and temperature...
Tomorrow I'll give it a try with binary.
Tks
Flavio

You need to convert the temperature value into BCD (binary coded decimal) then add the "C" digit before displaying it.

Some TM1637 modules have 6 digits, I think, hence my question.

If you have an int variable that is the temperature, just pass it in like you are with the constants.

It's a number, it's holding the value of some temperature. There is no need to shoehorn it into a textual representation of a hexadecimal number so it looks the same as 0x22C or whatever.

a7

I agree there is no need to translate to a text representation, but some shoehorning may be required to translate the temperature value into BCD and then append the "C" digit before displaying it.

This should work as long as the temperature is only 2 digits.

int t = 22;
int bcd = ((t / 10) * 16 + t % 10) * 16 + 0xC;
display.showNumberHexEx(bcd,0b00000000,false,3);          // shows: _22C_

Is the temperature from a type K thermocouple and could be hundreds of degrees? If so:

int t = 321;
int bcd = ((t / 100) * 256 + ((t / 10) % 10) * 16 + t % 10) * 16 + 0xC;
display.showNumberHexEx(bcd,0b00000000,false,3);          // shows: _321C_

Tks. I'm in bed with fever so I'll check tomorrow... I am happy with rounding it up to zero decimals... it is for a small electric car...

OIC. I was not reading the 'C' as the crude trick it would be to exploit it as 'C' for centigrade.

That would work for Fahrenheit also! Kelvin's outta luck here for using that trick.

So yes, 10 * temperature converted to three BCD nibbles and add 0xc.

I'm too lazy to see if that's any better or worse than doing it in the several ways I did think of.

a7

Hmm... negative values are supported automatically for decimal, but then you can't use the C/F trick. showNumberHexEx takes a uint16_t, so the coldest would be 0F. But looks like you can use showNumberBaseEx and pass -16 as the base, and get down to -99C.

Then it's just doing the int to BCD conversion, and since the numbers are limited to three digits -- you'll want to clamp them at the min/max -- it's pretty straightforward.

Are we dealing with this library?

  void showNumberHexEx(uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);

I would probably try it with 2 calls. First with length = 3, pos = 0 for setting the number.
The second with length = 1, pos = 3 for setting the C.
This would avoid the conversion decimal -> BCD.

I am in bed with over 38C :face_with_thermometer: :smile:

YES! It could work... I tried the other way around but - at least - the leading blanks were "printed".
Will try. Tks to all.
F